88.5k views
2 votes
Write a method starString that accepts an integer parameter n and returns a string of stars (asterisks) 2n long (i.e., 2 to the nth power).

User Orionss
by
5.2k points

1 Answer

2 votes

Answer:

public class Main

{

public static void main(String[] args) {

System.out.println(starString(4));

}

public static String starString(int n){

double p = Math.pow(2,n);

String s = "";

for(int i=0; i<p; i++)

s += "*";

return s;

}

}

Step-by-step explanation:

Create a method named starString that takes an integer parameter, n

Get the 2 to the nth power using pow method and set it to the p

Create an empty string that will hold the asterisks

Create a for loop that will iterate p times. Inside the loop, concatenate an asterisk to the s

Return the s

Inside the main method, call the method with an integer parameter

User Hillmark
by
5.2k points