84.8k views
0 votes
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:
Write the nested loops to control the number of rows and the number of columns that make up the letter E.
In the loop body, use a nested if statement to decide when to print an asterisk and when to print a space. The output statements have been written, but you must decide when and where to use them.
Execute the program. Observe your output.
Modify the program to change the number of rows from five to seven and the number of columns from three to five.
What does the letter E look like now?

2 Answers

2 votes

Final answer:

To create the letter E using asterisks, use nested loops in your Java program. Modify the program by changing the number of rows and columns for a different representation of the letter E.

Step-by-step explanation:

To create the letter E using asterisks, you will need to use nested loops in your Java program. The outer loop will control the number of rows, and the inner loop will control the number of columns. Within the loop body, you can use a nested if statement to decide when to print an asterisk and when to print a space. Here's an example of how the nested loops can be implemented:

for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 3; col++) {
if (col == 1 || (row == 1 || row == 3 || row == 5)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}

When you modify the program to change the number of rows to seven and the number of columns to five, the letter E will look different. It will have seven rows and five columns, resulting in a larger representation of the letter E.

User Oliver Baumann
by
6.1k points
2 votes

Answer:

/Create a class LetterE.

public class LetterE

{

//Define the main() function.

public static void main(String args[])

{

//Declare the variables.

final int NUM_ACROSS = 3;

final int NUM_DOWN = 5;

int row;

int column;

//Begin the for loop.

for(int k=1; k<=NUM_DOWN; k++)

{

//Begin the for loop.

for(int l=1; l<=NUM_ACROSS; l++)

//End of for loop.

//Statement for the next line.

System.out.println();

}//End of for loop.

//End of the workdone.

System.exit(0);

}//End of the main() function.

}//End of the LetterE class.

Step-by-step explanation:

User Ybbest
by
6.2k points