CS 10: Introduction to Computer Programming

Final Examination

Open-Book, 3 hours, Spring 1997

General Instructions



Question  Value  Score 
1  8 
2  8
3  18
4  24
5  16
6  16
7  10
Total  100 

1. Give an equivalent sequence of Java statements that does not use the for statement.  If it cannot be done, say "It cannot be done." 




2. Give an equivalent sequence of Java statements that does not use the switch statement.  If it cannot be done, say "It cannot be done."


3. Create a class Rectangle.  The class has attributes length and width, each of which defaults to 1.  It has a method that calculates the area of the rectangle.  It has set and get methods for both length and width.  The set method should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.  Rectangle also has a predicate method square that returns true if and only if the rectangle is square.

4. Create a class IntegerSet Each object of the class can hold integers in the range 0 through 100.  A set is represented internally as an array of ones and zeros.  Array element a[i] is 1 if i is in the set.  Array element a[i] is 0, otherwise.  For example, for the set {3, 5, 7, 10} a[3] is 1, a[5] is 1, a[7] is 1, and a[10] is 1; all other elements of a are 0.

    Provide the following methods:

5. Using the following interface for a class HugeInteger, give a HugeInteger method, factor, that returns a HugeInteger that is its smallest factor greater than 1.  For example, if factor were applied to a HugeInteger representing 12, it would return a HugeInteger representing 2.  The method factor can assume that its HugeInteger is not in the error state.  It does not have to be efficient. 

6. Using a method factor that operates according to the specifications of the previous problem, write a HugeInteger method enumerateFactors that uses the method outputHugeInteger to print out all of its prime factors with repetition.   For example, if enumerateFactor were applied to a HugeInteger representing 12, then enumerateFactor would invoke outputHugeInteger with:
    2
    2
    3

It looks like this:

and uses the drawString method to print the factors onto the Graphics object, g.

7. Describe the difference between composition and inheritance.  Give examples of each.