Java: Assume the availability of a method named makeStars that can be passed a n
ID: 3922367 • Letter: J
Question
Java:
Assume the availability of a method named makeStars that can be passed a non-negative integer n and that returns a String of n asterisks. Write a method named printTriangle that receives a non-negative integer n and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 asterisks, and then a line of n-2 asterisks, and so on. For example, if the method received 5 it would print:
* * * * *
* * * *
* * *
* *
*
The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke makeStars to accomplish the task of printing a single line.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void makeStars(int n)
{
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
System.out.print("*");
System.out.print(" ");
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
makeStars(5);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.