CS 211
Data Structures


Stonehill College           CS 211


Assignment 1
Due : Sept  18, 2007

 

1.      A Polymorphic Video Store

Your friend Electronic Eddie has decided to open a business that rents movies and games. Unfortunately, Eddie has very little startup money and cannot afford to buy the latest software package to manage his inventory.  As a programmer without peer, you have come to Eddie’s rescue and have volunteered to write a system for his business.

Your first step is to design a class hierarchy which includes the following classes:

Item  with the following attributes:

a 3-digit ID number (String)
a title (String)
rental price (double)
status (boolean: true(1) if in stock, false(0) if currently rented)
the current renter’s name (String).

The methods of  Item are the standard getter and setter methods as well as method
                              
                   
void display() 

which  displays the string "Not applicable"                              

Game (extends Item) with the following additional attributes:
    age level (an integer from 3 to 16, 16 signifies 16+ )

Movie  (extends Item) with the following additional attributes:

playing time in minutes (int)
rating : G, PG, PG13, or R (String)
format: ‘V’ for VHS cassette, ‘D’ for DVD (char)

Each class implements a display() method that prints all the data of the invoking object .

Once you have implemented the above classes, you should design and implement a program that utilizes the Item hierarchy.  Your system should be menu-driven and include the following options:

a.        Check out an item.
Your system should query the user for the ID number of the item and the renter’s name.  If the item is already checked out your system should say so.

b.       Check in an item.
Your system should ask for the ID number of the item. If it is already checked in, indicate that.

c.        Search for an item by ID number to see if it is in stock.
You should use binary search for this option.  Consequently, all rental items are kept sorted by ID number.

d.       Search for an item by title.
Since the rentals are not sorted by title, you might use sequential search here.
If the title is not in the inventory, say so.

e.        Display the entire inventory, sorted by ID.

f.         Add a new item to the inventory.

g.       Delete an item from the inventory (equivalent to selling a used video or game).
Ask for the ID number of the item to be deleted. If the ID doesn’t match one of the items in inventory, a message  should be printed.

h.       Display the menu

i.         Exit

When the program begins, the program should

You must create a file, say data.txt with  30 items in it -- 10 games and  20 movies.  Use one line in a file for each item.  The first entry on each line should be either G or M.  Do not include spaces in the title of the movie or the renter's name, use dashes instead.  A typical entry
for movies has the format
   

M id title fee running-time rating DVDorVHS InorOut renter

For Games an entry has the form

 G id title fee age INorOutrenter

A renter's name appears only if the movie or game is checked out.

Two typical entries are:
M 112 The-Bourne-Ultimatum 3.75 123 PG13 D 0 Agnes-Gooche
G 107 The-Sims-2  4.50 12 1

 

2.      Distance in Polymorphictown

In the bustling metropolis of Polymorphictown, where streets are laid out in a grid-like fashion and each city block is a .1mile square, “distance” is a relative matter.

 

 

For example, straight-line (Euclidean)  distance (“as the crow flies”) from the corner of 42nd St. and 11th Ave to the corner of 46th St. and 8th Ave is just 5 blocks or ˝ mile which is the length of the line segment joining the two corner points.  You can easily calculate this distance using the Pythagorean theorem. Such a measure of distance is called the Euclidean distance.

 

 

On the other hand, a Polymorphictown taxi driver calculates the “distance” between those same corner points as 7 blocks or .7 miles. We’ll call this measure the taxi distance.  (Note that more than one route with distance 7 blocks is possible.)

 

 

 

 

Moreover, for Polymorphictown cyclists, “distance” has yet another interpretation.

In ecological Polymorphictown, two bicycle paths crisscross every city block along the diagonals.  Using Pythagoras’ theorem, you can calculate that the length of each bike path is  blocks  or  miles.


 

 

Cyclists (or skaters or pedestrians) usually travel along adjacent bike paths as far as possible and then continue on city streets.  So to a cyclist, the distance between those same locations is  blocks or   miles . We’ll call such a metric, the bicycle distance.

 

 

 

Write a program that calculates the distance between any two corner locations in Polymorphictown Of course, this distance differs for taxi drivers, cyclists and soaring pigeons.   Your program should also display directions from the starting location to the destination. The directions are not unique.  Any set directions that give the minimum distance is OK. Below is sample output from the program.   Assume streets are numbered from 1 to 100 and avenues from 1 to  12. 

Sample output:

Enter T or t for taxi; C or c  for cyclists;  E or e for Euclidean: T

Directions?  Y  or y  for Yes: Y

Start location: 42  11

End location: 46   8

Distance is 7 blocks or .7 miles

Directions:

42 11

42 10

42 9

42 8

43 8

44 8

45 8

46 8

 

Again? Y or y for yes: Y

 

Enter T or t for Taxi; C or c  for cyclists;  E or e for Euclidean: C

Directions?  Y  or y for Yes: Y

Start location: 42  11

End location: 46   8

Distance is  5.243 blocks or .5243 miles

Directions:

                        42 11

                        43 10

                        44 9

                        45 8

                             46 8

 

Again? Y or y for yes: Y

 

Enter T or t for Taxi; C or c  for cyclists;  E or e for Euclidean: E

Directions?  Y or y  for Yes: Y

Start location: 42  11

End location: 46   8

Distance is 5 blocks or .5 miles

Directions:

                        42 11

                        46 8

Comment: These directions just give start and end.

Again? Y or y for yes: N

Hints:

 

a.             Define class StreetCorner with data
              int street, avenue

         Include a dummy method
     double distance (Corner  place)
    
void directions(Corner place)

b.   Define three concrete classes, TaxiCorner, BicycleCorner, and Euclid Corner that inherit Corner.
    Each class implements its own version of distance and directions