/**
This class describes triangle objects that can be displayed
as shapes like this:
[]
[][]
[][][]
*/
public class Triangle
{
/**
Constructs a triangle.
@param aWidth the number of [] in the last row of the triangle.
*/
public Triangle(int aWidth)
{
width = aWidth;
}
/**
Computes a string representing the triangle.
@return a string consisting of [] and newline characters
*/
public String toString()
{
String r = "";
for (int i = 1; i <= width; i++)
{
// Make triangle row
for (int j = 1; j <= i; j++)
r = r + "[]";
r = r + "\n";
}
return r;
}
private int width;
}
/**
This program prints two triangles.
*/
public class TriangleRunner
{
public static void main(String[] args)
{
Triangle small = new Triangle(3);
System.out.println(small.toString());
Triangle large = new Triangle(15);
System.out.println(large.toString());
}
}

How would you modify the nested loops so that you print a square instead of a triangle? Answer: Change the inner loop to for (int j = 1; j <= width; j++)
What is the value
of n after the following nested loops?
int n = 0;
for (int i = 1; i <= 5; i++)
for (int j = 0; j < i; j++)
n = n + j;
Answer: 20.