previous | start | next

File Triangle.java

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    /**
11        Constructs a triangle.
12       @param aWidth the number of [] in the last row of the triangle.
13     */
14    public Triangle(int aWidth)
15    {
16       width = aWidth;
17    }
18
19    /**
20        Computes a string representing the triangle.
21       @return a string consisting of [] and newline characters
22     */
23    public String toString()
24    {
25       String r = "";
26       for (int i = 1; i <= width; i++)
27       {  
28          // make triangle row
29          for (int j = 1; j <= i; j++)
30             r = r + "[]";
31          r = r + "\n";
32       }
33       return r;
34    }
35
36    private int width;
37 }


previous | start | next