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
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();
}
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images
- USING JAVA: Write a program that analyzes an object falling for 10 seconds. It should contain main and two additional methods. One of the additional methods should return the distance an object falls in meters when passed the current second as an argument. See the formula needed below. The third method should convert meters to feet. You can look up the conversion factor needed online. The main method should use one loop to call the other methods and generate a table as shown below. The table should be displayed in formatted columns with decimals as shown.arrow_forwardJava Jiffy labarrow_forwardAny help with this first problem would be really helpful! Thank you so much!! 1. Write the following exercises using Java code. Add comments in each program to explain what your code is doing. A. Write a program that generates two random integers, both in the range 50 to 100, inclusive. Use the Math class. Print both integers and then display the positive difference between the two integers, but use a selection. Do not use the absolute value method of the Math class. B. Duke Shirts sells Java t-shirts for $24.95 each, but discounts are possible for quantities as follows: 1 or 2 shirts, no discount and total shipping is $10.003-5 shirts, discount is 10% and total shipping is $8.006-10 shirts, discount is 20% and total shipping is $5.0011 or more shirts, discount is 30% and shipping is free Write a Java program that prompts the user for the number of shirts required. The program should then print the extended price of the shirts, the shipping charges, and the total cost of the…arrow_forward
- Please help me! I have a lot of mini problems. I'm not sure how to start After #6, I have other problems. Those are : 7.) Write a method that reads a one-line sentence as input and then displays the following response: If the sentence ends with a question mark (?) and the input contains an even number of characters, display the word Yes. If the sentence ends with a question mark and the input contains an odd number of characters, display the word No. If the sentence ends with an exclamation point (!), display the word Wow. In all other cases, display the words You always say followed by the input string enclosed in quotes. Your output should all be on one line. Be sure to note that in the last case, your output must include quotation marks around the echoed input string. In all other cases, there are no quotes in the output. Your program does not have to check the input to see that the user has entered a legitimate sentence. Notes: This code requires a three-way selection statement and…arrow_forwardbasic java please Write a void method named emptyBox()that accepts two parameters: the first parameter should be the height of a box to be drawn, and the second parameter will be the width. The method should then draw an empty box of the correct size. For example, the call emptyBox(8, 5) should produce the following output: ***** * * * * * * * * * * * * ***** The width of the box is 5. The middle lines have 3 spaces in the middle. The height of the box is 8. The middle columns have 6 spacesarrow_forwardComplete the following program to implement the user interface of the preceding exercise. For simplicity, only the units cm, m, and in are supported. Hint: The value of factor1 or factor2 should be the conversion factor from the selected unit to cm. Ex: If the selected unit is in, factor1 is 2.54 because 1 in = 2.54 cm."in java"arrow_forward
- Problem: Min Method (Return the Smaller Number) Create a method GetMin(int a, int b), which returns the smaller of two numbers. Write a program, which takes as input three numbers and prints the smallest of them. Use the method GetMin(…), which you have already created. Sample Input and Output Input Output Input Output 123 1 -100-101-102 -102 Hints and Guidelines Define a method GetMin(int a, int b) and implement it, after which invoke it from the main program as shown below. In order to find the minimum of three numbers, first, find the minimum of the first two and then the minimum of the result and the third number: var min = GetMin(GetMin(num1, num2), num3);References: Programming Basics with C#: Comprehensive Introduction to Programming with C#: Book + Video Lessons by Dr. Svetlin Ivanov NakovPaperbackarrow_forwardInstructor note: This lab is part of the assignment for this chapter. HINT: 1). The decision means how many times to flip the coin. 2). Use a random integer variable to indicate the head or tail in each toss. Java Define a method named coinFlip that takes a Random object and returns "Heads" or "Tails" according to a random value 1 or 0. Assume the value 1 represents "Heads" and 0 represents "Tails". Then, write a main program that reads the desired number of coin flips as an input, calls method coinFlip() repeatedly according to the number of coin flips, and outputs the results. Assume the input is a value greater than 0. Ex: If the random object is created with a seed value of 2 and the input is: 3 the output is: Heads Tails Heads Note: For testing purposes, a Random object is created in the main() method using a pseudo-random number generator with a fixed seed value. The program uses a seed value of 2 during development, but when submitted, a different seed value may be used…arrow_forwardpython-Exercise 1 Write a python program to display the greater number between 2 numbers. The numbers are given by user. Input : 17 -6 Output: The greatest number is 17 2.Use a class and add a method called greatestNumberarrow_forward
- In this assignment you will demonstrate your knowledge of debugging by fixing the errors you find in the program below. Fix the code, and paste it in this document, along with the list of the problems you fixed.This example allows the user to display the string for the day of the week. For example, if the user passed the integer 1, the method will return the string Sunday. If the user passed the integer 2, the method will return Monday. This code has both syntax errors and logic errors. Hint: There are two logic errors to find and fix (in addition to the syntax errors).Inport daysAndDates.DaysOfWeek;public class TestDaysOfWeek {public static void main(String[] args) {System.out.println("Days Of week: ");for (int i = 0;i < 8;i++) {System.out.println("Number: " + i + "\tDay Of Week: " + DaysOfWeek.DayOfWeekStr(i) )}}}package daysAndDatespublic class DaysOfWeek {public static String DayOfWeekStr(int NumberOfDay) {String dayStr = ""switch (NumberOfDay) {case 1:dayStr =…arrow_forwardIn the following code stream, identify if there are any hazards. Crisply describe if it is possible to get around the hazards, and what the resulting steps will be. lw r2, 0(r1) add r3, r2, r3 sub r1, r1, r4arrow_forwardProgram in C++ & Visual Studio not Studio Code Everyone has played Yahtzee... Right? There are so many better dice games, but my family likes this one the best. A YouTube video that describes Yahtzee can be found here. The basic rules are as follows: At the start of your turn you roll five normal everyday six sided dice. In the course of your turn you would choose to reroll any of the dice up to two more times. We are more concerned in this problem with the dice at the completion of your turn. This website gives a great rundown of the probabilities of Yahtzee that you will need. NOTE: You may ONLY use a set and map data structure to complete the solution to this problem Let’s see what results the dice give us using the following data structures and process: Prompt the user to enter five valid numbers (Range: One to six inclusive on both ends) Each time the user enters a valid number, “place” it into a map of integersCreate and use a set of integers with your data to…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education