Description
This lab is for practicing the nested for loops and switch statement.
Use the following Coding Guidelines:
• When declaring a variable, you usually want to initialize it.
• 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.
Assignments Documentation:
At the beginning of each programming assignment you must have a comment block with the
following information:
/
*———————————————————————-
—–
// AUTHOR: (Put your name here)
// FILENAME: Lab6.java
// SPECIFICATION: This program is for practicing nested loops.
// It prints out a certain size of pyramid and
triangle of stars
// INSTRUCTIONS: Read the following code skeleton and add your own
code
// according to the comments. Ask your TA or your class-
// mates for help and/or clarification. When you see
// //–> that is where you need to add code.
// LAB LETTER: (Put your lab letter)
//———————————————————————
—-*/Getting Started
Create a class called Lab6. Use the same setup for setting up your class and main method as
you did for the previous assignments. Be sure to name your file Lab6.java.
Hints
Please replace //–> with the correct program to finish the task according to the
corresponding comment.
Please replace ??? with the correct program to enable the program to run as required.
/
*———————————————————————-
—–
// AUTHOR: (Put your name here)
// FILENAME: Lab6.java
// SPECIFICATION: This program is for practicing nested loops.
// It prints out a certain size of pyramid and
triangle of stars
// INSTRUCTIONS: Read the following code skeleton and add your own
code
// according to the comments. Ask your TA or your class-
// mates for help and/or clarification. When you see
// //–> that is where you need to add code.
// LAB LETTER: (Put your lab letter)
//———————————————————————
—-*/
//import Scanner class
import java.util.Scanner;
//declare the class Lab6
public class Lab5
{
//declare the main method
public static void main(String[] args)
{
// Declare Constant integers PYRAMID = 1, TRIANGLE = 2, QUIT = 3
final int PYRAMID = 1;
final int TRIANGLE = 2;
final int QUIT = 3;
// Define scan object of the type Scanner class
//–>
// Create an integer variable named choice.
//—>
//define an int variable
//–>
// Create a do-while loop that exits only when the user chooses quit
(choice = QUIT)
// Have the do-statement here
??
{
// Print the following options:
// “This proGram does the following:”
//–>
// “1. Print a PYRAMID:”
//—>
// “2. Print a TRIANGLE:”
//–>
// “3. Quit”
//–>
// Read the value the user enters and store it in an integer variable
//–>
// Create a switch statement with
switch(???)
{
case PYRAMID:
// Print “Please input the Pyramid height:
//–>
//scan the next integer and assign it to
//–>
// outer loop to handle number of rows
// First for loop
// 1st ??? –> define an int variable and initialize it to 0
// 2nd ??? –> check if is less than
// 3rd ??? –> increment variable by 1
for (???;???;???)
{
// inner loop to handle number of columns
// Second for loop
// Let the inner loop run from j=0 up to and including i
for (???;???;???)
{
// The inner loop prints the stars
//–>
}
// Start a new line
//–>
}
//terminate this case using break
//—>
case TRIANGLE:
// Print “Please input the Triangle height:
//–>
//scan the next integer and assign it to
//–>
//Calculate the number of spaces required and initialize k with
that.
//–>
// outer loop to handle number of rows
// First for loop
// 1st ??? –> define an int variable and initialize it to 0
// 2nd ??? –> check if is less than to
// 3rd ??? –> increment variable by 1
for (???;???;???)
{
// This inner for loop handles the number of spaces
// Let the inner loop run from j=0 up to k (excluding k)
for (???;???;???)
{
// The inner loop prints the spaces
//–>
}
//Decrementing spaces after each loop
//This inner for loop is for printing stars
// Let the inner loop run from j=0 up to and including i
for (???;???;???)
{
// The inner loop prints the stars
//–>
}
//
// Start a new line
//–>
case QUIT:
// Print “Qutting the program as you requested…”
//–>
//terminate this case
//–>
default:
// Print “Please choose again”
//–>
}
}while(???);
//Close the scanner object.
}
}
Sample output:
Please select an option:
1. PYRAMID
2. TRIANGLE
3. QUIT
1
Please enter the size:
5
*
* *
* * *
* * * *
* * * * *
Please select an option:
1. PYRAMID
2. TRIANGLE
3. QUIT
2
Please enter the size:
6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Please select an option:
1. PYRAMID
2. TRIANGLE
3. QUIT
3
Qutting the program as you requested…