In: Computer Science
Java Programming:
Write a program that allows the user to compute the power of a number or the product of two numbers.
Your program should prompt the user for the following information:
• Type of operation to perform
• Two numbers (the arguments) for the operation to perform
The program then outputs the following information:
• The result of the mathematical operation selected.
Menu to be displayed for the user:
MATH TOOL
1. Compute the power of a number
2. Compute the product of two numbers
Note: The user will enter the menu number corresponding to the mathematical operation desired, and NOT the type of operation text. For example, if the user wants to compute the power of a number, he/she would type 1 (not “Compute the power of a number”).
In addition to the main method, your program MUST contain three user-defined methods as follows:
1. Compute the power of a number
2. Compute the product of two numbers
3. Print the formatted result of the mathematical operation
When you write each of your methods, be sure to think about the following:
• What information does the main method need to provide to this method in order for it to work properly? For example, in order to calculate the power of a number, what information does the method need from main?
• What information does main expect to receive, if any, after this method executes? For example, when calculating the power of a number, what information does main expect to receive back from the method?
• Do NOT use any method already defined in Java such as Math.pow, Math.max, etc. • You MUST store any calculations in variables (i.e. do not perform calculations directly in your System.out.println statements) • You do NOT need to worry about formatting the decimal places on values you calculate • You may include additional methods if you want, but you MUST include at least these three methods (plus main).
Hi,
Here is the code for your assignment. I have added comments in the code for your reference.
The output and screenshot are at the end of this answer.
If you have any questions or need clarifications, leave me a comment.
If this answer helps you with your assignment, spare a moment and upvote this answer.
//File: MathCalc.java
import java.util.Scanner;
public class MathCalc {
//method to compute the power of a number1 raised to number2
private static int computePower(int num1, int num2) {
int result = 1;
for(int i = 1; i <= num2; i++) {
result *= num1;
}
return result;
}
//method to compute the product of number1 and number2
private static int computeProduct(int num1, int num2) {
return num1 * num2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//show the menu
System.out.println("1. Compute the power of a number");
System.out.println("2. Compute the product of two numbers");
System.out.print("Select your choice (1/2): ");
int menuChoice = input.nextInt();
//depending on user's choice, compute the math operation.
if(menuChoice == 1) {
System.out.print("To compute the power, Enter 2 numbers: ");
int num1 = input.nextInt();
int num2 = 0;
if(input.hasNextInt()) {
num2 = input.nextInt();
}
int result = computePower(num1, num2);
System.out.format("Power of %d and %d is %d%n", num1, num2,
result);
} else if (menuChoice == 2) {
System.out.print("To compute the product, Enter 2 numbers:
");
int num1 = input.nextInt();
int num2 = 0;
if(input.hasNextInt()) {
num2 = input.nextInt();
}
int product = computeProduct(num1, num2);
System.out.format("Product of %d and %d is %d%n", num1, num2,
product);
} else {
System.out.println("Invalid choice!");
}
}
}
//Output