Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%

Hi, would you be able to check my code and see if it meet the intended requirements, if not, please assist and correct any error in it for me. Below is the code and the requirements. Thank you.

#Spefication/Requirement

1. The program consists of a single class called Loops
2. Loops has a main method
3. Loops has the following static methods
a. calculateSum – the input to this method is an integer. The methods returns
the sum 1+2+3+…+n. Use a for loop to calculate the sum
b. calculateProduct – the input to this method is an integer. The method
returns 1*2*3*…*n. Use a while loop to calculate the product.
c. calculatePower – the input to this method is a double value, say x, and an
integer value – say n. The method calculates and returns xy
. Use a for loop to do
the calculation.
d. menu – this method requires no input parameters. It provides the user the
following choices: 
1. Calculate sum 1+2+3+4
2. Calculate product 1*2*3*…*n
3. CalculateXToYPower
4. QUIT
The method returns the user’s selection.

4. In the main method, you should implement the following:
main()
{
 display menu and get user’s choice (1,2,3,4)
 LOOP (Use a while or do-while)


 if the choice is 1 ask the user to enter an integer – say n, then read the input from the user
 call calculateSum to get sum 1+2+3+…+n
 print n and the sum


 if the choice is 2 ask the user to enter an integer – say n, then read the input from the user
 call calculateProduct to get the product 1*2*3*…*n
 print n and the product


 if the choice is 3 ask the user to enter a double, say x, then read the input
 ask the user to enter an integer – say n, then read the input
 call calculatePower to get xn

print the answer

 if the choice is 4, terminate the program (You can say System.exit(0);
 display menu again and get new choice
END LOOP 

#Code

import java.util.Scanner;

public class Loops {

public static int calculateSum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

public static double calculateProduct(int n) {
double product = 1;
int i = 1;
while (i <= n) {
product *= i;
i++;
}
return product;
}

public static double calculatePower(double x, int n) {
double result = 1;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}

public static int menu() {
System.out.println("\n1. Calculate sum 1+2+3+...+n");
System.out.println("2. Calculate product 1*2*3*...*n");
System.out.println("3. Calculate X to the power of Y");
System.out.println("4. QUIT");
System.out.print("Enter your choice: ");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

public static void main(String[] args) {
int choice = menu();
while (choice != 4) {
Scanner scanner = new Scanner(System.in);
if (choice == 1) {
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
System.out.println("Sum: " + calculateSum(n));
} else if (choice == 2) {
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
System.out.println("Product: " + calculateProduct(n));
} else if (choice == 3) {
System.out.print("Enter a double: ");
double x = scanner.nextDouble();
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
System.out.println("Power: " + calculatePower(x, n));
}
choice = menu();
}
}
}
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education