Write a menu driven program to do each of the following. Continue to prompt the
ID: 3587832 • Letter: W
Question
Write a menu driven program to do each of the following. Continue to prompt the user with the menu until option 11 – Quit is selected. Display an error message if the user makes an invalid selection.
PART I. SINGLE LOOPS Use the appropriate loop to do the following:
1. Write a WHILE LOOP that will print one row of 15 asterisks to the screen.
2. Write a FOR LOOP that will print one row of 15 asterisks to the screen.
3. Display the odd numbers between 1 and 50.
4. Write a code segment that will prompt the user to enter a positive number, max. Verify the number is positive before continuing. Display all integers from 1 to max.
5. Write a code segment that will prompt the user to enter a negative number, min. Verify the number is negative before continuing. Display all integers from 1 down to min.
6. Write a code segment that will prompt the user to enter a positive number, max. Sum all the even numbers from 2 to max and display the total.
7. Write a code segment that will prompt the user for a positive number, max, and display a table of integers from 1 to max. Display only 5 integers in each row. For example, the user enters a max of 12.
1 2 3 4 5
6 7 8 9 10
11 12
Hint: Find the pattern as to when a newline needs to occur. (Use the modulus (%) operator)
Hint2: Use printf( ) to format the numbers into columns
PART II. NESTED for LOOPS LAB
For problems 8-10, only one character (‘*’ or ‘ ‘ ) may be output at a time
8. Print a sequence of asterisk characters in the following configuration, continuing for LINES number of asterisks. LINES is a named constant with the value of 7.
*
*
*
*
*
*
*
9-10 Write nested for-loops that will display each of the following patterns:
9. ********** 10. **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
11. Quit
Explanation / Answer
import java.util.Scanner;
public class LoopTest {
public void function1() {
System.out.println("Printing * using while loop");
int i = 0;
while (i < 15) {
System.out.print("*");
i++;
}
}
public void function2() {
System.out.println("Printing * using for loop");
for (int i = 0; i < 15; i++) {
System.out.print("*");
}
}
public void function3() {
int n = 50;
System.out.println("Printing Odd numbers between 1 and " + n);
for (int i = 1; i <= n; i++) {
// if the number is not divisible by 2 then it is odd
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
public void function4(int max) {
if (max > 0) {
for (int i = 1; i <= max; i++) {
System.out.print(i+" ");
}
} else {
System.out.println("Number is not positive");
}
}
public void function5(int min) {
if (min < 0) {
for (int i = 1; i >= min; i--) {
System.out.print(i+" ");
}
} else {
System.out.println("Number is not negative");
}
}
public void function6(int max) {
int sum = 0;
if (max > 0) {
for (int i = 2; i <= max; i++) {
sum += i;
}
System.out.println("Sum = " + sum);
} else {
System.out.println("Number is not positive");
}
}
public void function7(int max) {
if (max > 0) {
for (int i = 1; i <= max; i++) {
if ((i - 1) % 5 == 0)
System.out.println();
System.out.print(i+" ");
}
} else {
System.out.println("Number is not positive");
}
}
public void function8() {
int lines = 7;
for (int i = 1; i <= lines; i++) {
System.out.println("*");
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
}
}
public void function9() {
int n = 10;
for( int i = 1; i <= n; i++ )
{
for( int j = 1; j <= n; j++ )
{
if(i<=j)
System.out.print( "*" );
}
System.out.println();
}
}
public void function10() {
int n = 10;
for( int i = 1; i <= n; i++ )
{
for( int j = 1; j <= n; j++ )
{
if(i<j)
System.out.print( "*" );
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
String choice = null;
LoopTest test = new LoopTest();
Scanner scan = new Scanner(System.in);
do {
System.out.println(" Main Menu: Enter your choice:: enter 11 to quit");
choice = scan.nextLine();
switch (choice) {
case "1":
test.function1();
break;
case "2":
test.function2();
break;
case "3":
test.function3();
break;
case "4":
System.out.println("Enter a positive number");
int max = Integer.parseInt(scan.nextLine());
test.function4(max);
break;
case "5":
System.out.println("Enter a negative number");
int min = Integer.parseInt(scan.nextLine());
test.function5(min);
break;
case "6":
System.out.println("Enter a positive number");
max = Integer.parseInt(scan.nextLine());
test.function6(max);
break;
case "7":
System.out.println("Enter a positive number");
max = Integer.parseInt(scan.nextLine());
test.function7(max);
break;
case "8":
test.function8();
break;
case "9":
test.function9();
break;
case "10":
test.function10();
break;
}
} while (!choice.equals("11"));
scan.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.