UW Home     CSE Home   Announcements    Message Board    Contact Info 

 
 

Homework #4

Due: Online by Wednesday, February 18, by 9:00 p.m. No late asssignments will be accepted.

For this assignment, design and implement the classes described in the following problems. When you are done, you should turn in the Java source files electronically over the web.

0. Download the skeleton files for the homework from here.

1.         Many computer systems are used to model moving objects.  Objects in motion have a direction and a speed.  These qualities can be modeled using a velocity vector that has a direction and a length.  In this problem you will implement a new class Velocity that can be used to model a changing velocity vector.  Implement the constructor and all the following methods.  Read the javadoc specification for this class (provided to you in the download file) for details of the implementation.  The specific formulas that you need are given in the specification.

Velocity(double deltaX, double deltaY)
          Initialize a new Velocity object, remembering the given (dx,dy) direction.
void bounceHorizontal()
          Bounce the vector as though it were bouncing off of a wall.
void bounceVertical()
          Bounce the vector as though it were bouncing off of the floor or the ceiling.
double getDX()
          Get the change in x per unit time represented by this vector.
double getDY()
          Get the change in y per unit time represented by this vector.

2.         Further operations that you might need for Velocity vectors are rotate() (a command) and magnitude() (a query).  Implement the following two methods in your Velocity class.  Again, the specific calculations that you need are given in the specification.

double magnitude()
          Return the magnitude (the length) of this vector.
void rotate(double degrees)
          Turn the direction vector through a counter-clockwise angle specified in degrees.

3.         You have been provided with a simple Rock class.  It models a rock that has a mass.  The only thing that you can do with these Rocks is ask them how much their mass is using the getMass() method.  We will imagine that these rocks are on Mars and that our job is to carry them around with a Rover.  Your task is to write a class Rover that has a certain carrying capacity and whose method verifyLoad() can take an ArrayList of Rocks and verify that it can carry the rocks without exceeding its capacity.  This is a simple task, it is not a trick question.  See the specification for a little more detail on the implementation.

Rover(double capacity)
          Initialize a new Rover object using the given capacity in kilograms.
boolean verifyLoad(ArrayList rocks)
          Check that we can in fact take the load of rocks that has been selected for us.

4.         You have been provided with a simple BirdFeeder class.  A BirdFeeder contains a certain amount of food, and it should have a method feedTheBirds(int serving)that counts how many birds could be fed using a particular serving size.  Your task is to implement that method.  Be sure to read the method details in the specification carefully.

BirdFeeder(int initialLevel)
          Construct a new bird feeder with the given amount of food.
int feedTheBirds(int serving)
          Feed each bird the serving size each until the food is all gone.
int getFoodLevel()
          Return the amount of food in the feeder.
void setFoodLevel(int amount)
          Fill the feeder.

5.         You have been provided with a simple Car class that models cars with a brand name and a model year.  Your task is to write a ParkingLot class that can manage a list of Cars, adding and removing them from a parking lot.  It has an infinite number of parking spots, but it's a little inconvenient to use because the most recent Car to arrive is the only one that can leave.  After it is gone, then the Car that arrived before it can leave, and so on.  Thus, the first Car to arrive cannot leave until all other Cars have left.  Read the method details in the specification to fully understand the operation of the class.

a.              Implement:

ParkingLot()
            A new ParkingLot object does not have any cars on it, initialize appropriately.
int getCarCount()
            Return the number of Cars presently in the ParkingLot.
void parkCar(Car car)
            Park one Car in the lot (ie, add it to the end of the list of cars currently in the lot).

b.            Implement:

void parkCar(ArrayList cars)
            Park several Cars in the lot (ie, add them to the end of the list of cars currently in the lot).
Car unparkCar()
            Take the last car that was parked out of the lot.

What to Turn In

Use the this online turnin form to turn in the files containing the Java classes that give your solution to these problems.