Assignment #3: Inheritance
The assignment consists of three completely separate parts.
Part I: Forming an Inheritance Hierarchy
A set of classes is used to handle the different ticket types for a theater.
All tickets have a unique serial number that is assigned
when the ticket is constructed and a price.
There are three basic types of tickets:
Ticket type |
Description |
Sample toString Output |
WalkupTicket |
These tickets are purchased on the day of the event for $50 |
SN: 314, $50 |
AdvanceTicket |
Tickets purchased ten or more days in advance cost $30. Tickets
purchased fewer than ten days in advance cost $40. |
SN: 612, $40 |
StudentAdvanceTicket |
These are AdvanceTickets that cost half of what
an AdvanceTicket would normally cost. |
SN: 59, $15 (student) |
- Design a class hierarchy that encompasses the above three classes, and
adds an abstract class called Ticket.
- Implement the Ticket abstract class.
This class should store a serial number as its private data, and
the constructor should initialize the serial number.
Also keep a static variable lastSerialNumber that can
keep track of the last serial number assigned (so you can avoid
duplicates).
Provide an appropriate abstract method (HINT: what can ALL tickets do?),
and also provide an implementation of toString that prints
the serial number and price information.
- Implement the WalkupTicket class.
- Implement the AdvanceTicket class.
Provide a constructor that takes a parameter indicating the number of
days in advance that the ticket is being purchased. Recall that the
number of days of advanced purchase affects the ticket price.
- Implement the StudentAdvanceTicket class.
Provide a constructor that takes a parameter indicating the number of
days in advance that the ticket is being purchased. The toString
method should include a notation that this is a student ticket.
This ticket costs have of an Advanceticket. If the pricing scheme
for AdvanceTicket changes, the StudentAdvanceTicket
price should be computed correctly with no code modification to
the StudentAdvanceTicket class.
- Provide a test program that populates an array of Ticket
with all three kinds of tickets, and then calls a function that computes
the total cost of all the tickets in the array.
Part II: I/O and the Decorator Pattern
Do some simple I/O using the Java I/O library. You will read a file
that contains an unknown number of words into an
array of String.
Print out the exact number of words read.
The file is contained on the Internet.
There are two versions of the assignment. Do both.
Version #1
Version #1 of the program gets the file located at
http://www.cs.fiu.edu/~weiss/cop3337/assignments/dict.txt.
Here is some starter code (you will need to find the appropriate
import directives.
URL url = new URL( "http://www.cs.fiu.edu/~weiss/cop3337/assignments/dict.txt" );
InputStream is = url.openStream( );
Once you have the InputStream you can use a Scanner
to read
a line at a time. Then you can use ArrayList to store the strings.
Version #2
The URL http://www.cs.fiu.edu/~weiss/cop3337/assignments/dict.ser.gz
stores a String[]
in compressed gzip form.
Open up the URL, wrap inside a BufferedInputStream, then
a GZIPInputStream, and then a ObjectInputStream, and call
readObject. You will get the entire String array in
a single read.
Part III: Interfaces and Function Objects
Method transform
takes two identically sized-arrays of double as parameters:
input and output,
and a third parameter representing a function to
be applied to the input array.
For instance, in the following code fragment:
double [ ] input = { 1.0, -3.0, 5.0 };
double [ ] output1 = new double [ 3 ];
double [ ] output2 = new double [ 3 ];
double [ ] output3 = new double [ 4 ];
transform( input, output1, new ComputeSquare( ) );
transform( input, output2, new ComputeAbsoluteValue( ) );
transform( input, output3, new ComputeSquare( ) );
The intended result is that output1 contains
{ 1.0, 9.0, 25.0 },
output2 contains { 1.0, 3.0, 5.0 }, and the third call to
transform throws an IllegalArgumentException because the arrays
have different sizes.
Implement the following components:
-
An interface that will be used to specify the
third parameter to transform.
-
The transform method (which is a static method).
Remember to throw an exception if the
input and output arrays are not identically-sized.
-
The classes ComputeSquare and ComputeAbsoluteValue.