Big Java / Java Concepts Lab 17

Discovering Classes


1.

You have been given an assignment to write a program to schedule classrooms in your college's building. Classrooms can be reserved for classes, seminars, guest speakers, and for meetings for clubs (such as the Java Users Group). Clubs can meet only after 5 p.m. and only if the room is not needed by any other group. Each group needing a classroom is given a priority. Classes in your college have the highest priority, with clubs having the lowest priority. Some classes in your college have higher priority over others, especially if the instructor has tenure. Classrooms are characterized by size, amenities (overhead projectors, etc.) and comfortable seats. Groups can specifically request a room by room number or simply give a description of what they need. Groups also may request specific time slots or give a range of acceptable time slots. Rooms are not available after 10 p.m. and before 5 a.m.

Make a list of classes that you may need for your program.


2.

From your list of potential classes in your classroom-scheduling program, pick three potential classes and identify their main responsibilities.


3.

A CRC card describes a class, its responsibilities, and its collaborating classes. You already identified the classes and responsibilities in the classroom-scheduling program. Now you will identify the collaborating classes.

From your list of potential classes in your classroom-scheduling program, pick three potential classes and identify their collaborators.

Relationships Between Classes

4.

Using the list of classes for your classroom-scheduling program, identify any objects that may use inheritance. List the superclasses (if any) followed by their subclasses. If you see no need for inheritance, explain.


5.

Inheritance can be described as an "is-a" relationship between classes. Aggregation can be described as a "has-a" relationship. Identify any aggregation relations in the classroom scheduling problem.


6.

Identify the relationships between classes in the following code:

class GroceryBag extends BasicContainer
{
    private Fruit[] fruits;
    private Vegetable[] veggies;

. . .

    public void addItem(Food item)
    {
       . . .
    }

    public Money computeTotal()
    {
       . . .
    }

    . . .

}

Inheritance

From which classes does GroceryBag inherit?

Aggregation

Identify any aggregation relationships (has-a relationship).

Dependency

On which classes does GroceryBag depend (uses relationship)?

Case Study

7.

In this case study, you will develop a program to be used for ordering from an online pizza restaurant. Allow the user the following choices: pizza size (small, medium, or large), pizza crust type (thin or thick), pizza toppings, as well as small, medium, and large drinks. The user may select specific toppings. A user can order multiple pizzas and drinks. The order should include the customer's delivery address. After taking the order, print an order slip that shows the order information and total price.

Each topping has a price (for example, onions $1 and pepperoni $2). The price of a pizza is the sum of the base price ($10 for a small pizza, $13 for a medium, and $16 for a large pizza), plus the sum of the topping prices. The crust type doesn't affect the price.

The price of a drink is the base price of the drink ($1 for soda, $2 for beer) multiplied by a size factor (0.8 for small, 1.0 for medium, 1.2 for large drinks).

The delivery charge is $3. It is waived for pickup orders.

What classes can you identify using the "nouns from the requirements specification" technique?


8.

Not all of the classes are immediately visible in the requirements specification. In our example, a pizza and a drink have common properties:

  • the price
  • the description that is printed on the order slip
In fact, if the restaurant were to start serving other items (such as bread sticks or sandwiches), those items also would have a price and a description.

Define a suitable class that captures this commonality, and indicate how the classes Pizza and Drink are related to this class.


9.

What are the responsibilities and collaborators of the Order class? 


10.

You probably decided you need a class Topping. What is the code for your Topping class? If you think you do not need a class Topping, explain why.


11.

A very important class in your program is probably going to be called Pizza. What is the code for your Pizza class. If you think you need superclasses and/or subclasses of Pizza, supply the code for these classes too.


12.

Supply the code for your Drink class.


13.

An order should contain a list of items (pizzas or drinks), so that the total price can be calculated and the order can be formatted to be printed. Supply the code for your Order class here.


14.

Supply the code of any other classes you need to complete your program.