Question
When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive.
- When declaring a variable, you usually want to initialize it.
- Remember you cannot initialize a number with a string.
- Remember variable names are case sensitive.
- Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
- Use white space to make your program more readable.
- Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs.
Problem Description: Displaying menu options and performing operations based on user choice
For this Lab your program must perform three different operations based on a users input. To do this, you must show the user the menu, ask the user for their choice, perform the appropriate computation, and then repeat until the user exits. The menu is as follows:
Please choose from following menu: 1) Print all integer numbers between two given integers. 2) Display a right triangular pattern of stars. 3) Quit.
Step 1: Getting Started
Create a class called Lab5. Use the same setup for setting up your class and main method as you did in previous labs and assignments. Be sure to name your file Lab5.java.
Assignment Documentation:
At the beginning of each programming assignment you must have a comment block with the following information:
// ------------------------------------------------------------ // AUTHOR: your name // FILENAME: title of the source file // SPECIFICATION: description of the program // FOR: CSE 110- Lab #5 // TIME SPENT: how long it took you to complete the assignment //-------------------------------------------------------------
Step 2: Beginning the loop and declaring variables
The first part of the problem is to show the user the menu, collect the user's menu choice as an input, and repeat it until the user selects the exit option. For this, we need to declare a variable named choice to store the user choice.
int choice = 0;
Next, we must select the appropriate loop for the main body of the program. In this case, since we know that the program should execute at least once, a do-while loop is most appropriate. Inside the loop, print the menu using System.out.println statements and then request the menu option from the user. A skeleton do-while loop follows. Remember to add the Scanner statement that requests the choice from the user.
do{ //Please choose from following menu: // 1) Print all integer numbers between two given integers. // 2) Display a right triangular pattern of stars. // 3) Quit. // Use scanner to collect user input } while(/*termination condition*/);
Remember every loop should have a termination condition. Here the loop should run until user chooses option 4. So the termination condition would be (choice != 3)
Step 3: Performing operations
Based on the choice entered by the user, we need to do some arithmetic operations. Inside the do-whileloop, but after the menu, initialize a switch-case statement to perform the operations. The switch-casestatement hinges on the choice variable, which should now have an integer value. An example switch-casestatement follows.
switch(choice) { case 1:break; case 2: break; case 3: break; default: break; }
Step 4: CASE 1 - Print Integers
If the user chooses option 1, they wish to print out a list of the integers from start to end. Thus, we must request these numbers from the user. First, we declare two new integers, one for start, one for end.
int start = 0, end = 0;
Ask the user to enter a starting number (start) using System.out.println and then use a Scanner object to get the value. Then do this again for end.
To perform the operation, we need another variable that will count from start to end in a loop. We will ad this value to count each time the body of the loop executes.
For that, declare and initialize a variable named count.
int count;
Begin a for loop using count with a condition that will cause it to repeat until its value is greater than end. Inside the loop we will print out the current value of count.
for (count = start; count <= end; count++) { //}
Step 5: CASE 2 - Nested for Loops
This will print a triangle made of stars (*).
Ask the user to input the height of the triangle. Write a nested for-loop to print out a right triangle of stars. Here is a pseudo-code (algorithm) to help you to print the triangle of stars.
get height of triangle from user initialize numberOfStars to 0 for rowCount = 1 to height numStars += 1 for starCount = 1 to numStars print a star print a newline character
You will need to declare variables for the height, and to keep count of the rows and to keep track of the count of stars in each row.
You will need to ask the user to enter height using System.out.println and a use a Scanner object to scan the value that the user enters and assign it to height.
The outer loop has to repeat height times (once for each row), and the inner loop has to print the stars for each row.
After executing inner loop print a new line so that the next group of stars will be printed on a new line.
Your outer loop will look like this:
for (rowCount = 1; rowCount <= height; rowCount ++) { //Inner Loop //Print }
The inner loop prints the correct number of stars on each line (row) - one star for the first iteration, two stars for the second iteration and so on until you print the last line which has height stars. Notice that the number of stars to print on a row is the same as the rowCount for that row - in other words, when rowCount is 1, we will print 1 star, and when rowCount is 2 we will print 2 stars, and so on.
Your outer loop will look like this:
for(starCount = 1; starCount <= rowCount; starCount ++){ //}
Sample Output
Below is an example of what your output should roughly look like when this lab is completed, with user input in red.
Sample Run 1:
Please choose from following menu 1) Print all integer numbers between two given integers. 2) Generate a right triangular pattern of stars. 3) Quit. 1 Enter the start number: 1 Enter the end number: 4 1 2 3 4 Please choose from following menu 1) Print all integer numbers between two given integers. 2) Generate a right triangular pattern of stars. 3) Quit. 2 Enter the height: 5 * ** *** **** ***** Please choose from following menu 1) Print all integer numbers between two given integers. 2) Generate a right triangular pattern of stars. 3) Quit. 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started