Test Solutions, Comp Sci

Submitted by: Submitted by

Views: 97

Words: 352

Pages: 2

Category: Science and Technology

Date Submitted: 12/11/2013 03:00 PM

Report This Essay

1. 2750

2. Is a relationship therefore all choices are ok.

3. IndexOutOfBoundsException. Since John is not found in position 0, 1, 2, or 3, count is incremented in the loop and there is an attempt to check the next (non-existent) element in this array list.

4. IndexOutOfBoundsException

5. public class Marathoner extends Runner{

public Marathoner(String firstName, String lastName){

super(firstName, lastName);

}

public void race(double raceLength){

final int Minimum_Race_Length=1.0;

final double Minutes_Per_Mile=8.5;

final int Minutes_Per_Hour=6.0;

if(raceLength > Minimum_Race_Length){

super.race(raceLength);

}

else{

double trainingTime=Minutes_Per_Mile *raceLength/Minutes_Per_Hour;

train(trainingTime);

}

}

}

6. Course has a ClassRoster

Course has a Teacher

ClassRoster has a Student

Person has a HomeAddress

7. The endOfMonth calls are resolved by early binding because there is no implicit object--

there are two static methods with the same name but different parameters, and the

compiler merely picks the appropriate one.

The call to getBalance is resolved by late binding because the selection of which

account's balance is displayed does not take place until the program runs.

8. You can convert a subclass reference to a superclass reference. Therefore, a reference to a SavingsAccount object can be converted to a BankAccount reference.

SavingsAccount collegeFund = new SavingsAccount(10);

BankAccount anAccount = collegeFund;

Furthermore, all references can be converted to the type Object.

Object anObject = collegeFund;

Very occasionally, you need to carry out the opposite conversion, from a superclass reference to a subclass reference. For example, you may have a variable of type Object, and you know that it actually holds a BankAccount reference. In that case, you can use a cast to convert the type:

BankAccount anAccount = (BankAccount) anObject;

However,...