Friday, August 26, 2022

Setting up the environment variables for Java in Windows

How to set the Environment Variables for Java in Windows?

Step 1 - Go to Control Panel -> System and Security  System. In System, select the Advanced System Settings option as highlighted below. 


Step 2 - Under the Advanced System Setting option click on Environment Variables as highlighted below. Alternately, you can also search for Environment Variables in Windows search and it will directly go to Environment Variables



Step 3 - Now, Alter the “Path” variable under System variables so that it also contains the path to the Java environment. Select the “Path” variable and click on the Edit button as highlighted below. 
NOTE: Edit the Path in System Variables, and not in User Variables



 

Step 4 - You will see a list of different paths, click on the New button, and then add the path where java is installed. Add the path of the location where you have installed Java. 



Step 5 - Click on OK to save the settings. Now to check whether the installation is done correctly or not, open the command prompt and type java --version. You will see that java is running on your machine and it will also display the installed Java version.



To make sure whether the compiler is set up, type javac in the command prompt. You will see a list related to javac.



Congratulations Java is now installed in your system

Sunday, August 21, 2022

How to download and install Java in Windows?

Below is the process to download and install Java in Windows

Follow the below steps to download and install Java in windows. All the steps described below have been performed on the Windows 10 operating system, but the procedure is quite similar to other windows versions as well.

Step 1 - Go to https://www.oracle.com/java/technologies/downloads. This takes you to the Official Oracle Java Downloads page.



