Hw 4 Debra
Tue Oct 15 2024 17:04:14 GMT+0000 (Coordinated Universal Time)
Saved by @shivamp
package patterns; import java.util.Scanner; public class Patterns { // All of the code for the main method is complete. No modifications are necessary. public static void main(String[] args) { // call method to print design patterns printPatterns(); // call method to print horizontal bars printHorizontalBars(); // call method to print vertical bars printVerticalBars(); } // end main // Complete the code for the following method according to the project specifications. public static void printPatterns() { System.out.println("printPatterns() method called...."); // nested for loop to print the following pattern goes below: // # # # # # # # # // # # # # # # # // # # # # # # // # # # # # // # # # # // # # # // # # // # // NOTE: You can copy the following nested-for loop structure and change it based on // each of the four patterns that you need to draw Remove comments before coding for (int row = 1; row <= 8; row++) // add code to complete the for loop { for ( int col = 1; col<= 8; col++) // add code to complete the for loop { if (row + col == 9 || row == col || row = 1 || row 8) // add condition to if-statement { // You can swap the following print statements based on the logic you wish to use System.out.print("#"); // print space in pattern (you can swap with print statement below to make logic easier } else System.out.print(" "); // print character for pattern System.out.println(); // move to next line of the pattern been printed } */ // Tested for loop to print the following pattern goes below: // # # # # # # # # // # # # # # # # // # # # # # # // # # # # # // # # # # // # # # // # # // # for (int row = 1; row <= 8; row++) // add code to complete the for loop { for ( int col = 1; col<= 8; col++) // add code to complete the for loop { if (row + col <= 9) // add condition to if-statement { // You can swap the following print statements based on the logic you wish to use System.out.print("#"); // print space in pattern (you can swap with print statement below to make logic easier } else System.out.print(" "); // print character for pattern System.out.println(); // move to next line of the pattern been printed System.out.println // nested for loop to print the following pattern goes below: //(HINT: what do the #'s on the top and bottom have in common. What is the // relationship between row and column values...plug in numbers for their location // to help find the answers to the logic you need to use..think in terms of the // row and column value) // // # # # # # # # # // # # // # # // # # // # # // # # // # # // # # # # # # # # for (int row = 1; row <= 8; row++) // add code to complete the for loop { for ( int col = 1; col<= 8; col++) // add code to complete the for loop { if (row + col == 9 || row == col || row = 1 || row 8) // add condition to if-statement { // You can swap the following print statements based on the logic you wish to use System.out.print("#"); // print space in pattern (you can swap with print statement below to make logic easier } else System.out.print(" "); // print character for pattern System.out.println(); // move to next line of the pattern been printed System.out.println(); Tested for loop to print the following pattern goes below: // # # # # # # # # // # // # // # # # # # # # # // # # # # # # # # // # // # // # # # # # # # } // Complete the code for the following method according to the project specifications. public static void printHorizontalBars() { Scanner sc = new Scanner(System.in); int num1 = 0; // number of times to print * on line 1 int num2 = 0; // number of times to print * on line 2 int num3 = 0; // number of times to print * on line 3 int num4 = 0; // number of times to print * on line 4 System.out.println("printHorizontalBars() method called...."); // prompt user to enter four integers (selecting enter after each number) // read the numbers from the keyboard - remove comments tags to start // num1 = sc.nextInt(); // num2 = sc.nextInt(); // num3 = sc.nextInt(); // num4 = sc.nextInt(); // for loop to print out the number of *s for line 1 // for loop to print out the number of *s for line 2 // for loop to print out the number of *s for line 3 // for loop to print out the number of *s for line 4 } // Complete the code for the following method according to the project specifications. public static void printVerticalBars() { Scanner sc = new Scanner(System.in); int num1 = 0; // number of times to print * in column 1 int num2 = 0; // number of times to print * in column 2 int num3 = 0; // number of times to print * in column 3 int num4 = 0; // number of times to print * in column 4 // HINT: Printing to the console requires that you print from left to right starting // at the highest level and then moving from left to right...continuing to move from top // to bottom level of the pattern. If you think of each column as a building, which building has // the most floors. // // For each building as you move from left to right...do you print ** or do you print two spaces. // You will need to print two spaces between each building to keep things manageable // You will also need to use a System.out.println() when you are finishing printing each // level. // // Since you are moving from the highest level to the lowest level, what is your for loop // going to look like. You will need one for loop with four (4) if-else statements (one for each building) // inside the loop to help make the decision as to whether you will need to print the pattern // or spaces. // // When your loop has ended, you will then need to print a row of dashes at the bottom // of the pattern. int max = -1; // max number of *'s to print in any column System.out.println("printVerticalBars() method called...."); // prompt user to enter four integers (selecting enter after each number) System.out.println(); // read the numbers from the keyboard - remove comment tags to start // num1 = sc.nextInt(); // num2 = sc.nextInt(); // num3 = sc.nextInt(); // num4 = sc.nextInt(); // find the max of all inputs // for loop to print out the max number of levels // if-else statement for building 1 // if-else statement for building 2 // if-else statement for building 3 // if-else statement for building 4 // end loop // print row of dashes } }
Comments