Java Reference
In-Depth Information
28 int small erArea =
smallerTriangle.getArea();
29 return smallerArea + width;
30 }
31
32 private int width;
33 }
ch13/triangle/TriangleTester.java
1 public class TriangleTester
2 {
3 public static void main(String[] args)
4 {
5 Triangle t = new Triangle( 10 );
6 int area = t.getArea();
7 System.out.println( ÐArea: Ñ + area);
8 System.out.println( ÐExpected: 55Ñ );
9 }
10 }
Output
Enter width: 10
Area: 55
Expected: 55
S ELF C HECK
1. Why is the statement if (width == 1) return 1 ; in the
getArea method unnecessary?
2. How would you modify the program to recursively compute the area of
a square?
591
592
C OMMON E RROR 13.1: Infinite Recursion
A common programming error is an infinite recursion: a method calling itself over
and over with no end in sight. The computer needs some amount of memory for
bookkeeping for each call. After some number of calls, all memory that is
Search WWH ::




Custom Search