Question: This part is intentionally a bit less specific as the objective is for you to minimize the unnecessary redundancy in Lab2 by generalizing the loop

  1. This part is intentionally a bit less specific as the objective is for you to minimize the unnecessary redundancy in Lab2 by generalizing the loop redundancy where possible by encapsulating the inner loops (within the shape drawing functions) into their own functions. Just like back in Lab1, some functionally will be unique enough to keep everything together in a single function, other places may have the same reoccurring structures which could be broken out into their own slapper functions. In short: Remove the inner-loops from your Lab2 Solution and, WHERE POSSIBLE, replace them with calls to functions/methods, passing any necessary parameters.

public class Lab2 { static final int ROWS = 5; public static void main(String[] args) { System.out.println("Shape 1:"); printShape1(); System.out.println("Shape 4:"); printShape4(); } static void printShape1() { // print rows for (int i = 1; i <= ROWS; i++) { // print 5 '*' characters. printRowOfChars(5,'*'); System.out.println(); } } static void printShape4() { // print rows for (int i = 1; i <= ROWS; i++) { // It's own method because it's just printing a single character // a given number of times. printRowOfChars((ROWS - i) * 2, ' '); // Kept here becuase it isn't a single character, but a pare of two // different characters. for (int j = 1; j <= i; j++) { System.out.print(i); System.out.print(" "); }

System.out.println(); } } // Prints a row of a single character. static void printRowOfChars(int count, char what) { for(int i = 0; i < count; i++) { System.out.print(what); } }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!