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

Object-Oriented Programming in Java. MISM/MSIT 95-713. Homework 2. Due:
September 19, 2005. There are 4 exercises in this homework, each 25 points. For
this homework assignment and for all the upcoming homework assignments,
follow the commenting and coding convention discussed in the class, unless ...

Part of the document


Object-Oriented Programming in Java
MISM/MSIT 95-713
Homework 2
Due: September 19, 2005
There are 4 exercises in this homework, each 25 points.
For this homework assignment and for all the upcoming homework assignments,
follow the commenting and coding convention discussed in the class, unless
otherwise specified explicitly. Make sure you follow the minimal class
description guidelines 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. Put all your
java files into a zip file named Homework2.zip and submit it via the Drop
Box on the blackboard before the beginning of the class on the due date.
Also, print all your java files with your name and bring them to me at the
beginning of the class on the due date.
1. Write a Java class for representing a work order in a warehouse shipment
system. The class should be named as WorkOrder and should contain the
following fields:
. A static integer for the next id. It should be initialized to 1 and
should be incremented with each WorkOrder object creation.
. An integer for the id of the WorkOrder object. It will be assigned
to the next id.
. A String for customer name.
. A String for part id.
. An integer for quantity.
. A String for the status.
. A Date for the order date.
. A Date for the shipment date.
Define two constructors: The default constructor, and another constructor
that takes customer name, part id and the quantity. In both the
constructors, the order date should be set to the current date of the
creation of the work order object, and the shipment date should be set to
three days from the order date. Provide getter methods for all the
fields. In the getter method of the order date, return a clone of it so
that it cannot be modified outside of the object. Provide setter methods
for members that can be modified after the work order object is created.
Define a Homework2_1 class with a main function. In the main function,
create several work order objects and print them out utilizing their
toString method. Have all your code in file Homework2_1.java.
2. Define a Rectangle class that abstracts a geometric rectangle. It should
have four integers, x1, y1, x2, y2 where (x1,y1) is the upper left corner
and (x2,y2) is the lower right. You may assume the coordinates are
provided properly. Define the following methods:
. area() returns the area of the rectangle.
. length() returns the length of the perimeter of the rectangle.
. move() moves the rectangle by a deltaX and a deltaY integers. Note
that deltaX and deltaY can be negative. If the rectangle
coordinates go below zero with the move operation, make those
coordinates equal to zero.
. Overloaded contains() methods. The first one takes x and y integers
and returns if (x,y) is contained in the rectangle. The second
contains() method takes another rectangle object and returns true
if it is included in the rectangle.
. findIntersection() takes a rectangle object and find its
intersection with itself. If the other rectangle doesn't intersect
with itself, it returns a rectangle object with all 0 coordinates.
. equals() method checks if another rectangle is equal to itself
(i.e. all coordinates are equal).
Define a Homework2_2 class with a main function. In the main function,
create rectangle objects and invoke their methods. Have all your code in
file Homework2_2.java.
3. Define a RationalNumber class to abstract rational numbers. It should
have a long numerator and a long denominator. Define the following
methods:
. add() takes a rational number object as the parameter and returns a
new rational number which is the addition of the parameter and
itself. Similarly, define multiply(), subtract() and divide().
. invert() returns the inversion of the rational number (i.e. 1/a).
Don't worry about reducing the resulting rational number after addition
or multiplication. However, if you want to do it, you may use the gcd()
method of the BigInteger class. Doing this will not get you additional
credit.
Define a Homework2_3 class with a main function. In the main function,
create rational number objects and invoke their methods. Have all your
code in file Homework2_3.java.
4. Consider the UML conceptual model shown in Figure 1. Write class
definitions for each of the concepts.

[pic]

Figure 1 UML Conceptual Model for Exercise 4

ReciptPrinter has one method, printReceipt(), which simply prints the
parameter.
CardReader has the following methods:
. readCard() returns a string, say "ABC123".
. confiscateCard() simply prints out a message.
. releaseCard simply prints out a message.
CashDispenser has one method, dispenseCash(), which takes an integer
amount as the parameter and prints a message.
DepositUnit has one method, takeDepositEnvelope(), which prints a
message.
IOUnit defines a Transaction enum with objects of DEPOSIT, WITHDRAW,
QUERY and CANCEL. It has the following methods:
. obtainCustomerPin() prompts the user for a pin, reads and returns
it.
. obtainTransaction() prompts the user to select a transaction from a
menu and returns the transaction.
. obtainAmount() prompts the user for an amount and returns it.
BankServer has the following methods:
. verifyPin() returns always true.
. doWithdraw() and doDeposit() don't do anything.
. doQuery() returns 100.0.
Atm has the following methods:
. Constructor that takes a BankServer object reference.
. handleTransaction() handles a user transaction. It executes the
sequence of following:
o read card
o obtain customer pin
o verify pin with the bank server
o obtain transaction type
o if transaction is DEPOSIT
. obtain amount
. take deposit envelope
. send doDeposit message to bank server with card number
and amount
. print receipt
o if transaction is WITHDRAW
. obtain amount
. send doWithdraw message to bank server with card number
and amount
. dispense cash
. print receipt
o if transaction is QUERY
. send doQuery message to bank server with card number
. print receipt with query amount
o if transaction is CANCEL
. print receipt saying transaction cancelled
o release card at the end of any transaction.
Define a Homework2_4 class with a main function. In the main function,
create a bank server and an atm object. Invoke handleTransaction on the
atm object. Have all your code in file Homework2_4.java.