For code given below: 1. write the nested loops to control the number of rows an
ID: 3814788 • Letter: F
Question
For code given below:
1. write the nested loops to control the number of rows and the number of columns that make up the letter E
2. in the 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
3. compile and execute.
4. modify program to change the number of rows from five to seven and the number of columns from 3 to 5
// 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.
Explanation / Answer
// LetterE.java - This program prints the letter E with 5 asterisks
// across and 7 asterisks down.
// Input: None.
// Output: Prints the letter E.
public class LetterE
{
public static void main(String args[])
{
final int NUM_ACROSS = 5; // Number of asterisks to print across.
final int NUM_DOWN = 7; // 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.
for(row=0;row<NUM_DOWN;row++)
{
if(row % 2 == 0)
{
for(column=0;column<NUM_ACROSS;column++)
{
System.out.print("*");
// Decide when to print asterisk in column 1.
}
}
else
{
System.out.print("*");
// Decide when to print a space instead of an asterisk.
}
System.out.println();
}
// Figure out where to place this statement that prints a newline.
// This is the work done in the endOfJob() method
System.exit(0);
} // End of main() method.
} // End of LetterE class.
/*
output:
*****
*
*****
*
*****
*
*****
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.