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

Written Assignment: Calculator Program Design a calculator program that will add

ID: 3626716 • Letter: W

Question

Written Assignment: Calculator Program

Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.

Your program should contain the following:
•The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculator program.
• When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displaying the result, prompting the user to add two more different numbers) until the user enters a sentinel value to end the chosen arithmetic operation.
• If the user chooses division, do not allow the user to divide by 0. Display an error message to user and ask the user to choose another denominator.

You are to submit, as a Microsoft Word Document, the following for this assignment:
1. Program Code

Remember to follow the guidelines of good program design. Make sure to use meaningful variable names and thoroughly comment each line of your code. You may only use techniques learned in Modules One, Two and Three.

Below is a screen output sample:

Welcome to the Calculation Program

(1) Addition
(2) Subtraction
(3) Multiplication
(4) Division
(Q) Quit Program
Thanks

Explanation / Answer

import java.io.InputStreamReader;
import java.io.BufferedReader ;
import java.io.IOException;

public class calculate {
    public static void main (String[] args) {
   
    while(true) {
        System.out.println("Welcome in Calculator Choose 1 for ADD Choose 2 for SUB Choose 3 for MUL Choose 4 for DIV Q for Exit");
            String str = read();
        if (str.equals("Q")) {
            System.out.println("Goodbye!");
            System.exit(-1);
        }
        if (str.equals("1"))
            System.out.println("Result :" +add());
        if (str.equals("2"))
            System.out.println("Result :" +sub());
        if (str.equals("3"))
            System.out.println("Result :" +mul());
        if (str.equals("4"))
            div();
        System.out.println("Choose again or quit with Q ");    
        }
    }
   
    static String read() {   
        InputStreamReader reader = new InputStreamReader (System.in);
        BufferedReader myInput = new BufferedReader (reader);
        String str = new String();
        try {
            str = myInput.readLine(); } catch (IOException e) {
            System.out.println ("Error :( " + e);
            System.exit(-1);
        }
        return str;
    }
    static int add() {
        System.out.println ("Welcome on ADD, please write one number and press Enter");
        int a = Integer.parseInt(read());
        System.out.println ("Another one number and press Enter");
        int b = Integer.parseInt(read());
        return a+b;
    }
    static int sub() {
        System.out.println ("Welcome on SUB, please write one number and press Enter");
        int a = Integer.parseInt(read());
        System.out.println ("Another one number and press Enter");
        int b = Integer.parseInt(read());
        return a-b;
    }
    static int mul() {
        System.out.println ("Welcome on MUL, please write one number and press Enter");
        int a = Integer.parseInt(read());
        System.out.println ("Another one number and press Enter");
        int b = Integer.parseInt(read());
        return a*b;
    }
    static void div() {
        System.out.println ("Welcome on DIV, please write one number and press Enter");
        int a = Integer.parseInt(read());
        System.out.println ("Another one number and press Enter");
        int b = Integer.parseInt(read());
        while (b==0) {
            System.out.println("Sorry, but I can't divide by 0, retry!");
            b = Integer.parseInt(read());
        }
        System.out.println("Result :" +a/b);
    }
}