Test 2 Sample
Test 2 Sample
+ act(int): int
+ move(P): boolean
+ walk(double)
+ move(C): boolean
+ run(int): int
+ walk(double)
How many methods are effectively present in class C? In other words, if you have an instance
of class C, how many different methods can you invoke on it based on this diagram? List these
methods and indicate, for each, whether it overrides a method in the parent; is inherited from
the parent; or is a brand new method in the child.
Answer:
There are five methods in C:
A x = new B();
x.move(x);
Answer:
a) The first is allowed thanks to the substitutability principle. The second calls for a cast down,
which is not done by the compiler.
b) The one in B. Early binding binds with the method in A since it is the target class. Late
binding searches for a bind in B and finds the overriding walk method.
c) The one in A. Early binding finds move(A) in the target class and binds with it. Late binding
searches for this signature in B and finds move(A) more specific than move(B).
This invocation, however, is not trivial because Stock overrides this method, so we try various
variations:
Explain why both variations will fail; i.e. both will invoke the method in Stock. Can you think of
any other variation that will invoke the method in Object?
Answer:
Both variations will succeed in getting the compiler to bind with the method in Object during
early binding. The VM, however, determines the target class based on the actual object, not
its reference, and hence binds with the method in Stock. Hence, the variations will fail to bind
with Object during late binding.
There is, in fact, no way for the client to invoke the toString of Object on an instance of Stock.
X r = new Y();
r.m(); Y Z
Answer:
a) To get an instance, you invoke the factory method. If that method is not static, then you first
need to get an instance, which is circular.
b) The first statement is allowed thanks to the substitutability principle. The second statement
binds with the method in Y because of late binding (this is always the case with interfaces
because all the (non-static) methods are effectively overridden).
which returns a randomly chosen Fraction object. Note that since Money and MixedNumber
are members of the Fraction inheritance chain, the returned object could be an instance of any
of three classes. See the end of this booklet for an overview of their APIs.
Write a code fragment that invokes this method five times in a loop and outputs the product of
all 5 fractions and the three type counts, i.e. how many of the returned objects were of each of
the three types.
Answer:
final int REPEATS = 5;
Fraction product = new Fraction(1,1);
int fraction = 0;
int mixedNumber = 0;
int money = 0;
output.println(fraction);
output.println(mixedNumber);
output.println(money);
b) Suppose you decided on a Set, how do you choose between HashSet and TreeSet?
c) Suppose you decided on TreeSet, write the statement that creates an empty collection.
d) How does the collection know how to sort the fraction objects?
Answer:
Answer:
Section 10.3.1 gives three different approaches for accomplishing this.
Constructor Summary
Fraction()
Construct a default fraction with numerator equal to 0 and denominator equal to 1.
Fraction(long numerator, long denominator)
Construct a fraction with the passed numerator and denominator.
Method Summary
void add(Fraction other) Add the passed fraction to the fraction on which it was called.
Fraction cloneMe() Copy the state of this object.
int compareTo(java.lang.Object other)
Compare this object with the specified object for order.
void divide(Fraction other)
Divide the fraction on which the method was called by the passed fraction.
boolean equals(java.lang.Object other)
Determine if this fraction is the same as the passed one.
long getDenominator() An accessor to the denominator of this fraction.
long getNumerator() An accessor to the numerator of this fraction.
static getRandom()
Fraction Create a random fraction.
char getSeaparator() An accessor to the separator of this fraction.
int hashCode() Compute a hash code for this fraction.
void multiply(Fraction other)
Multiply the fraction on which the method was called by the passed fraction.
void pow(int exponent)
Raise the fraction on which the method was called to the passed exponent.
boolean resembles(Fraction other)
Determine if this fraction resembles the passed one.
void setDenominator(long denominator) A mutator for the denominator of this fraction.
void setFraction(Fraction other) A mutator for this fraction.
void setFraction(long numerator, long denominator) A fraction mutator.
void setNumerator(long numerator) A mutator of the numerator of this fraction.
static setSeed(long seed)
void Change the seed of the random sequence returned by getRandom().
boolean setSeparator(char newSeparator) A mutator of the separator of this fraction.
void subtract(Fraction other)
Subtract the passed fraction from the fraction on which it was called.
String toProperString() Return this fraction as a proper fraction.
String toString() Return a string representation of this fraction.
Constructor Summary
MixedNumber()
Construct a default mixed number with sign +1, whole part equal to 0, numerator equal to zero, and
denominator equal to 1.
MixedNumber(int s, long w, long n, long d)
Construct a mixed number having the passed sign, whole part proper numerator, and proper denominator.
Method Summary
long getProperDenominator()
An accessor to the proper denominator of this mixed number.
long getProperNumerator()
An accessor to the proper numerator of this mixed number.
static Fraction getRandom()
Create a random mixed number with a randomly selected sign and with a whole
part, proper numerator, and denominator uniformly distributed in [0,1000) (but no zero‐
denominator mixed number is generated).
int getSign()
Determine and return the sign of this mixed number.
long getWhole()
An accessor to the whole part of this mixed number.
boolean resembles(MixedNumber other)
Determine if this mixed number resembles the passed one.
java.lang.String toString()
Determine and return a string representation of this mixed number.
Constructor Summary
Money()
Construct a default money amount of +1 dollar and zero cents.
Money(double m)
Construct a money amount having the passed real number value.
Money(int s, long d, long c)
Construct a money amount having the passed sign, dollar amount, and cent amount.
Method Summary
long getCent()
An accessor to the cent amount of this money amount.
long getDollar()
An accessor to the dollar amount of this money amount.
static Fraction getRandom()
Create a random fraction.
boolean resembles(Money other)
Determine if this money object resembles the passed one.
java.lang.String toString()
Determine and return a string representation of this money amount.