In: Computer Science
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"));
}
}
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