6  Lab Wednesday pm:

Methods for Unions

(12.1.1, 12.1.2, 12.1.3, 12.1.4) - do two of these

Recall problem 2: that described a revision of the problem in exercise 2:
Problem Develop class definitions for a collection of classes that represent several different kinds of files. All files have name and size given in bytes. Image files (gif) also include information about the height and width of the image, and the quality of the image. Text files (txt) include information about the number of characters, words, and lines. Sound files (mp3) include information about the playing time of the recording, given in seconds.
  1. Develop the method timeToDownload that computes how long it takes to download a file at some network connection speed, typically given in bytes per second. The size of the file is given in bytes.

  2. Develop the method smallerThan that determines whether the file is smaller than some maximum size that can be mailed as an attachment.

  3. Develop the method sameName that determines whether the name of a file is the same as some specified name.  Solution

Recall the problem  fig:grocery-classes:
Problem Develop a program that keeps track of the items in the grocery store. For now, we assume that the store deals only with ice cream, coffee, and juice. Each of the items is specified by its brand name, weight and price. Each coffee is also labelled as either regular or decaffeinated. Juice items come in different flavors, and can be packaged as frozen, fresh, bottled, or canned. Each package of ice cream specifies its flavor and whether this is a sorbet, a frozen yogurt, or regular ice cream.
  1. Develop the method unitPrice that computes the unit price of some grocery item. The unit price is given in cents per unit of weight.

  2. Develop the method lowerPrice that determines whether the unit price of some grocery item is lower than some amount.

  3. Develop the method cheaperThan that determines whether one grocery item is cheaper than some other one, in terms of the unit cost.  Solution

Recall the exercise  2: Take a look at the data definition in figure  6.
                 +---------------------+
                 | ATaxiVehicle        |
                 +---------------------+
                 | int idNum           |
                 | int passangers      |
                 | int pricePerMile    |
                 +---------------------+
                         / \
                         ---
                          |
      +----------------+--+-----------------+
      |                |                    |
 +--------+     +---------------+   +----------------+
 | Cab    |     | Limo          |   | Van            |
 +--------+     +---------------+   +----------------+
 |        |     | int minRental |   | boolean access |
 +--------+     +---------------+   +----------------+

Figure: A class diagram for taxis

Translate it into a collection of classes. Also create instances of each class.
  1. Develop the method fare that computes the fare in a given vehicle, based on the number of miles travelled, and using the following formulas for different vehicles.

    1. passengers in a cab just pay flat fee per mile

    2. passengers in a limo must pay at least the minimum rental fee, otherwise they pay by the mile

    3. passengers in a van pay $1.00 extra for each passenger

  2. Develop the method lowerPrice that determines whether the fare for a given number of miles is lower than some amount.

  3. Develop the method cheaperThan that determines whether the fare in one vehicle is lower than the fare in another vehicle for the same number of miles.  Solution

Consider the following revision of the problem in exercise 2:
Problem Develop a program that assists a bookstore manager in a discount bookstore. The program should keep a record for each book. The record must include its title, the author's name, its price, and its publication year. In addition, the books There are three kinds of books with different pricing policy. The hardcover books are sold at 20% off. The sale books are sold at 50% off. The paperbacks are sold at the list price.
  1. Develop the class hierarchy to represent books in the discount bookstore.

  2. Develop the method salePrice that computes the sale price of each book.

  3. Develop the method cheaperThan that determines whether on book is cheaper than another book.

  4. Develop the method sameAuthor that determines whether some book was written by the specified author.  Solution