You are responsible for all material presented in lecture. You are also responsible for all material covered in lab and the concepts taught by any programming assignments.
Answering the applicable "self-test" questions in each chapter of the recommended text (by Savitch) 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.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
{
private int nrWheels;
public Vehicle( )
{ this( 4 ); }
public Vehicle ( int nrWheels )
{ setWheels( nrWheels); }
public String toString ( )
{ return "Vehicle with " + getWheels()
+ " wheels"; }
public int getWheels ( )
{ return nrWheels; }
public void setWheels ( int wheels )
{ nrWheels = wheels; }
}
|
// Parking Lot class
public class Lot
{
private final static int MAX_VEHICLES = 20;
private int nrVehicles;
private Vehicle [] vehicles;
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 (int v = 0; v < nrVehicles; 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;
}
}
|
// 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); }?
Comparable interface defines the method compareTo.
What are the compareTo interface semantics?compareTo method that compares one Person
object to another. Assume that a Person consists of a first name,
last name, and age.
True/False
Comparable interface is often implemented by classes
that require sorting.