Question

In: Computer Science

Java Programming: Write a program that allows the user to compute the power of a number...

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).

Solutions

Expert Solution

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


Related Solutions

IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT