CS 10: Introduction to Computer Programming

In-Class Examination 3

Closed-Book, 50 minutes, Fall 1997
 

General Instructions


Question  Value 
25 
15 
20 
30 
5 10
Total 100 


1. Create a class Rectangle. The class has private float attributes length and width. It has a no-argument constructor that sets these values to 1.  It has a method area that returns the area of the rectangle.  It has set and get methods for both length and width.  The set methods should verify that length and width are numbers larger than 0.0 and less than 20.0.  If they aren't, then the member is set to 0.

2.
A. Give a declaration, based on this class, for two Rectangle objects: cigar and box. Give Java code for setting cigar to a   length of 3 and a width of 4, and box to a length of 6 and a width of 3.

B. Use the drawString method to display the area of cigar on a Graphics object called g, starting at the pixel whose coordinates are (50, 60).  Use the System.out.println method to print the area of box.

3.
A. Declare a 2-dimensional int array, table, whose first dimension represents rows and whose second dimension represents columns.  Initialize it, in the declaration, so that it has 2 rows:  the first row has 3 values: -1, 0, 1;  the second row has 5 values: -2, -1, 0, 1, 2.

B. Declare a 2X2 boolean array (i.e., 2 rows and 2 columns), exclusive_or.  Initialize it, in the declaration, so that  exclusive_or[i][j] is true if and only if either i equals 1 or j equals 1, but not both.

4. Write a Java method that has two parameters: a 2-dimensional int array called table, and a 1-dimensional int array called sums.  Array element table[i][j] is on the same diagonal, called diagonal i+j,  as table[k][l] when i + j = k + l.  For example, table[3][4] is on the same diagonal (diagonal 7) as table[1][6].  Your method puts the sum of the entries on diagonal i of table into the ith element of sums.

5.  Declare a 10 X 10 int array called matrix.  Declare a 1-dimensional int array called diagonal with the same number of elements as there are diagonals in array matrix.  Write a Java statement that invokes the method described in question 4 with matrix and diagonal as arguments.