Question:
Create a method called printLineOfStars(int n).
Then create methods to print a Square and Triangle using the method printLineOfStars(int n).
Code:
import java.util.Scanner; public class LineOfStars { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Integer: "); int i = input.nextInt(); System.out.println("Line:"); printLineOfStars(i); System.out.println("Square:"); printSquare(i); System.out.println("Triangle:"); printTriangle(i); } public static void printLineOfStars(int n){ for(int i=1;i<=n;i++){ System.out.print("*"); } System.out.println(); } public static void printSquare(int n){ for(int i=1;i<=n;i++){ printLineOfStars(n); } } public static void printTriangle(int n){ for(int i=1;i<=n;i++){ printLineOfStars(i); } } }
Output:
$ java LineOfStars Enter Integer: 6 Line: ****** Square: ****** ****** ****** ****** ****** ****** Triangle: * ** *** **** ***** ******