0% found this document useful (0 votes)
3 views2 pages

Java Menu-Driven Pattern Generator

This Java code defines a menu-driven program that prints different patterns based on the user's input selection. It uses a do-while loop with a Scanner to accept user input. Based on the input choice, one of four cases is executed to print a pattern using nested for loops: Case 1 alternates between * and #; Case 2 prints * everywhere except in the center; Case 3 alternates between 1 and 0; and Case 4 exits the program.

Uploaded by

sfsicseclass10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Menu-Driven Pattern Generator

This Java code defines a menu-driven program that prints different patterns based on the user's input selection. It uses a do-while loop with a Scanner to accept user input. Based on the input choice, one of four cases is executed to print a pattern using nested for loops: Case 1 alternates between * and #; Case 2 prints * everywhere except in the center; Case 3 alternates between 1 and 0; and Case 4 exits the program.

Uploaded by

sfsicseclass10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

Scanner;
class P2
{
public static void main(String args[])
{
Scanner scan=new Scanner([Link]);
int ch;
do
{
[Link]("**********MENU**********");
[Link]("Available choices are:");
[Link]("1. Pattern 1");
[Link]("2. Pattern 2");
[Link]("3. Pattern 3");
[Link]("[Link]");
[Link]("Enter your choice:-");
ch=[Link]();
switch (ch)
{
case 1:
for(int i=1;i<=5;i++)
{
for(int j=i;j<=5;j++)
{
if(i%2==0)
{
[Link]("*");
}
else
{
[Link]("#");
}
}
[Link]();
}
break;
case 2:
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==4 && j==4)
{
[Link]("4");
}
else
{
[Link]("*");
}
}
[Link]();
}
break;
case 3:
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==0)
{
[Link]("1");
}
else
{
[Link]("0");
}
}
[Link]();
}
break;
case 4:
[Link]("***EXIT***");
break;
default:
[Link]("INVALID");
}
}while(ch!=4);
}
}

Common questions

Powered by AI

Nested loops are utilized to construct rows and columns of characters dynamically for each pattern. The outer loop iterates through rows, while the inner loop manages character placement within each row. This structured approach enables fine control over output format, such as alignment and specific condition-based character changes, essential for generating the patterns effectively .

Pattern 1 generates output where the character '#' is printed for rows with an odd index and '*' for rows with an even index. For each row, the number of printed characters decreases with each successive row beginning from 5 down to 1. This is controlled by the conditional expression `if(i%2==0)` which checks if the row index is even or odd .

The menu is structured to present the user with four options, each represented numerically. It facilitates user interaction by allowing users to select among predefined patterns or exit the program. This design uses a series of print statements to present information and a switch-case structure to execute specific actions based on the user's input .

The switch-case structure manages flow control by directing execution based on the user's menu choice. Each case corresponds to a distinct action, allowing the program to execute the specified pattern routine or handle incorrect inputs. It simplifies the menu's complexity by clearly mapping inputs to outputs, thus enhancing manageability and readability .

In each case section, loops define the basic structure of output (e.g., number of lines, alignment), while conditions within these loops determine specific character placements. For instance, Pattern 1 uses conditions to alternate characters by row parity, and Pattern 2 uses conditions to replace a common character with '4' at a specific location, illustrating a combination of iterative and conditional logic to form complex outputs .

The program allows termination by implementing a case for the value '4' in the switch-case structure. When selected, it prints the message '***EXIT***' and completes the current iteration of the do-while loop, allowing the loop's condition `while(ch!=4)` to terminate execution since 'ch' equals 4 .

The 'do-while' loop in the Java program allows the menu to be displayed repeatedly until the user decides to exit by choosing option 4. This ensures that after every valid or invalid choice, the menu reappears, prompting for another action until the exit condition is met .

Pattern 2 constructs a 5x5 grid where each position typically displays '*'. The distinctive feature is at position (4,4) (considering a 1-based index), where the character is '4' instead of '*. This uniqueness is achieved by the condition `if(i==4 && j==4)` which checks for this specific grid location .

In Pattern 3, columns within each row print '0' or '1' based on whether the column index 'j' is odd or even, controlled by the condition `if(j%2==0)`. Each successive row increases in length, starting with one character in row 1, up to five in row 5, leading to a triangle-like shape .

Error handling in the menu system is achieved by using a 'default' case in the switch statement. When an invalid choice is entered, this case triggers, printing 'INVALID'. While basic, this mechanism serves as error feedback to notify users of inputs outside the available menu options without halting program execution .

You might also like