Assignment #3: Inheritance
The assignment consists of two 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.
- After you have done this, add code that handles the following:
Some tickets can be upgraded.
If a ticket is Upgradeable
it has a method named upgrade.
Invoking upgrade will print the
message "you've been upgraded!".
Design an appropriate interface, and make StudentAdvanceTicket
and WalkupTicket upgradeable.
- 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.
Also invoke upgrade on each appropriate ticket is the array.
Part II: 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.