CS 10: Introduction to Computer Programming
In-Class Examination 3
Closed-Book, 50 minutes, Fall 1997
General Instructions
-
Before you answer any questions, write your name, perm number, and
discussion section on your answer booklet.
-
Read each question carefully.
-
Make sure that you clearly understand a question before answering it.
-
Please put your answer to each question on its own page in your blue-book.
-
Answers that do not show work will not get partial credit.
-
You may wish to work out an answer on scratch paper before writing it on
your answer page; answers that are difficult to read may lose points for
that reason.
Question |
Value |
1 |
25 |
2 |
15 |
3 |
20 |
4 |
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.
public class Rectangle {
private float length,
width;
public Rectangle() {
length = 1;
width = 1;
}
public float getLength()
{ return length; }
public float getWidth()
{ return width; }
public void setLength(float
Length) {
length = ( 0.0 < Length && Length < 20.0) ? Length : 0;
}
public void setWidth(float
Width) {
width = ( 0.0 < Width && Width < 20.0) ? Width : 0;
}
public float area()
{ return length*width; }
}
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.
Rectangle cigar = new Rectangle(), box = new Rectangle();
cigar.setLength(3);
cigar.setWidth(4);
box.setLength(6);
box.setWidth(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.
Solution:
g.drawString("cigar
area = " + cigar.area(), 50, 60);
System.out.println("box
area = " + box.area());
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.
Solution:
int table[][] = {{-1,
0, 1}, {-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.
boolean exclusive_or[][] = {{false, true}, {true, false}};
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.
Solution:
void summer(int table[][],
int sums[]) {
for (int i = 0; i < sums.length; i++) sums[i] = 0;
for (int i = 0; i < table.length; i++)
for (int j = 0; j < table[i].length; j++)
sums[i + j] += table[i][j];
}
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.
int matrix[][] = new int[10][10], diagonal[] = new int[19];
...
summer(matrix, diagonal);