Tuesday, February 17, 2015

Basic Java Calculator using Methods

import java.util.Scanner;

public class calculator{
   
    private static final Scanner STDIN = new Scanner(System.in);
   
    public static void main(String[] args){
       
        int inpOpr;
        System.out.println("\n 1. Addition");
        System.out.println("\n 2. Substraction");
        System.out.println("\n 3. Multiplication");
        System.out.println("\n 4. Division");
        System.out.println("\n Enter your choice");
  

        //Scanner STDIN = new Scanner(System.in);
       
        inpOpr = STDIN.nextInt();
       
        while(!(inpOpr > 0 && inpOpr < 5)){
       
            System.out.println("\n Invalid input.. \n Please enter your choice again: ");
            inpOpr = STDIN.nextInt();       
        }
       
        switch(inpOpr){
            case 1:
                performAddition();
                break;
           
            case 2:
                performSubstraction();
                break;
               
            case 3:
                performMultiplication();
                break;
           
            case 4:
                performDivision();
                break;
       
       
        }
   
    }
   
    public static void performAddition(){
        int numbers[] = inputNumbers();
        int sum = numbers[0] + numbers[1];
        System.out.println("Sum is "+sum);
   
    }
    public static void performSubstraction(){
       
        int numbers[] = inputNumbers(); //get two input numbers in array from the function
        int sum = numbers[0] - numbers[1];
        System.out.println("Substraction is "+sum);
   
    }
    public static void performMultiplication(){
       
        int numbers[] = inputNumbers();
        int multi = numbers[0] * numbers[1];
        System.out.println("Multiplication is "+multi);
   
    }
    public static void performDivision(){
       
        int numbers[] = inputNumbers();
        int div = numbers[0] / numbers[1];
        System.out.println("Division is "+div);
   
    }
   
    public static int[] inputNumbers(){ //get two input numbers for all operation
   
        int[] numbers = new int[2];
        System.out.println("Input first integer: ");
        numbers[0]= STDIN.nextInt();
        System.out.println("Input second integer: ");
        numbers[1]= STDIN.nextInt();
       
        return numbers;
   
    }

}

2 comments:

  1. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    aws training in chennai
    AWS TRAINING IN VELACHERY
    AWS TRAINING IN TAMBARAM
    aws training in chennai
    aws training in annanagar
    aws training in chennai

    ReplyDelete
  2. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
    AWS Training in Bangalore
    AWS training in sholinganallur
    AWS training in Tambaram
    AWS training in Velachery

    ReplyDelete