Question

Summary In this lab, you add nested loops to a Java program provided. The program should...

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

  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 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.
  3. Execute the program. Observe your output.
  4. 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?
  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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++. Write a program that uses nested while loops or for statements to display Pattern A...
C++. Write a program that uses nested while loops or for statements to display Pattern A below, followed by an empty line and then another set of loops that displays Pattern B. Once again no setw implementation is allowed here and you must use appropriate control structures with proper indentation to generate these static patterns. Use meaningful variable identifier names, good prompting messages and appropriate comments for each loop segment as well as other sections in the body of your...
Using a for Loop visual c++ Summary In this lab the completed program should print the...
Using a for Loop visual c++ Summary In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Please use my template import java.util.Scanner; public class...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
VBA LOOPS The two macros in this homework should be assigned to separate buttons and should...
VBA LOOPS The two macros in this homework should be assigned to separate buttons and should be located on the same worksheet. When you are done, upload your saved workbook below. Be sure to write your code in a module in the workbook you will submit, not in your personal workbooks or sheets. To access the correct module, go to your “Visual Basic,” right-click “Microsoft Excel Objects” in the workbook you are submitting, and then click “Insert” and “Module.” Problem...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT