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 right 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 right 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 anything you need
//-->

//declare the class Lab6
//-->

//declare the main method
//-->

// Declare Constant integers Trangle = 1, QUIT = 2


// Define scan object of the type Scanner class
//-->


// Create an integer variable named choice.
//-->


// 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 proram does the following:"
//-->

// "1. Print a Triangle:"
//-->
// "2. Quit"
//-->
// Read the value the user enters and store it in an integer variable
//-->

// Create a switch statement with as input for the 2 cases
switch(???)
{
case Trangle:
//define an int variable
//-->

// Print "Please input the triangle height:"
//-->

//scan the next integer and assign it to
//-->

// First for loop
// 1st ??? --> define an int variable and initialize it to 1
// 2nd ??? --> check if is less than equal to
// 3rd ??? --> increment variable by 1
for (???;???;???)
{
// Second for loop
// Let the inner loop run from j=1 up to and including i
for (???)
{
// The inner loop prints the single star
//-->
}
// Start a new line
//-->
}

// First for loop
// 1st ??? --> define an int variable and initialize it to size-1
// 2nd ??? --> check if is greater than or equal to 1
// 3rd ??? --> subtract variable by 1
for(???;???;???)
{
// Second for loop
// Let the inner loop run from j=0 up to i
for(???;???;???)
{
// The inner loop prints the single star
//-->

}
// start a new line
//-->

}
//terminate this case using break
//-->
case QUIT:
// Print "You choose to quit"
//-->

//terminate this case
//-->

default:
// Print "Please choose again"
//-->

}
}while(???);
//Remember to close the braces for the main method and class.
//-->
//-->

Sample output:

This proram does the following:
1. Print a Triangle:
2. Quit
1
Please input the triangle height:5
*
**
***
****
*****
****
***
**
*
This proram does the following:
1. Print a Triangle:
2. Quit
3
Please choose again
This proram does the following:
1. Print a Triangle:
2. Quit
1
Please input the triangle height:4
*
**
***
****
***
**
*
This proram does the following:
1. Print a Triangle:
2. Quit
2
You choose to quit
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.