import java.util.*;
class Triangle {
public static String[]Triangles;
public void drawTriangle() {
//This method writes out the menu and asks for the input value.
//Then it calls two other methods, which draws the simple triangle
//as well as the bigger version of the triangle.
Triangle c = new Triangle();
Scanner scan = new Scanner(System.in);
int numSides;
System.out.println("Welcome to the equilateral triangle drawing program.");
System.out.print("Enter the length of the side: ");
numSides = scan.nextInt();
//Asks for the user input, variable set to numSides
c.drawFirstTriangle(numSides);
//Calls the method to draw simple triangle
System.out.println("");
System.out.println("A larger version looks like this:");
c.drawSecondTriangle(numSides);
//Calls the method to draw the bigger version of the triangle
}
public static void drawFirstTriangle(int sides) {
int height = sides;
char[] pad = new char[sides];
Triangles = new String[sides];
Arrays.fill(pad, ' ');
//Initiating variables to be used within the class
//Arrays.fill will occupy the pad array, putting a blank space char in it
for(int i=0; i<sides; i++){
String triangle = "";
//Initiating triangle lines, initially set to blank.
int j = 0;
System.out.print(new String(pad,0,i));
//Prints the spaces before the first * character is printed.
//First run it will not print a space since i = 0, starting i = 1
//this will print a blank space "i" number of times
while(j < height) {
triangle = triangle + "* ";
j++;
}
Triangles[i] = triangle;
System.out.println(triangle);
//Once the string triangle is finished, it is stored in a array called Triangles
height--;
}
}
public static void drawSecondTriangle(int sides) {
Triangle c = new Triangle();
char[] pad = new char[sides*sides];
Arrays.fill(pad, ' ');
int height = 0, numCount = sides;
//Initiating variables to be used within the class
//Arrays.fill will occupy the pad array, putting a black space char in it
//pad[] array's bound will be sides * sides since that's how many rows there will be
for(int j = 0; j < sides; j++) {
for(int i = 0; i < sides; i++) {
int count = 0;
String interSpaces = new String(pad,0,i*2);
String spaces = new String(pad,0,height);
//Initiating the two different spacings, one in the beggining
//and the other for spacing in between the triangles
System.out.print(spaces);
while(count < numCount) {
System.out.print(Triangles[i]);
System.out.print(interSpaces);
count++;
//Prints out the first complete set of triangles
//Example: if length was 4, then it would print
//4 full triangles (the base)
}
System.out.println("");
height++;
}
//Once the inner for loop is done, then it goes in the loop again to
//build the next set of triangles, which will be 1 less triangle
//then the previous set.
numCount--;
}
}
public static void main(String[]args) {
//Initiates the Triangle class, and allows main method to call on other methods
Triangle c = new Triangle();
c.drawTriangle();
}
}