Question: I cant Figure out what is wrong with my Code here is what I have package starString; public class starString { public static void main(String[]

I cant Figure out what is wrong with my Code here is what I have

package starString;

public class starString {

public static void main(String[] args) { for (int i = 0; i <= 4; i++) System.out.println(starString(i)); } public static String StarString (int n) { if (n < 0 ) throw new IllegalArgumentsException(); else if (n==0) return "*"; else return starString (n-1) + starString (n-1); }

}

Instructions:

. Write a recursive method called starString that accepts an integer as a parameter and prints to the console a string of stars (asterisks) that is 2 n (i.e., 2 to the n th power) long. For example,

  • starString(0) should print * (because 2 0 == 1)

  • starString(1) should print ** (because 2 1 == 2)

  • starString(2) should print **** (because 2 2 == 4)

  • starString(3) should print ******** (because 2 3 == 8)

  • starString(4) should print **************** (because 2 4 == 16)

The method should throw an IllegalArgumentException if passed a value less than 0.

any help would be great thank you in advance

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!