Step 2 - Scroll down to the version of Java that you want to download and click on the Operating System for which you want to download(In our case, it's Windows) as shown below.



Step 3 - Click on the download link suitable for your computer Operating system. Here, we have selected the x64 Installer. 



Step 4 - After the downloading procedure is complete, we need to run the installer(i.e., the downloaded file). Once the Java installation wizard opens, click on the Next button as shown below.




Step 5 - Again click on the Next button if we wish to install the Java development kit in the default directory, or we can change this directory by clicking on the Change button. 



Step 7 - The installation will begin as shown below.


Step 10 - After the installation is completed we will get the below confirmation window. We can click on the Close button after the confirmation window appears that the Java is installed. 



Step 11 - Setup the Environment variables. Follow the below link to set Environment Variables.
https://www.computerscience.hydentsoft.com/2022/08/setting-up-environment-variables-for.html



Monday, August 15, 2022

Introduction to Java

What is Java?

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. 

It is a general-purpose programming language intended to let programmers write once, and run anywhere meaning that compiled Java code can run on all platforms that support Java without the need to recompile.

Brief History of Java

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. 
Initially, it was named Oak. Since Oak was already a registered company, James Gosling and his team changed the name to Java.


Features of Java

Simple - Java is easy to learn and its syntax is quite simple, clean, and easy to understand.

Secure - Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.

Robust - Java is robust because it utilizes strong memory management. There is an absence of pointers that bypasses security dilemmas. There is automatic garbage collection in Java which runs on the Java Virtual Machine to eliminate objects which are not being accepted by a Java application anymore.

Object Oriented - In java, everything is an object which has some data and behavior.

Platform Independence - Unlike other programming languages such as C, C++, etc which are compiled into platform-specific machines. Java is guaranteed to be a write-once, run-anywhere language.

High Performance - Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But, Java enables high performance with the use of the Just-In-Time(JIT) compiler.

Distributed - Java is also a distributed language. Programs can be designed to run on computer networks. Java has a special class library for communicating using TCP/IP protocols.

Saturday, August 13, 2022

Binary Numbers in Java

Binary Literals are the literals that consist of only 0s and 1s. Java allows you to declare a binary number by adding a prefix `0b` or `0B`.


Example 1

public class BinaryNumber {
    public static void main(String[] args)
    {
        byte decNumber = 3; // Decimal number
        byte binNumber = 0b011; // Binary value of 3
        byte binNumber1 = 0B011; // Binary value of 3
  
        System.out.println("Decimal Number is " + decNumber);
        System.out.println("Binary Number with b is " + binNumber);
        System.out.println("Binary Number with B is " + binNumber);
    }
}

Output
Decimal Number is 3
Binary Number with b is 3
Binary Number with B is 3

Let's take another example.

Example 2

public class BinaryNumber {
    public static void main(String[] args)
    {
        byte decNumber = 3; // Decimal number
        byte binNumber = 0b101; // Binary value of 5
        byte binNumber1 = 0B101; // Binary value of 5
  
        System.out.println("Decimal Number is " + decNumber);
        System.out.println("Binary Number with b is " + binNumber);
        System.out.println("Binary Number with B is " + binNumber);
    }
}

Output

Decimal Number is 3
Binary Number with b is 5
Binary Number with B is 5

Tuesday, August 9, 2022

Octal numbers in Java

Literals with a leading zero are octal literals. 

Any number prefixed with a 0 is considered octal in Java(or in many programming languages for that matter). 

Octal numbers can only use digits 0-7, just like decimals can use 0-9, and binary can use 0-1.


Here is the declaration and initialization.

int myOct = 034;

Example

public class Octal1 {
   public static void main(String []args) {
      int myOct = 034;
      System.out.println(myOct);
   }
}

Output

28

Let's take another example.

Example 2:


public class Demo {
   public static void main(String []args) {
      int myOct = 023;
      System.out.println(myOct);
   }
}

Output

19
The output will always be printed in decimal. That is the reason why we get the decimal values as `28` and `19` in the output.
The octal of  `19`(decimal) is `023`(octal) and the octal of `28`(decimal) is `034'(octal)

Sunday, August 7, 2022

Java Program 1 - Print the details of a person in Tabular form

Program to write the details in tabular form. The tabular form means, the column names will be teh properties like, Name, Institute and Location.

TestCase:
Name: MyName
Institute: MyCollege
Location: MyLocation

Output:


  

This can be done in two ways, using System.out.print() and System.out.format().

Using System.out.print()

In this approach, first we hardcode the column names, "Name", "Institute", "Location" and then we start the for loop, this for loop will be running till the end of the largest string. Inside the for loop, we write the condition to take an empty space when the characters in any of the string is over(This is because the length of the strings could be different for different words).

As the print() function does not have any formatting options, we have to give the tab spaces based on the length of the column heading.
For example, the 2nd Column(Institute) has 9 characters and to make sure that all the rows align with the column names we will be giving the two tab spaces inside the for loop. 

Code:

public class MyDetails {
public static void main(String[] args) {
String name = "MyName";
String institute = "MyCollege";
String location = "MyLocation";

int length = Math.max(location.length(), Math.max(name.length(), institute.length()));
System.out.println("---------------------------------");
System.out.print("Name" + "\t");
System.out.print("Institute" + "\t");
System.out.print("Location");
System.out.println();
System.out.println("---------------------------------");
for(int i=0; i< length; i++) {
char nameChar = i < name.length() ? name.charAt(i) : ' ';
char instituteChar = i < institute.length() ? institute.charAt(i) : ' ';
char locationChar = i < location.length() ? location.charAt(i) : ' ';
System.out.print(nameChar + "\t");
System.out.print(instituteChar + "\t" + "\t");
System.out.println(locationChar);
}
}
}




Using System.out.format()

Similar to the above approach, here too we will be hardcoding the values of the column headings(Name, Institute and Location). But the advantage with the format() method is that this method provides the ways to format the text in a tabular form. The %10s means 10 spaces will be left before this character/String is printed. System.out.format("%s %14s %17s", "Name", "Institute", "Location"); means we leave 0 spaces before Name, 14 spaces before Institute and 17 spaces before Location. Similarly, we add the formatting in the for loop as well, and it makes sure to leave the given spaces before printing the character

public class MyDetails2 {
public static void main(String[] args) {
String name = "MyName";
String institute = "MyCollege";
String location = "MyLocation";

int length = Math.max(location.length(), Math.max(name.length(), institute.length()));
System.out.println("---------------------------------");
System.out.format("%s %14s %17s", "Name", "Institute", "Location");
System.out.println();
System.out.println("---------------------------------");
for(int i=0; i< length; i++) {
char nameChar = i < name.length() ? name.charAt(i) : ' ';
char instituteChar = i < institute.length() ? institute.charAt(i) : ' ';
char locationChar = i < location.length() ? location.charAt(i) : ' ';
System.out.format("%s %9s %18s", nameChar, instituteChar, locationChar);
System.out.println();
}
}
}


Saturday, August 6, 2022

Leetcode #1 - Two Sum

 Problem Statement:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.

Bruteforce Approach:

The idea here is that we have to find the two values from the array whose sum is equal to target. the brute force approach would be to loop through all the elements in the array and another nested loop to loop from the next elements onwards.

Code:

class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i=0; i<nums.length; i++) {
for(int j=i+1; j<nums.length; j++) {
if(nums[i] + nums[j] == target)
return new int[] {i,j};
}
}
return new int[] {0,0};
}
}

Time Complexity: O(n*n)
Space Complexity: O(1)

As we can see, in the brute force approach, the time complexity is O(n*n). We can refine our solution and use the extra linear space and improve the time complexity to linear time.


Optimal Approach:

In this approach, we will be using a HashMap to store the elements with arrayValue as Key and index as value. We will loop through all the elements of the array, and for each element, we check if the map already contains `target - currentValue`. If it exists we return the value from the Map and the current index, else we add the current value into the map along with its index.

Code:

class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); int[] ans = new int[2]; for(int i=0; i< nums.length; i++) { if(hm.containsKey(target - nums[i])) { ans[0] = hm.get(target - nums[i]); ans[1] = i; } else { hm.put(nums[i], i); } } return ans; } }

Time Complexity: O(n)
Space Complexity: O(n)

LeetCode link: https://leetcode.com/problems/two-sum/

Setting up the environment variables for Java in Windows

How to set the Environment Variables for Java in Windows? Step 1 -   Go to Control Panel -> System and Security  System .  In System, sel...