2  Lab Monday pm:

Goals

Learn to make data definitions
Learn to use ProfJ

Simple classes

(2.1.1, 2.1.2, 2.1.3, 2.1.4) - do two of these

Translate the three examples of information from the GPS problem into instances of GPSLocation.

Also interpret new GPSLocation(50.288,0.11) in this context. Solution

Take a look at this problem statement:

Develop a program that assists a bookstore manager. The program should keep a record for each book. The record must include its book, the author's name, its price, and its publication year.

Develop an appropriate data definition and implement the definition with a class. Create instances of the class to represent these three books:

  1. Daniel Defoe, Robinson Crusoe, $15.50, 1719;

  2. Joseph Conrad, Heart of Darkness, $12.80, 1902;

  3. Pat Conroy, Beach Music, $9.50, 1996. Solution

Study this class definition:
class Image {
 int height /* pixels */;
 int width /* pixels */;
 String source /* file name */;
 String quality /* informal */;

 Image(int height, int width, String source, String quality) {
  this.height = height;
  this.width  = width ;
  this.source = source;
  this.quality = quality;
 }
}

Draw the class diagram.

The class definition was developed in response to this problem statement:

Develop a program that creates a gallery from image descriptions. Those specify the height, the width, the source of the images, and, for aesthetic reasons, some informal information about its quality.

Interpret the following three instances in this context:

new Image(5, 10, "small.gif", "low");
new Image(120, 200, "med.gif", "low");
new Image(1200, 1000, "large.gif", "high"); $\mbox{\solution{data-pics}\eoe}$


+-------------------------+
| Car                     |
+-------------------------+
| String model            |
| int price /* dollars */ |
| double milage           |
| boolean used            |
+-------------------------+

Figure: A class diagram for cars

Translate the class diagram in figure 2 into a class definition. Also create instances of the class. Solution

Classes with containment

(3.1.1, 3.1.2, 3.1.3) - do two of these


+-----------------------------+
| WeatherRecord               |
+-----------------------------+
| Date d                      |-----------------+
| TemperatureRange today      |--+              |
| TemperatureRange normal     |--+              |
| TemperatureRange record     |--+              |
| double precipitation        |  |              |
+-----------------------------+  |              |
                                 v              v
              +---------------------+   +------------+
              | TemperatureRange    |   | Date       |
              +---------------------+   +------------+
              | int high            |   | int day    |
              | int low             |   | int month  |
              +---------------------+   | int year   |
                                        +------------+

Figure: A class diagram for weather records

Develop a data definition and Java classes for this problem:
Develop a ``real estate assistant'' program. The ``assistant'' helps the real estate agent locate houses of interest for clients. The information about a house includes its kind, the number of rooms, its address, and the asking price. An address consists of a street number, a street name, and a city.

Represent the following examples using your classes:

  1. Ranch, 7 rooms, $375,000, 23 Maple Street, Brookline;

  2. Colonial, 9 rooms, $450,000, 5 Joye Road, Newton; and

  3. Cape, 6 rooms, $235,000, 83 Winslow Road, Waltham. Solution

Take a look at the data definition in figure 2. Translate it into a collection of classes. Also create examples of weather record information and translate them into instances of the matching class. Solution

Revise the data representation for the book store assistant in exercise 2 so that the program keeps track of an author's year of birth in addition to its name. Modify class diagram, the class definition, and the examples. Solution

Union

(4.1.2, 4.1.3, 4.1.4) - do two of these

Consider a revision of the problem statement in exercise 2:
Develop a program that creates a gallery from three different kinds of records: images (gif), texts (txt), and sounds (mp3). All have names for source files and sizes (number of bytes). Images also include information about the height, the width, and the quality of the image. Texts specify the number of lines needed for visual representation. Sounds include information about the playing time of the recording, given in seconds.

Develop a data definition and Java classes for representing these records. Then represent these three examples with Java objects:

  1. an image, stored in flower.gif; size: 57,234 bytes; width: 100 pixels; height: 50 pixels; quality: medium;

  2. a text, stored in welcome.txt; size: 5,312 bytes; 830 lines;

  3. a music piece, stored in theme.mp3; size: 40960 bytes, playing time 3 minutes and 20 seconds. Solution

Take a look at the data definition in figure 2.
                   +---------------------+
                   | 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. Solution

Draw a UML diagram for the classes in figure 2Solution
abstract class AMuseTicket {
Date d;
int price;
}

class MuseAdm
 extends AMuseTicket {
 MuseAdm(Date d,
         int price) {
  this.d = d;
  this.price = price;
 }
}










class OmniMax
 extends AMuseTicket {
 ClockTime t;
 String title;

 OmniMax(Date d,
         int price,
         ClockTime t,
         String title) {
  this.d = d;
  this.price = price;
  this.t = t;
  this.title = title;
 }
}



class LaserShow
 extends AMuseTicket {
 ClockTime t;
 String row;
 int seat;

 LaserShow(Date d,
         int price,
         ClockTime t,
         String row,
         int seat) {
  this.d = d;
  this.price = price;
  this.t = t;
  this.row = row;
  this.seat = seat;
 }
}
Figure: Some classes