Learn to use ProfessorJ
Define classes using the Insert Java Class widget
Make examples of data in the Java Interactions Box
Explore the use of the Test Case tool
Explore the use of the Interactions window
Design and use simple classes, classes with containment and unions:
Translate information into data definitions
Interpret data as information
Translate class diagram into class definitions
Represent Java classes as class diagrams
Book
class.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.
This example is included in you notes.
Use the Java class widget to define this class. Select add class diagram checkbox.
In the Interactions box make instances of this class that represent the following information:
Daniel Defoe, Robinson Crusoe, $15, 1719;
Joseph Conrad, Heart of Darkness, $12, 1902;
Pat Conroy, Beach Music, $9, 1996.
Use the Test Case tool to check that some of the fields of the instances you defined have the correct values.
Use the Interactions pane to make an instance of your favorite book. Try some tests similar to those done iin the Test Case tool.
Understanding data definitions.
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:
Image im1 = new Image(5, 10, "small.gif", "low"); Image im2 = new Image(120, 200, "med.gif", "low"); Image im3 = new Image(1200, 1000, "large.gif", "high");
What is the value of im2.quality
, of im3.height
?
+-------------------------+ | 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.
Classes with containment
Note: The solution to the exercise 3.1.3 is in your handout. You may use it as a guide. Remember to include a class diagram and examples of instances for each class you design.
+-----------------------------+ | 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:
Ranch, 7 rooms, $375,000, 23 Maple Street, Brookline;
Colonial, 9 rooms, $450,000, 5 Joye Road, Newton; and
Cape, 6 rooms, $235,000, 83 Winslow Road, Waltham.
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.
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.
Union
Defining classes from the given information.
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 three records. Then represent the following examples with Java objects:
an image, stored in the file flower.gif; size: 57,234 bytes; width: 100 pixels; height: 50 pixels; quality: medium;
a text, stored in welcome.txt; size: 5,312 bytes; 830 lines;
a music piece, stored in theme.mp3; size: 40960 bytes, playing time 3 minutes and 20 seconds.
Defining classes from the class diagrams.
Take a look at the data definition in figure 2. Translate it into a collection of classes. Also create instances of each class.
+---------------------+ | ATaxiVehicle | +---------------------+ | int idNum | | int passangers | | int pricePerMile | +---------------------+ / \ --- | +----------------+--+-----------------+ | | | +--------+ +---------------+ +----------------+ | Cab | | Limo | | Van | +--------+ +---------------+ +----------------+ | | | int minRental | | boolean access | +--------+ +---------------+ +----------------+
Figure: A class diagram for taxis
Representing class hierarchies as class diagrams.
Draw a UML diagram for the classes in figure 2.
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
You may want to think of other information that can be represented as simple classes, classes with containment, or union of classes. Write the problem description and ask you partner to design the classes. Or make the class diagram and ask your partner to make instances of these classes.