0

Write the definition of a method named makePowerOfTwoStars that receives a non-negative integer n and prints a string consisting of 2^n asterisks. The method must not use a loop of any kind.

public static String makePowerOfTwoStars(int n) {
    if (n == 0)
        return "*";
    else {
        return "*" + makePowerOfTwoStars((int)(Math.pow(2,n-1) - 1)) + "*";
    }
}

I have been working on this for a while, and I cannot figure out the necessary algorithm to accomplish the goal.

After continuing to work on this, it seems that the method itself is a void method that does the printing itself. Thanks to Antoniossss for the working algorithm for the String-returning method. Now I get the error "void type not allowed here". I realize it makes no sense to print something from a method that returns nothing, but I don't really know how to fix it, since the program that calls the method does not print. Here is the new code, and thanks for the help.

public static void printPowerOfTwoStars(int n) {

    if (n == 0) {
        System.out.print("*");
    } else {
        System.out.print(printPowerOfTwoStars(n - 1) + printPowerOfTwoStars(n - 1));
    }
}

Also, I realize that the method name has changed. The program that calls the method apparently has a typo.

so8857
  • 351
  • 2
  • 14
  • Can you use a stringbuilder and a for loop to create a string with n amount of asterisks? – This_Is_Alex_ Jan 26 '17 at 02:29
  • @This_Is_Alex_ no loops are allowed. – so8857 Jan 26 '17 at 02:30
  • Sorry missed the most important part – This_Is_Alex_ Jan 26 '17 at 02:36
  • 1
    Or a non-recursive method `int n = 5; String output = new String(new char[n]).replace("\0", "*"); System.out.println(output);` – Scary Wombat Jan 26 '17 at 02:37
  • @ScaryWombat close but it should be 32 stars – Antoniossss Jan 26 '17 at 02:39
  • or see this http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java – Scary Wombat Jan 26 '17 at 02:45
  • `stars(n) : if n == 0 then result = "*"; else result = stars(n-1) + stars(n-1);`. Just think of it: 2^n is actually 2 * 2^(n-1). i.e. you get number of stars for `(n-1)`, and then double it – Adrian Shum Jan 26 '17 at 02:45
  • @Antoniossss Good joke – Scary Wombat Jan 26 '17 at 02:45
  • @ScaryWombat well I think that's not a joke: OP is asking how to print `2^n` stars, not n stars – Adrian Shum Jan 26 '17 at 02:46
  • @AdrianShum I would have thought that the OP would have enough java knowledge to know to replace `n` with `2^n` – Scary Wombat Jan 26 '17 at 02:49
  • @ScaryWombat that's actually my doubt :P – Adrian Shum Jan 26 '17 at 02:51
  • @AdrianShum I probably could have figured that out, but the method isn't recursive, so ultimately is not particularly helpful in this instance. – so8857 Jan 26 '17 at 02:53
  • @ScaryWombat where is that joke ? – Antoniossss Jan 26 '17 at 02:57
  • @Antoniossss My Bad, I thought you were joking. It seems that people assume that the OP does not know how to replace `int n = 5;` with `int n = 5; n = (int)(Math.pow(2,n-1) - 1);` – Scary Wombat Jan 26 '17 at 03:01
  • @so8857 Why my method is not recursive? It IS recursive. The way to find stars for n is to find stars for n-1 and double it. if n == 0, it is 1 star. Very typical recursive method – Adrian Shum Jan 26 '17 at 03:03
  • @ScaryWombat sorry mate 4:14am here, didnt get the you right. – Antoniossss Jan 26 '17 at 03:14
  • It's marked as duplicate so I can't provide an official answer, but this works. Keep in mind though that it's VERY inefficient, but it sounds like you don't really care about that. `public static String makePowerOfTwoStars(int n) { if ( n == 0 ) { return "*"; } else { return makePowerOfTwoStars( n-1 ) + makePowerOfTwoStars( n-1 ); } }` – voipdaddy Jan 26 '17 at 03:25
  • @Antoniossss I must have misunderstood what you were saying. Sry. I have updated the problem, and thanks for your help. – so8857 Jan 26 '17 at 03:32
  • @voipdaddy Thanks for your help. I updated the problem. – so8857 Jan 26 '17 at 03:32
  • Just remove the System.out.println from your recursive call. Instead just do two recursive calls like this: `public static void printPowerOfTwoStars(int n) { if (n == 0) { System.out.print("*"); } else { printPowerOfTwoStars(n - 1); printPowerOfTwoStars(n - 1); }}` – voipdaddy Jan 26 '17 at 03:36
  • @voipdaddy Thanks so much! It works. – so8857 Jan 26 '17 at 05:14

0 Answers0