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
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...
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 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...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT