Friday, 2 September 2011

Calculator Program

           
                Previously, you have learnt how to write a simple Java program using a Notepad or an IDE. From now onwards, we shall be using Eclipse IDE(as I prefer it more on my Linux machine). So, if you have skipped the tutorial on how to use Eclipse IDE, do check it out once. 

Next program--> Write a program that takes two numbers and produces the sum, difference, product and quotient of those numbers. 

   Step 1-->  Open Eclipse IDE. Create a new Java class in the existing project you created in last tutorial. 
                    Name the file "Calculator.java".
   Step2-->  Write the following code.
                 
                   
//Declaring the class(Note:- The filename & the class name should be same)
public class Calculator {

 //The main() method. The starting point of the Java program.
public static void main(String[] args) {

              //Declaring the variables
int firstNum, secondNum;  // Stores the input numbers
int sum=0;                         // This variable stores the sum of two numbers
int difference=0;               // This variable stores the difference of two numbers
int product=0;                   // tutorial  This variable stores the product of two numbers
float quotient=0;               // This variable stores the quotient of two numbers
 
firstNum=100;                   // Storing 100 in first variable
secondNum=50;                // Storing 50 in second variable
 
// Adding and storing the sum of two numbers
sum=firstNum+secondNum;          

                 // Subtracting and storing the difference of two numbers
difference=firstNum-secondNum; 

                 // Multiplying and storing the product of two numbers  
product=firstNum*secondNum;   

                  // Dividing and storing the quotient of two numbers 
quotient=firstNum/secondNum;   
 

             // Displaying the results in system console
System.out.println("Sum is: "+sum);   
System.out.println("Difference is: "+difference);
System.out.println("Product is: "+product);
System.out.println("Quotient is: "+quotient);
 
}
}

  Step3-->  Save & run the code.

Explanations--> 

          There are a few things in this program that you have not seen earlier.
  • Variable Declaration--> 
                The format of declaring variables is:-
                         data_type variable1,variable2...;

         Now, data_type can be can be either Java's primitive data_types (int,float,char, boolean,etc.) or user_defined data_types(e.g. Class).  
           
  • Operation on variables--> As you can see, we have used 4 different operators on two variables to produce different results. These operators(+,-,*,/) are called arithmetic operators. There are several other operators. We shall deal with them when we do more programs.


Now, that completes this lesson. Click on Next Tutorial for the next program.


>>Next Tutorial>>      

_____________________________________________________________________________________

No comments:

Post a Comment