Daily Archives: March 4, 2012

Java || Simple Math Using Int & Double

This page will display the use of int and double data types.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

Notice in lines 14-16, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 29 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.

I obtained data from the user by using the Scanner Class.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Add.java

*** To run the compiled program, simply type this command:

java Add

The above code should give you the following output:

Please enter the first number: 8
Please enter the second number: 24
The sum of 8 and 24 is: 32

==== SUBTRACTING TWO NUMBERS ====

Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 29, replace the addition symbol with a subtraction sign, and you should have something like this

Note: In the above example, “cin.nextDouble()” was used on line 26 in place of “nextInt.” The declaration “nextDouble” can be used in place of “nextInt” in case you want to obtain floating point data from the user.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Subtract.java

*** To run the compiled program, simply type this command:

java Subtract

The above code should give you the following output

Please enter the first number: 8
Please enter the second number: 23.99999
The difference between 8 and 23.99999 is: -15.99999

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 29, and replacing the designated math operator with the star symbol “*”.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Multiply.java

*** To run the compiled program, simply type this command:

java Multiply

The above code should give you the following output

Please enter the first number: 7.999999
Please enter the second number: 24
The product of 7.999999 and 24 is: 191.99997


==== DIVIDING TWO NUMBERS TOGETHER ====

In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where the use of the double data type is mandatory.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Divide.java

*** To run the compiled program, simply type this command:

java Divide

The above code should give the following output

Please enter the first number: 7.99999
Please enter the second number: 23.99999
The quotient of 7.99999 and 23.99999 is: 0.33333305

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 29, from division, to modulus.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Modulus.java

*** To run the compiled program, simply type this command:

java Modulus

The above code should give the following output

Please enter the first number: 23.99999
Please enter the second number: 8
The remainder of 23.99999 and 8 is: 7.9999905

Java || Hello World!

This page will consist of creating the typical “hello world!” application. First, you will need to create a new project in whichever IDE you are using. I am using a simple text editor and the terminal, but any compiler will do.

When you have everything loaded up, you will start your code off by entering the headers like so into your HelloWorld.java file.


The most basic form of a class definition is shown above. The keyword “class” begins the class definition for the class named “HelloWorld.”

Next, you are going to add the main function. This will generally look something like this:


Every program in Java must contain a main method declaration as shown above. The declaration “public” and “static” can be written in any order (public static or static public), but the custom is to use “public static.”

We are halfway finished, now all we have to do is say hello to the world! You will do that by adding this line of code to your program, which basically outputs data to the screen


The above code uses the “System” class from the core library to print the “Hello World!” message to standard output.

So when you add everything together, your full code will look something like this

===== HOW TO COMPILE CODE USING THE TERMINAL =====

Once you have loaded the terminal, if you have your java source file saved in any directory other than the home folder, you will need to change directories to that specific folder where your source file resides. Many typically save their source files to the desktop.

*** If your files are located on the desktop, in the terminal, type this command:

@ubuntu:~$ cd Desktop

*** Next we will compile the java source file, and this can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

@ubuntu:~/Desktop$ javac HelloWorld.java

*** To run the compiled program, simply type this command:

@ubuntu:~/Desktop$ java HelloWorld

And there you have it! After you compile the above code, you should get this as your output

Hello World!