You are responsible for all material presented in lecture. You are also responsible for all associated reading assignments and material covered in lab.
Answering the applicable "self-test" questions in each chapter of the text is also a good way to review. The answers to the self-test questions are found at the end of each chapter.
DO NOT EXPECT to see these specific questions on your exam.clone
method, and why is it necessary (in the context of polymorphism)?True/False
Examine this code, then answer the questions below.
public class Exam2Code { public static void main ( String[ ] args ) { Lot parkingLot; Car chevy = new Car( ); Car camry = new Car( ); MotorCycle harley = new MotorCycle( 3 ); MotorCycle honda = new MotorCycle( ); ParkingLot.park ( chevy ); ParkingLot.park ( honda ); ParkingLot.park ( harley ); ParkingLot.park ( camry ); System.out.println( parkingLot.toString( ) ); } } |
// Vehicle class public class Vehicle { public Vehicle( ) { this( 4 ); } public Vehicle ( int nrWheels) { setWheels( nrWheels); } public String toString ( ) { System.out.println( "Vehicle with " + getWheels() + " wheels"); } public int getWheels ( ) { return nrWheels; } public void setWheels (int wheels ) { nrWheels = wheels; } private int nrWheels; } |
// Parking Lot class public class Lot { private final int MAX_VEHICLES = 20; public Lot ( ) { nrVehicles = 0; vehicles = new Vehicle[MAX_VEHICLES]; } public int nrParked ( ) { return nrVehicles; } public void park ( Vehicle v ) { vehicles[ nrVehicles++ ] = v; } public int totalWheels ( ) { int nrWheels = 0; for (v = 0; v < nrVehichles; v++ ) nrWheels += vehicles[ v ].getWheels( ); return nrWheels; } public String toString( ) { String s = ""; for ( v = 0; v < nrVehicles; v++ ) s += vehicles[ v ].toString( ) + "\n"; return s; } private int nrVehicles; private Vehicle [] vehicles; } |
// MotorCycle class public class MotorCycle extends Vehicle { public MotorCycle ( ) { this( 2 ); } public MotorCycle( int wheels ) { super( wheels ); } } //=================== // Car class public class Car extends Vehicle { public Car ( ) { super( 4 ); } public String toString( ) { return "Car with " + getWheels( ) + " wheels"; } } |
nrVehicles
in the Lot
class?Lot
's park
method. How would you correct
this coding error?Vehicle
constructor, what is the purpose of { this( 4 ); }
?MotorCycle
constructor, what is the purpose of { super( nrWheels); }
?finally
block? What kind of operations would typically be
performed there?try
block has multiple catch
blocks, the order in which the catch
blocks are defined is important. Why is this so?throws
clause and when is it needed?True/False
Comparable
interface defines the method compareTo
.
What are the Comparable interface semantics?compareTo
?True/False
Comparable
interface is often implemented by classes
that require sorting.Comparble
interface.