Friday, 20 October 2017

Calculator Application in java

Project Detail: Create calculator application which accepts two numbers from user and perform addition, subtraction, multiplication and division on given numbers.
                                     --------------------------  *  -----------------------------


SourceCalculation.java

package calculator;

public class Calculation {
      int result;
      //Method for addition
      public int add(int num1, int num2){
            result = num1+num2;
            return result;
      }
      //Method for subtraction
      public int substract(int num1, int num2){
            result = num1-num2;
            return result;
      }
      //Method for multiplication
      public int multiply(int num1, int num2){
            result = num1*num2;
            return result;
      }
      //Method for division
      public float divide(int num1, int num2){
            result = num1/num2;
            return result;
      }
     
}

SourceCalculatorVO.java

package calculator;

public class CalculatorVO {
      private int firstNumber;
      private int secondNumber;
     
      public int getFirstNumber() {
            return firstNumber;
      }
      public void setFirstNumber(int firstNumber) {
            this.firstNumber = firstNumber;
      }
      public int getSecondNumber() {
            return secondNumber;
      }
      public void setSecondNumber(int secondNumber) {
            this.secondNumber = secondNumber;
      }
     

}

SourceMainClass.java

package calculator;

public class MainClass {

      public static void main(String[] args) {
           
            CalculatorVO calVO = new CalculatorVO();
            //Input can be taken from user
            calVO.setFirstNumber(100);
            calVO.setSecondNumber(11);
           
            Calculation cal = new Calculation();
           
            int addResult = cal.add(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Addition Result ="+addResult);
           
            int subResult = cal.substract(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Subtraction Result ="+subResult);
           
            int mulResult = cal.multiply(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Multiplication Result ="+mulResult);
           
            float divResult = cal.divide(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Division Result ="+divResult);

      }

}


OUTPUT:
Addition Result =111
Subtraction Result =89
Multiplication Result =1100
Division Result =9.0

1 comment:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.