Question

In: Computer Science

Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...

Write a calculator program that prompts the user with the following menu:

Add

Subtract

Multiply

Divide

Power

Root

Modulus

Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program.

EXAMPLE PROGRAM EXECUTION:

Add

Subtract

Multiply

Divide

Power

Root

Modulus

Please enter the number of the menu option that corresponds to the operation you'd like to perform: 7

Please enter the first value: 5

Please enter the second value: 3

5 % 3 = 2

Would you like to perform another calculation? Y

Add

Subtract

Multiply

Divide

Power

Root

Modulus

Please enter the number of the menu option that corresponds to the operation you'd like to perform: 2

Please enter the first value: 5

Please enter the second value: 3

5 - 3 = 2

Would you like to perform another calculation? N

THANK YOU! GOODBYE!

THIS IS WHAT I HAVE SO FAR BUT NOT WORKING :(

package homework02;

import java.util.Scanner;

/**

*

* @author Phpawn

*/

public class HOMEWORK02 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

  

Scanner scan= new Scanner(System.in);

  

String result;

double formula,firstnumber = 0,secondnumber = 0;

  

do{

  

System.out.println("1.add\n 2.subtract\n 3.multiply\n 4.divide\n 5.power\n 6.root\n 7.modulus\n)");

System.out.println("Please enter the number of the menu option that corresponds to the operation you'd like to perform");

formula= scan.nextInt();

  

if(formula==1){

System.out.printf("%f + %f = %f",firstnumber,secondnumber,firstnumber+secondnumber);

}

else if (formula==2){

System.out.printf("%f - &f = %f",firstnumber,secondnumber,firstnumber-secondnumber);

}

else if (formula==3){

System.out.printf ("%f * %f = %f",firstnumber,secondnumber,firstnumber*secondnumber);

}

else if(formula==4){

System.out.printf("%f / %f = %f",firstnumber,secondnumber,firstnumber/secondnumber);

}

else if(formula==5){

System.out.printf("%f ^ %f = %f", firstnumber,secondnumber,Math.pow(firstnumber,secondnumber));

}

else if(formula==6){

System.out.printf("%f^1/%f = %f",firstnumber,secondnumber,Math.pow(firstnumber,1/secondnumber));

}

else if(formula==7){

System.out.printf("%f %% %f = %f",firstnumber,secondnumber,firstnumber%secondnumber);

  

}

  

System.out.println("Would you like to perform another operation");

result = scan.next();

}

while (result.equals("y"));

}

}

Solutions

Expert Solution

Hi

I have modified the code and working fine now and highlighted the code changes below.

HOMEWORK02.java

import java.util.Scanner;

public class HOMEWORK02 {
public static void main(String[] args) {
  
Scanner scan= new Scanner(System.in);
  
String result;
double formula,firstnumber = 0,secondnumber = 0;
  
do{
  
System.out.println("1.add\n 2.subtract\n 3.multiply\n 4.divide\n 5.power\n 6.root\n 7.modulus\n)");
System.out.println("Please enter the number of the menu option that corresponds to the operation you'd like to perform");
formula= scan.nextInt();
System.out.println("Please enter the first value: ");
firstnumber = scan.nextDouble();
System.out.println("Please enter the second value: ");
secondnumber = scan.nextDouble();

if(formula==1){
System.out.printf("%f + %f = %f",firstnumber,secondnumber,firstnumber+secondnumber);
}
else if (formula==2){
System.out.printf("%f - %f = %f",firstnumber,secondnumber,firstnumber-secondnumber);
}
else if (formula==3){
System.out.printf ("%f * %f = %f",firstnumber,secondnumber,firstnumber*secondnumber);
}
else if(formula==4){
System.out.printf("%f / %f = %f",firstnumber,secondnumber,firstnumber/secondnumber);
}
else if(formula==5){
System.out.printf("%f ^ %f = %f", firstnumber,secondnumber,Math.pow(firstnumber,secondnumber));
}
else if(formula==6){
System.out.printf("%f^1/%f = %f",firstnumber,secondnumber,Math.pow(firstnumber,1/secondnumber));
}
else if(formula==7){
System.out.printf("%f %% %f = %f",firstnumber,secondnumber,firstnumber%secondnumber);
}
System.out.println();
System.out.println("Would you like to perform another operation");
result = scan.next();
}
while (result.equals("y"));
}
}

Output:

1.add
2.subtract
3.multiply
4.divide
5.power
6.root
7.modulus
)
Please enter the number of the menu option that corresponds to the operation you'd like to perform
1
Please enter the first value:
5
Please enter the second value:
6
5.000000 + 6.000000 = 11.000000
Would you like to perform another operation
y
1.add
2.subtract
3.multiply
4.divide
5.power
6.root
7.modulus
)
Please enter the number of the menu option that corresponds to the operation you'd like to perform
2
Please enter the first value:
6
Please enter the second value:
5
6.000000 - 5.000000 = 1.000000
Would you like to perform another operation
y
1.add
2.subtract
3.multiply
4.divide
5.power
6.root
7.modulus
)
Please enter the number of the menu option that corresponds to the operation you'd like to perform
3
Please enter the first value:
8
Please enter the second value:
9
8.000000 * 9.000000 = 72.000000
Would you like to perform another operation
y
1.add
2.subtract
3.multiply
4.divide
5.power
6.root
7.modulus
)
Please enter the number of the menu option that corresponds to the operation you'd like to perform
4
Please enter the first value:
6
Please enter the second value:
2
6.000000 / 2.000000 = 3.000000
Would you like to perform another operation
y
1.add
2.subtract
3.multiply
4.divide
5.power
6.root
7.modulus
)
Please enter the number of the menu option that corresponds to the operation you'd like to perform
5
Please enter the first value:
6
Please enter the second value:
6
6.000000 ^ 6.000000 = 46656.000000
Would you like to perform another operation
y
1.add
2.subtract
3.multiply
4.divide
5.power
6.root
7.modulus
)
Please enter the number of the menu option that corresponds to the operation you'd like to perform
6
Please enter the first value:
36
Please enter the second value:
6
36.000000^1/6.000000 = 1.817121
Would you like to perform another operation

n


Related Solutions

PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the...
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator: The calculator has one “accumulator”, which holds the result of all prior calculations. The calculator is set to 0.0 when the calculator is turned on (i.e., instantiated). On the console, the user then enters an operator (+, -, *, or /) and a number. Your console will then apply the operator and operand to the number in the accumulator, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator. For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as...
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers....
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. The class must check for a correct operator (+,*,-,/), and a number (integer) for the second and third argument entered. The calculator cannot divide by zero...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
In Python, write a calculator that will give the user the following menu options: 1) Add...
In Python, write a calculator that will give the user the following menu options: 1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Example 1: Let's calculate! 1) Add 2)...
Write a python program to display a menu with the following options: (1) add, (2) subtract,...
Write a python program to display a menu with the following options: (1) add, (2) subtract, (3) multiply, (4) divide (5) mod, and (6) do nothing. I f the user enters something else (except 1-6), the program should display an error message. Otherwise, it should ask the user for two numbers, perform the calculation, and display the result.
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
How to add, multiply, and divide numbers in flowgorithm
How to add, multiply, and divide numbers in flowgorithm
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to enter two integers and an operation and then outputs the answer. Prompt the user three times, don't expect all the inputs on one line. Possible operations are + - * / No other words or symbols should be output, just the answer. Requested files import java.util.Scanner; /** * This is a simple calculator Java program. * It can be used to calculate some simple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT