1 /**
2 This class describes triangle objects that can be displayed
3 as shapes like this:
4 []
5 [][]
6 [][][]
7 */
8 public class Triangle
9 {
10 private int width;
11
12 /**
13 Constructs a triangle.
14 @param aWidth the number of [] in the last row of the triangle.
15 */
16 public Triangle(int aWidth)
17 {
18 width = aWidth;
19 }
20
21 /**
22 Computes a string representing the triangle.
23 @return a string consisting of [] and newline characters
24 */
25 public String toString()
26 {
27 String r = "";
28 for (int i = 1; i <= width; i++)
29 {
30 // Make triangle row
31 for (int j = 1; j <= i; j++)
32 r = r + "[]";
33 r = r + "\n";
34 }
35 return r;
36 }
37 }