0% found this document useful (0 votes)
15 views10 pages

Test 2 Sample

The UML diagram shows a parent class P with 3 methods and a child class C that inherits 2 methods from P and defines 3 new methods of its own. There are a total of 5 methods in class C - 2 inherited from P, and 3 new methods defined in C. One of the inherited methods is overridden in C. [/SUMMARY]

Uploaded by

137Sakshi Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Test 2 Sample

The UML diagram shows a parent class P with 3 methods and a child class C that inherits 2 methods from P and defines 3 new methods of its own. There are a total of 5 methods in class C - 2 inherited from P, and 3 new methods defined in C. One of the inherited methods is overridden in C. [/SUMMARY]

Uploaded by

137Sakshi Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Consider the following UML class diagram:

+ 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:

1. move(C) which is brand new


2. run(int) which is also brand new
3. walk(double) which overrides the parent's method
4. act(int) which is inherited from the parent
5. move(P) which is inherited from the parent

CSE1020‐buildIt Written Test #2 Sample Page 1 of 10


Answer the following questions based on the UML diagram shown to A
the right:
+ act(int): int
a) Both of the following statements involve type mismatch (the type
of the left side of the assignment is different from that of the right + move(A): boolean
side). Explain why the first compiles while the second doesn't.
+ walk(double)
A a = new B();
B b = new A();
B
b) Which walk method will be invoked by the second statement of
the following pair, the one in A or the one in B? Justify. + move(B): boolean

A a = new B(); + run(int): int


a.walk(12.5);
+ walk(double)
c) Which move method will be invoked by the second statement of
the following pair, the one in A or the one in B? Justify.

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).

CSE1020‐buildIt Written Test #2 Sample Page 2 of 10


The toString method of the Object class returns the memory address at which an object is
stored. Since a Stock is-a Object, it inherits this method (obligatory inheritance) and we can
invoke it to find out the memory address of a given Stock instance.

This invocation, however, is not trivial because Stock overrides this method, so we try various
variations:

Stock s = new Stock(".AB");


output.println( (Object s).toString() );

Object obj = new Stock(".AB");


output.println( obj.toString() );

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.

CSE1020‐buildIt Written Test #2 Sample Page 3 of 10


a) An abstract class C has a factory method that returns an instance of its type.
Argue that this method "better be" static.

b) The UML diagram to the right shows an interface X


and two classes (Y and Z) that implement it. Based X
on this diagram, explain why the first statement in the m(): void
following pair compiles without problems. Moreover,
determine which method m is invoked by the second n(long): int
statement, the one in X or in Y.

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).

CSE1020‐buildIt Written Test #2 Sample Page 4 of 10


Suppose that the Util class has the method:

public static Fraction getFraction()

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;

for (int i = 0; i < REPEATS; i++)


{
Fraction f = Util.getFraction();
product.multiply(f);
if (f instanceof Money)
money++;
else if (f instanceof MixedNumber)
mixedNumber++;
else
fraction++;
}

output.println(fraction);
output.println(mixedNumber);
output.println(money);

CSE1020‐buildIt Written Test #2 Sample Page 5 of 10


Suppose you have a number of Fraction objects and you like to store them in a collection.

a) How do you decide whether to use a List or a Set?

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:

a) Read Section 10.1.1

b) Read Section 10.1.2

c) Set<Fraction> set = new TreeSet<Fraction>();


Note that it is a S/E violation if you declare via TreeSet or if you omit the generics

d) Based on "natural ordering", i.e. the return of compareTo in Fraction.

CSE1020‐buildIt Written Test #2 Sample Page 6 of 10


You are given a reference list to an instance of List<Fraction> (assert list != null).
How would you determine if this list contains duplicates? Write a fragment that outputs YES or
NO depending on whether the list has two or more equal fractions or not.

Answer:
Section 10.3.1 gives three different approaches for accomplishing this.

CSE1020‐buildIt Written Test #2 Sample Page 7 of 10


type.lib.Fraction

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.

CSE1020‐buildIt Written Test #2 Sample Page 8 of 10


type.lib.MixedNumber extends 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.

Methods inherited from class type.lib.Fraction


add, cloneMe, compareTo, divide, equals, getDenominator, getNumerator,
getSeaparator, hashCode, multiply, pow, resembles, setDenominator,
setFraction, setFraction, setNumerator, setSeed, setSeparator, subtract,
toProperString

CSE1020‐buildIt Written Test #2 Sample Page 9 of 10


type.lib.Money extends MixedNumber

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.

Methods inherited from class type.lib.MixedNumber


getProperDenominator, getProperNumerator, getSign, getWhole, resembles

Methods inherited from class type.lib.Fraction


add, cloneMe, compareTo, divide, equals, getDenominator, getNumerator,
getSeaparator, hashCode, multiply, pow, resembles, setDenominator,
setFraction, setFraction, setNumerator, setSeed, setSeparator, subtract,
toProperString

CSE1020‐buildIt Written Test #2 Sample Page 10 of 10

You might also like