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."
for ( int
count = 1; count <= 10; count++ ) {
if ( count == 5 )
continue;
g.drawString( Integer.toString( count ), xPos, 25 );
xPos += 10;
}
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:
For example, if a represents the
set {0, 2, 4} and b represents the
set {2, 3, 4, 5} then a.union(b) represents
the set {0, 2, 3, 4, 5}.
public class HugeInteger {
private int digits[];
// digits of huge integer
final static int SIZE
= 10; // number of digits of huge integer
boolean error;
// if overflow or underflow occurred, then true
// else false
// Construct a Huge
0.
public HugeInteger()
{}
// Constructor that
takes an array of SIZE digits
public HugeInteger(int
d[]) {}
// Construct a HugeInteger
of value I
public HugeInteger(HugeInteger
I) {}
// Return a String representation
of a HugeInteger
public String outputHugeInteger()
{}
// Add I to this, put
result in this
public HugeInteger
addHugeIntegers(HugeInteger I) {}
// Subtract I from this,
put result in this
public HugeInteger
subtractHugeIntegers(HugeInteger I) {}
// Multiply this by
I, put result in this
public HugeInteger
multiplyHugeIntegers(HugeInteger I) {}
// Divide this by I,
put result in this
public HugeInteger
divideHugeIntegers(HugeInteger I) {}
// return the remainder
of this when divided by I (this is unchanged)
public HugeInteger
modulusHugeIntegers(HugeInteger I) {}
// true when this ==
I
public boolean isEqualTo(HugeInteger
I) {}
// true when this <
I
public boolean isLessThan(HugeInteger
I) {}
}
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:
7. Describe the difference between composition and inheritance. Give examples of each.