Object-Oriented Programming in Java - Andrew.cmu.edu

Implement the inheritance hierarchy shown in Figure 1 in Java[1]. Have Point class, Circle class and Cylinder class in a single file named Homework3_1.java. Have the Homework3_1 class to create point, circle and cylinder objects, print them out via their toString() methods.

Part of the document


Object-Oriented Programming in Java
MISM/MSIT 95-712-C
Homework 3
Due: Wednesday, October 6, 2004, start of class
There are 3 exercises in this homework. The first two exercises are 25
points. The third exercise is 50 points.
Follow the commenting and coding convention, and the minimal class
description guideline as discussed in the lectures for all the classes you
introduce, except for the test driver classes that contain the main
function.
Name your files as specified in each of the exercises below. Generate the
Java documentation by running the javadoc utility over all the classes
including the ones in the package of exercise 3. Put all your java, the
compiled class files and the documentation files into a zip file named
Homework3.zip and submit it via the Drop Box on the blackboard before the
beginning of the class on October 6, 2004. Also, print all your .java files
with your name and bring them to me at the beginning of the class on
October 6, 2004. Don't print nor bring the documentation files.
1. Implement the inheritance hierarchy shown in Figure 1 in Java[1]. Have
Point class, Circle class and Cylinder class in a single file named
Homework3_1.java. Have the Homework3_1 class to create point, circle
and cylinder objects, print them out via their toString() methods.
Invoke the area() method on circle object, invoke area() and volume()
methods on cylinder object, and print all the results.
[pic]

Figure 1 Inheritance Hierarchy for Exercise 1

2. Implement the inheritance hierarchy shown in Figure 2 in Java. Have
Account and SavingAccount classes in a single file named Homework3_2.
The defaultInterestRate field in the SavingAccount class is static
with an initial value of 2.5. The setDefaultInterestRate() is a static
function.
Have the Homework3_2 class to create objects of Account and
SavingAcount classes, invoke deposit and withdraw methods on them, and
invoke the setDefaultInterestRate() and the applyMonthlyInterest() on
the SavingAccount object.
[pic]

Figure 2 Inheritance Hierarchy for Exercise 2

3. Consider the UML conceptual model shown in Figure 3. You will be
writing class definitions for each of the concepts and additionally
for the test driver program following the instructions below.
[pic]

Figure 3 Conceptual Model for Exercise 4

Add the class definitions of OrderItem, Order and RushOrder classes in
their separate .java files but all within the edu.cmu.heinz.oop95712c
package. Note that all of these classes are public.
OrderItem class has a String upc, an integer quantity and an integer
price, all private. The getCost() method returns the multiplication of
its quantity and price.
Order class has a list of OrderItem objects. The addOrderItem() method
takes an object of OrderItem as the parameter and stores it in the
list. The getTotal() method returns the total cost of all order items
in the order. The printOrderItems() method prints information about
each order item via the toString() method of the OrderItem class.
RushOrder class extends the Order class. It has an integer instance
variable deliveryDay, a protected member, which represents in how many
days the order should be delivered. The getTotal() method overrides
the definition of the super class in the following way: It first
invokes the getTotal() of the super class to find the total for all
items in the order. It then adds the delivery charge. The delivery
charge for one day delivery is $25, for two day delivery is $15, for
three day delivery is $10. It is free for four or more days. Note
that, the delivery charge should be added only if there are items in
the order. It means if the getTotal() of the super class returns 0, do
not add delivery charge, but just return 0.
Introduce a Homework3_3 class as a test driver in the default package.
It should execute as follows:
i. Create an array of four Order objects, calling it orders. The
first object is an instance of Order class, the second is an
instance of RushOrder class with one day delivery, the third
object is an instance of RushOrder class with two day delivery
and the forth object is an instance of RushOrder class with
three day delivery.
ii. Prompt the user to enter the UPC code for an item or "done" to
quit. Read the user input. You can do this with JOptionPane as
before.
o While the user enter a UPC (anything other than "done"),
prompt for and read the quantity. You may assume the user
enters a valid input, that is, an integer.
o Generate a random number from 50 to 100 for the price.
o Create an OrderItem object with UPC, quantity and price.
o Generate a random number from 1 to 7 for the delivery day.
o Add the order item to the element of the orders array
corresponding to the delivery day via the addOrderItem().
If delivery day is four or bigger, add the order item into
the first element, which is the instance of Order class. If
the delivery day is one, add it to the second element of
the orders array, which is the instance of the RushOrder
class with one day delivery, and so on.
o Continue the loop until either user enters "done".
iii. For each order in the orders array:
o Print out the type of order via the toString() methods.
o Print out all the order items.
o Print out the subtotal for this order.
iv. Print out the total cost in all of the orders.
-----------------------
[1] This inheritance hierarchy takes advantage of code reuse. However, it
doesn't necessarily represent the is-a relation.