Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Write a method called makeStars. The method receives an int parameter that

ID: 3779032 • Letter: J

Question

Java

Write a method called makeStars. The method receives an int parameter that is guaranteed not to be negative. The method returns a String whose length equals the parameter and contains no characters other than asterisks. Thus, makeStars(8) will return ******** (8 asterisks). The method must not use a loop of any kind (for, while, do-while) nor use any String methods other than concatenation. Instead, it gets the job done by examining its parameter , and if zero returns an empty string otherwise returns the concatenation of an asterisk with the string returned by an appropriately formulated recursive call to itself.

Explanation / Answer

import java.util.Scanner;
class stars
{
static String makeStars(int n)
{
   if(n==1)
       return("*");
   else
   {
       /*Use either of these statements*/
       return("*".concat(makeStars(n-1))); //concatenation using concat
       //return(makeStars(n-1).concat("*")); //concatenation using concat
       //return("*"+makeStars(n-1)); //concatenation using +
   }
}
   public static void main(String args[])
   {
       int n;
       Scanner in=new Scanner(System.in);
       System.out.println("Enter a number");
       n=in.nextInt();
       System.out.println(makeStars(n));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote