JAVA HELP : MODIFY THE CODE BELOW TO THE FOLLOWING REQUIREMENTS : 1. allow the u
ID: 3717700 • Letter: J
Question
JAVA HELP : MODIFY THE CODE BELOW TO THE FOLLOWING REQUIREMENTS :
1. allow the user to select a menu option that calls a method that will run a counting program. This counting program will also use a menu.
2.That menu will allow the user to display a series of numbers based on user input. The user will have two choices of how to count.
3.The first choice will be to count from a starting number, by an interval, up to some maximum value. This will require the user to enter a starting number, an interval, and a stop by number.
4.The second choice will be to count from a starting number by an interval some number of times. This will require the user to enter a starting number, an interval, and a maximum count. Do not display a number that is either larger than the stopping number or is not in the series of interval numbers. Validate that the series of numbers is at least two numbers long.
For example, do they want to count by 2s, 7s, ?? etc. Display the warning "The ending number needs to be at least one counting interval lager than the starting number!" and do not display any numbers if the ending number is not at least 1 interval larger than the starting number. Insert a loop that will allow the user to repeat the program as many times as they wish asking each time if the user would like to repeat the program. When they finish the counting program, return to the main menu. Use menus when giving the user a choice, and validate their choice before executing that choice. Exit all programs using a menu choice.
MODIFY THIS CODE :
public class MenuChoices
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
String menuChoice = "";
do
{
System.out.println("A) choice 1");
System.out.println("B) choice 2");
System.out.println("Q) Quit");
System.out.println("Please select a menu item.");
do
{
System.out.println("Enter "A", "B", or "Q" ");
menuChoice = keyboard.nextLine().toLowerCase().trim();
} while (!menuChoice.equals("a") && !menuChoice.equals("b") && !menuChoice.equals("q"));
switch (menuChoice)
{
case "a":
moduleOne();
break;
case "b":
moduleTwo();
break;
default:
System.out.println("Good-bye!!!");
break;
}
System.out.println("An excilent choice!");
} while (!menuChoice.equals("q"));
}
private static void moduleTwo()
{
System.out.println("You are in module two!");
}
private static void moduleOne()
{
System.out.println("You are in module one!");
}
}
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
public class MenuChoices
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
String menuChoice = "";
do
{
System.out.println("A) choice 1");
System.out.println("B) choice 2");
System.out.println("Q) Quit");
System.out.println("Please select a menu item.");
do
{
System.out.println("Enter "A", "B", or "Q" ");
menuChoice = keyboard.nextLine().toLowerCase().trim();
} while (!menuChoice.equals("a") && !menuChoice.equals("b") && !menuChoice.equals("q"));
switch (menuChoice)
{
case "a":
moduleOne();
break;
case "b":
moduleTwo();
break;
default:
System.out.println("Good-bye!!!");
break;
}
System.out.println("An excilent choice!");
} while (!menuChoice.equals("q"));
}
private static void moduleTwo()
{
Scanner keyboard = new Scanner(System.in);
int start, interval, times;
System.out.print("Enter the starting number: ");
start = keyboard.nextInt();
System.out.print("Enter the interval (e.g. 2 or 7): ");
interval = keyboard.nextInt();
System.out.print("How many times do you want to count? ");
times = keyboard.nextInt();
if(times < 2)
System.out.println("The ending number needs to be at least one counting interval lager than the starting number!");
else
{
int num = start;
for(int i = 1; i <= times; i++)
{
System.out.print(num + " ");
num = num + interval;
}
System.out.println();
}
}
private static void moduleOne()
{
Scanner keyboard = new Scanner(System.in);
int start, interval, maximum;
System.out.print("Enter the starting number: ");
start = keyboard.nextInt();
System.out.print("Enter the interval (e.g. 2 or 7): ");
interval = keyboard.nextInt();
System.out.print("What is the maximum value you want to go to? ");
maximum = keyboard.nextInt();
if(start+interval > maximum)
System.out.println("The ending number needs to be at least one counting interval lager than the starting number!");
else
{
for(int i = start; i <= maximum; i = i + interval)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}
output
------
A) choice 1
B) choice 2
Q) Quit
Please select a menu item.
Enter "A", "B", or "Q"
a
Enter the starting number: 3
Enter the interval (e.g. 2 or 7): 5
What is the maximum value you want to go to? 25
3 8 13 18 23
An excilent choice!
A) choice 1
B) choice 2
Q) Quit
Please select a menu item.
Enter "A", "B", or "Q"
b
Enter the starting number: 4
Enter the interval (e.g. 2 or 7): 7
How many times do you want to count? 1
The ending number needs to be at least one counting interval lager than the starting number!
An excilent choice!
A) choice 1
B) choice 2
Q) Quit
Please select a menu item.
Enter "A", "B", or "Q"
b
Enter the starting number: 4
Enter the interval (e.g. 2 or 7): 7
How many times do you want to count? 10
4 11 18 25 32 39 46 53 60 67
An excilent choice!
A) choice 1
B) choice 2
Q) Quit
Please select a menu item.
Enter "A", "B", or "Q"
q
Good-bye!!!
An excilent choice!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.