Homework 5 sample solution
1a)
public String getString()
{
return suit;
}
public void printString()
{
System.out.println(suit);
}
b)
public boolean isRed( Card c )
{
return c.getSuit().equals("diamond") || c.getSuit().equals("heart");
}
I used getString() because I need a
return value to compare against -- printString does not return anything.
2) This is purely an example -- many solutions are possible!
a)
Car properties:
VIN
year
fuel remaining
owner
destination
Car responsibilities:
start
turn off
signal for stop, left turn, right turn
get destination
Truck properties:
VIN
year
fuel remaining
driver
list of delivery stops (including information that tells which
deliveries have been completed)
load capacity
current load weight
Truck responsibilities:
start
turn off
signal for stop, left turn, right turn
load
unload
get destination (the next delivery stop)
Bus properties:
VIN
year
fuel remaining
driver
list of stops for this route
most recent stop passed
max passengers
current number of passengers
Bus responsibilities:
start
turn off
signal for stop, left turn, right turn
board passenger
disembark passenger
get destination (the next stop)
b)
Vehicle properties:
(These are the properties that are
common to the individual classes.)
VIN
year
fuel remaining
Vehicle responsibilities that are implemented in Vehicle:
(These are responsibilities that
don't need special handling for each type of vehicle.)
start
turn off
signal for left turn, right turn
Vehicle responsibilities that are abstract:
(These are responsibilities that all
of the individual classes have, but that must be performed differently
for each type.)
signal for stop (This is abstract because the bus, if
it's a schoolbus, has extra work to do, e.g. extend its stop sign.)
get destination (This is abstract
because each type needs to figure out its destination by a different
means -- the Car just needs to return its destination property, but the
Bus has to look up the stop after its current stop in its list of
stops, while the Truck has to find the first uncompleted delivery stop.)
All the properties not present in Vehicle will be defined in the
child classes. All the responsibilities not present in Vehicle, and the
abstract responsibilites of Vehicle, will be implemented in the child
classes.
3. Links to sample solution files: CastMember.java Dorothy.java Lion.java Oz.java Scarecrow.java