Question: Java: After the loops, add a call to a recursive method that will accomplish the same function. Keep the original loops also. The program should
Java:
After the loops, add a call to a recursive method that will accomplish the same function. Keep the original loops also.
The program should show the same results with the loop as with the recursive method.
Please test it to make sure it works the same as with the loops.
Please use comments as you convert these loops to recursive. I'm trying to learn this new method introduced to me.
Code:
public class SolvingStarDesigns {
public static void main(String[] args) { SolvingStarDesigns myStars = new SolvingStarDesigns(); myStars.makeDesign1(); myStars.makeDesign2(); myStars.makeDesign3(); myStars.makeDesign4(); } //4 rows, 4,3,2,1 stars public void makeDesign1() { System.out.println("Star Design #1:"); for (int i = 4; i > 0; i--) { for (int j = 1; j < i + 1; j++) { System.out.print("*"); } System.out.println(); } System.out.println(); } //5 rows, 1,2,3,4,5 stars public void makeDesign2() { System.out.println("Star Design #2:"); for (int i = 0; i < 5; i++) { for (int j = i + 1; j > 0; j--) { System.out.print("*"); } System.out.println(); } System.out.println(); } //5 rows, 5,4,3,2,1 stars public void makeDesign3() { System.out.println("Star Design #3:"); for (int i = 5; i > 0; i--) { for (int j = 1; j < i + 1; j++) { System.out.print("*"); } System.out.println(); } System.out.println(); } //4 rows, 1,3,5,7 stars public void makeDesign4() { System.out.println("Star Design #4:"); for (int i = 1; i < 5; i++) { for (int s = 1; s < (2*i); s++) { System.out.print("*"); } System.out.println(); } System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
