Sunday, July 16, 2017

Write a Java basic calculator.

/**
 * Created by Jo on 7/10/2017.
 * For more exercises and lessons, visit http://shegertech.blogspot.com/
 * Chapter 6, Exercise 1
 */

import java.util.Scanner;

public class Q1Solution {

    private float firstOperannd;
    private float secondOperannd;
    private float result;
    private int operator;
    private Scanner input = new Scanner(System.in);
    private boolean exitCalculator = false;

    public void startCalculator() {
        while (!exitCalculator) {

            System.out.print("Enter 1 for addition \n"
                    + "Enter 2 for subtraction \n"
                    + "Enter 3 for multiplication \n"
                    + "Enter 4 for division \n" + "Enter 0 for Exit"+"\n\nInput Menu number : ");
            operator = input.nextInt();

            switch (operator) {
                case 1:
                    result = add();
                    System.out.println("\nResult is " + result+"\n");
                    break;

                case 2:
                    result = subtract();
                    System.out.println("\nResult is " + result+"\n");
                    break;

                case 3:
                    result = multiply();
                    System.out.println("\nResult is " + result+"\n");
                    break;

                case 4:
                    result = divide();
                    System.out.println("\nResult is " + result+"\n");
                    break;

                case 0:
                    exitCalculator = true;
                    System.out.println("Calculator program Terminated \n");
                    break;

                default:
                    System.out.println("Please provide proper input \n");

            }
        }
    }

    private float add() {
        System.out.print("\nEnter first number : ");
        firstOperannd = input.nextInt();
        System.out.print("\nEnter second number : ");
        secondOperannd = input.nextInt();
        return firstOperannd + secondOperannd;
    }

    private float subtract() {
        System.out.print("\nEnter first number : ");
        firstOperannd = input.nextInt();
        System.out.print("\nEnter second number : ");
        secondOperannd = input.nextInt();
        return firstOperannd - secondOperannd;
    }

    private float multiply() {
        System.out.print("\nEnter first number : ");
        firstOperannd = input.nextInt();
        System.out.print("\nEnter second number : ");
        secondOperannd = input.nextInt();
        return firstOperannd * secondOperannd;
    }

    private float divide() {
        System.out.print("\nEnter first number : ");
        firstOperannd = input.nextInt();
        System.out.print("\nEnter second number : ");
        secondOperannd = input.nextInt();
        return firstOperannd / secondOperannd;
    }

    public static void main(String[] args) {
        Calculation c= new Calculation();
        c.startCalculator();
    }
}
Share: