Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA- Console calculator with rational numbers.HELP! Need working example! Write

ID: 3765959 • Letter: J

Question

JAVA- Console calculator with rational numbers.HELP! Need working example!

Write a calculator program that supports the following operations: +, -, *, and /. Input will either be in the form operand operator operand, or operator operand, in which case you use the previous result as the first operand.

Thank you!

Write a calculator program that supports the following operations:+, -, *, and/. Input will either be in the form operand operator operand, or operator operand, in which case you use the previous result as the first operand. Example interactions (user input in italics): 34 2.3 36.3 39.3 2.3 37 1+2 If input is not in the expected format, show an error message. 34 2.3 36.3 /3 Error: input must be operand operator operand, or operator operand /3 12.1 Note: Do not allow division by zero, this should show an error message. Note: Your program need not handle exponent notation for doubles as input, e.g., 34.3e-4.

Explanation / Answer

import java.util.*;

class calculator
{
   public static void main(String args[])
   {
       int a=0,b,c;
       String s;
       int i=0;

       char o;

       while (true)
       {
           Scanner scan = new

Scanner(System.in);
           s=scan.nextLine();
           if(s.length()==0)
               break;
           else if(s.length()<=1)
              

System.out.println("Pl. put space between all operand and

operator");
           String[] arr = s.split(" ");
           if(i==0)
           {
           a=Integer.parseInt(arr[0]);
           o=arr[1].charAt(0);
           b=Integer.parseInt(arr[2]);
           i++;
           }
           else
           {
           o=arr[0].charAt(0);
           b=Integer.parseInt(arr[1]);  
           }
           switch(o)
           {
               case '+':
                   a=a+b;
                  

System.out.println(a);
                   break;

               case '-':
                   a=a-b;
                  

System.out.println(a);
                   break;
               case '*':
                   a=a*b;
                  

System.out.println(a);
                   break;
               case '/':
                   a=a/b;
                  

System.out.println(a);
                   break;
           }
          
       }
   }
}