(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.
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.
Develop the method smallerThan
that determines whether the
file is smaller than some maximum size that can be mailed as an attachment.
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.
Develop the method unitPrice
that computes the unit price of
some grocery item. The unit price is given in cents per unit of weight.
Develop the method lowerPrice
that determines whether the
unit price of some grocery item is lower than some amount.
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.
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.
passengers in a cab just pay flat fee per mile
passengers in a limo must pay at least the minimum rental fee, otherwise they pay by the mile
passengers in a van pay $1.00 extra for each passenger
Develop the method
lowerPrice
that determines whether the fare for a given number of miles is lower than some amount.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.
Develop the class hierarchy to represent books in the discount bookstore.
Develop the method
salePrice
that computes the sale price of each book.Develop the method
cheaperThan
that determines whether on book is cheaper than another book.Develop the method
sameAuthor
that determines whether some book was written by the specified author. Solution