CS 261, Spring 2001
Assignment #4
(total = 16 pts., due on May 2, Wednesday)
- 1.
- (8 pts.)
You will make the SListG program better.
- (a)
- Add to class SListG a new member variable tail
that points to the last element in the list
as shown on viewgraph page List-3.
- (b)
- Reimplement the method void insertTail(String, Object)
so that it adds a new node at the end of the list
without traversing the list.
- (c)
- Add to class SListG the new method SLNodeG removeHead()
that removes the first node in the list and returns it.
- (d)
- Test your program with the requests stored in file
/nfs/stak/a2/classes/cs/minoura/cs261/javaProgs/slist/requests.txt.
- (e)
- Turn in hard copies of your program, with modified part clearly marked,
and of the output produced when your program is run as an application.
- (f)
- Put the new applet on the Web.
- 2.
- (8 pts.)
Assume that you operate a small rental-car agency
and must keep track of the available cars.
- (a)
- The cars are identified with numbers 0, ..., nCars - 1,
and the colors of the cars are designated by integers
Red = 0, Green = 1, and Blue = 2.
- (b)
- The available cars are managed by the object carPool,
which is an instance of class CarPool
and contains a linked list of nodes for available cars.
The list may be of any kind, singly-linked or doubly-linked.
- (c)
- The method int getCar(int color) of CarPool is called
to find a car of desired color and to remove it from the list of
available cars.
The integer returned by this method designates the car
to be rented.
If no car is available, -1 should be returned.
- (d)
- The method void returnCar(int i) of CarPool is called
when the car identified with number i is returned
and added to the list of available cars.
Test your program with the following conditions:
- Number of Cars
-
nCars = 4 with one red car, two green cars, and one blue car.
- Sequence of Requests
-
int i1 = carPool.getCar(Red);
carPool.returnCar(i1);
int i2 = carPool.getCar(Green);
int i3 = carPool.getCar(Blue);
int i4 = carPool.getCar(Green);
carPool.returnCar(i3);
int i5 = carPool.getCar(Blue);
carPool.returnCar(i4);
int i6 = carPool.getCar(Green);
int i7 = carPool.getCar(Blue); // error, return -1
int i8 = carPool.getCar(Red);
carPool.returnCar(i2);
int i9 = carPool.getCar(Red); // error, return -1
carPool.returnCar(i5);
carPool.returnCar(i6);
Toshimi Minoura
4/30/2001