Write a method named rowOfStars that takes a single integer parameter n and retu
ID: 3551466 • Letter: W
Question
- 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.
- 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();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.