Use the list of questions below as a guide when studying for the final exam. It is by no means a comprehensive list of questions. Also, these are not the exact questions that will be on the exam. The form of the questions on the exam may also be different from those found here.
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.
public class Exam2Code { public static void main ( String[ ] args ) { Lot parkingLot = new Lot(); 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 (Vehicle v : vehicles){ if(v != null){ s += 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
cla
ss?Lot
's park
method. How would yo
u 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. Each Person
can be sorted alphabetically
by last name, then first name in case of ties on the last name.Comparable
interface is often implemented by
classes
that require sorting.CDNotFoundException
. The program also catches this
exception, displays an appropriate error message to the user, then continues.
Write the code for the CDNotFoundException
exception class.
CDNotFoundException
so that
getMessage()
can return a message that includes the the CD's
ID (an integer) in your collection.
RuntimeExceptions
.
Use the following class definition when answering the questions below.
public class Utility { // no instance variables //..... }
Utility
class above?
First
that
returns the the first object from an ArrayList<T>
of
any base type as it would appear in the Utility
class above.
First
would be called from main( )
for
an ArrayList
of objects of type Bob
.
Monarch
and Lion
are derived classes of
Animal
):
Lion king = new Lion(); Monarch monarch = new Monarch(); Cage<Monarch> butterflyCage = new Cage<Monarch>(); Cage<Lion> lionCage = new Cage<Lion>(); lionCage.add(king); butterflyCage.add(monarch); Cage<Animal> animalCage = new Cage<Animal>(); animalCage.add(king); animalCage.add(monarch);Why will assigning
animalCage = lionCage;
and animalCage =
butterflyCage;
cause compile errors?
T
” when defining generic
classes or methods. In particular, what is the meaning of
<T>
, <T extends Animal>
, and
<T extends Comparable<T>>
. Give an example
definition that uses each of these.
<?>
,
<? extends T>
, <? super T>
, and
<T extends Comparable<? super T>>
? Give an
example definition that uses each of these.
T object = new T();
<T extends Comparable<T>>
in the method header below.
public class RClass<T extends Comparable<T>>
Box
that
contains objects of any type and supports the following operations. What
design decisions must be considered for these Box
operations?
Box
. The capacity is specified by the user
when the box is created.
Box
Box
Box
Box
Box
that holds 10 Integer
s.
Box
that holds 25 XYZ
objects.
List
reference rather than an ArrayList
reference?
List<Integer> myList = new ArrayList<Integer>( );
Collection<T>
represent?
Set<T>
and
List<T>
?
ArrayList<T>
and
LinkedList<T>
?
Collections
class used for?