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

Write a method named rowOfStars that takes a single integer parameter n and retu

ID: 3551466 • Letter: W

Question

  1. Write a method named rowOfStars that takes a single integer parameter n and returns a String with that many stars in it. For example, calling the method with a value of 3 would cause the String "***" to be returned.
  2. Using the method you wrote for the previous question, write a method that takes a single integer parameter n and prints a triangle of stars to the screen that has a height and base size equal to n. For example, calling the method with a value of 4 will produce the triangle:
     *
    **
    ***
    ****


Explanation / Answer


public class HelloWorld{


public static void main(String []args){

  

HelloWorld obj = new HelloWorld();

System.out.println(obj.printStars(4));

  

}

public String getStars(int a){

StringBuilder str = new StringBuilder();

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

str.append("*");

return str.toString();

}

public String printStars(int a){

StringBuilder str = new StringBuilder();

for(int i=1;i<=a;i++)

str.append(getStars(i)).append(" ");

return str.toString();

}

}

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