Answer to Question #323598 in Java | JSP | JSF for Harfa

Question #323598

Build a simple “English Language” calculator that does the following:

●    Takes three inputs from the keyboard

●    Two of the inputs are single-digit numbers (0 to 9)

●    The third input is a char from the keyboard, representing one of the five operations from the keyboard:

○    + (addition)

○    - (subtraction)

○    * (multiplication)

○    / (division)

○    ^ (exponentiation)

●    Output the description of the operation in plain English, as well as the numeric result

 

1
Expert's answer
2022-04-04T19:55:58-0400
import java.io.IOException;
import java.util.Scanner;

import static java.lang.Math.pow;

public class Calc {
    public static void main(String[] args) throws IOException {
        Scanner scan=new Scanner(System.in);
        System.out.print("Enter first number(0 to 9): ");
        int fNumb=scan.nextInt();
        System.out.print("Enter second number(0 to 9): ");
        int sNumb=scan.nextInt();
        System.out.println("Choose one of operation: +, -, *, /, ^.  ");
        char oper=(char)System.in.read();

        switch(oper){
            case '+':
                System.out.println("Result of addition: "+(fNumb+sNumb));
                break;
            case '-':
                System.out.println("Result of subtraction: "+(fNumb-sNumb));
                break;
            case '*':
                System.out.println("Result of multiplication: "+(fNumb*sNumb));
                break;
            case '/':
                System.out.println("Result of division:  "+  ((double) fNumb/(double) sNumb));
                break;
            case '^':
                System.out.println("Result of exponentiation: "+(pow(fNumb,sNumb)));
                break;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS