5. For the next four problems, consider the task of representing types of ticket
ID: 665681 • Letter: 5
Question
5. For the next four problems, consider the task of representing types of tickets to campus events. Each ticket hasa unique number and a price. There are three types of tickets: walk-up tickets, advance tickets, and student advance tickets. Figure 9.10 illustrates the types: Walk-up tickets are purchased the day of the event and cost $50 Advance tickets purchased 10 or more days before the event cost $30, and advance tickets purchased fewer than 10 days before the event cost S40. Student advance tickets are sold at half the price of normal advance tickets: When they are purchased 10 or more days early they cost $15, and when they are purchased fewer than 10 days early they cost $20 Implement a class called Ticket that will serve as the superclass for all three types of tickets. Define all common operations in this class, and specify all differing operations in such a way that every subclass must implement them No actual objects of type Ticket will be created: Each actual ticket will be an object of a subclass type. Define the following operati ions: The ability to construct a ticket by number The ability to ask for a ticket's price. ·The ability to print In a ticket object as a string. An example string would be "Number: 17, Price: 50·0" Ticket Ticket(number) getPrice0 toString0 Walkup Ticket Advance Ticket Student Advance Ticket Figure 9.10 Classes of tickets that are available to campus events 644 Chapter 9 Inheritance and Interfaces 6. Implement a class called WalkupTicket to represent a walk-up event ticket. Walk-up tickets are also constructed by number, and they have a price of S50 7. Implement a class called AdvanceTicket to represent tickets purchased in advance. An advance ticket is con structed with a ticket number and with the number of days in advance that the ticket was purchased. Advance tickets purchased 10 or more days before the event cost $30, and advance tickets purchased fewer than 10 days before the event cost $40 8. Implement a class called StudentAdvanceTicket to represent tickets purchased in advance by students. A student advance ticket is constructed with a ticket number and with the number of days in advance that the ticket was pur- chased. Student advance tickets purchased 10 or more days before the event cost $15, and student advance tickets purchased fewer than 10 days before the event cost $20 (half of a normal advance ticket). When a student advance ticket is printed, the string should mention that the student must show his or her student ID (for example, "Number: 17, Price: 15.0 (ID required) ").Explanation / Answer
Ticket.java
public abstract class Ticket {
private int number;
protected double price;
public Ticket(int number) {
this.number = number;
}
public double getPrice() {
return price;
}
public String toString() {
return "Number "+number+", price: "+price;
}
public static void main(String[] args) {
// Some testing code
Ticket sat = new StudentAdvanceTicket(1,11);
Ticket satLate = new StudentAdvanceTicket(2,9);
Ticket wt = new WalkupTicket(3);
Ticket at = new AdvanceTicket(4,10);
System.out.println("price of ticket purchased 10 days in advance: "+at.getPrice());
System.out.println(sat);
System.out.println(satLate);
System.out.println(wt);
System.out.println(at);
}
}
-------------------------------
AdvanceTicket.java
public class AdvanceTicket extends Ticket {
public AdvanceTicket(int number,int daysInAdvance) {
super(number);
if (daysInAdvance < 10) {
price = 40;
} else {
price = 30;
}
}
}
----------------------------------
StudentAdvanceTicket.java
public class StudentAdvanceTicket extends AdvanceTicket {
public StudentAdvanceTicket(int number, int daysInAdvance) {
super(number, daysInAdvance);
price = price/2;
}
}
--------------------------------
WalkupTicket.java
public class WalkupTicket extends Ticket {
public WalkupTicket(int number) {
super(number);
price = 50;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.