Summary
In this lab, you add nested loops to a Java program provided. The program should print the letter E. The letter E is printed using asterisks, three across and five down. Note that this program uses System.out.print("*"); to print an asterisk without a new line.
Instructions
// LetterE.java - This program prints the letter E with 3
asterisks
// across and 5 asterisks down.
// Input: None.
// Output: Prints the letter E.
public class LetterE
{
public static void main(String args[])
{
final int NUM_ACROSS = 3; // Number of asterisks to print
across.
final int NUM_DOWN = 5; // Number of asterisks to print down.
int row; // Loop control for row number.
int column; // Loop control for column number.
// This is the work done in the detailLoop() method
// Write a loop to control the number of rows.
// Write a loop to control the number of columns
// Decide when to print an asterisk in every column.
System.out.print(" ");
// Decide when to print asterisk in column 1.
System.out.print(" ");
// Decide when to print a space instead of an asterisk.
System.out.print(" ");
// Figure out where to place this statement that prints a
newline.
System.out.println();
// This is the work done in the endOfJob() method
System.exit(0);
} // End of main() method.
} // End of LetterE class.
Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.
CODE
// LetterE.java - This program prints the letter E with 3
asterisks
// across and 5 asterisks down.
// Input: None.
// Output: Prints the letter E.
public class LetterE
{
public static void main(String args[])
{
// Number of asterisks to print
across.
final int NUM_ACROSS = 3;
// Number of asterisks to print
down.
final int NUM_DOWN = 5;
// Loop control for row
number.
int row;
// Loop control for column
number.
int column;
// Write a loop to control the
number of rows.
for(row = 0; row < NUM_DOWN;
row++)
{
// Write a loop
to control the number of columns
for(column = 0;
column < NUM_ACROSS; column++)
{
/*
Number of rows are 5 and
columns are 3
rows are 0 - 4 and columns
are 0 - 2
first row will be 0 and last
row will be 4
first column will be 0 and
last column will be 2
we have to print * in every
column for first, third and fifth row
ie. 0, 2, 4 rows which are
even rows
we have to print * in column
1 for second and fourth row
ie. 1, 3 rows and column 0
which are odd rows
*/
// Decide when to print an asterisk in every
column.
//if row is an even number
if(row % 2 == 0)
{
System.out.print("*");
}
// Decide when to print asterisk in column
1.
//if row is odd and column is 0
else if(row % 2 == 1 && column ==
0)
{
System.out.print("*");
}
// Decide when to print a space instead of an
asterisk.
//if row is odd and column is not 0
else
{
System.out.print(" ");
}
}
// Figure out
where to place this statement that prints a newline.
//after printing
each row
System.out.println();
}
//exiting program
System.exit(0);
} // End of main() method.
} // End of LetterE class
OUTPUT
CODE SCREEN SHOT
Get Answers For Free
Most questions answered within 1 hours.