Question
The Concert Class 1. It will have two constructors that are identical as far as parameters are concerned to the Event class. 2. When the
The Concert Class 1. It will have two constructors that are identical as far as parameters are concerned to the Event class. 2. When the toString method is invoked, it must return the string with the word Concert placed in front of the string returned by the toString method of the Event class. A similar comment applies for the other two subclasses of Event as well.
EVENT CLASS
import java.util.Arrays;
/** * Represents a single event. It stores a description of the play, a unique id, * and the tickets sold for the play. * * */ public class Event { private String description; //dis of play public static final int CAPACITY = 5; //max seat for event private int ticketsSold; //num of tickets sold private int eventId; //unique id for event private double priceFactor; //factor used to multiply for this event private static int counter = 1; //counter private Ticket[] tickets; //to store all ticket objects
/** * Stores the description and price factor and assigns a unique id to the * event. The constructor also allocates the array tickets. * * @param description * a description of this Play * @param priceFactor * the price factor for this Play * */ public Event(String description, double priceFactor) { this.description = description; this.priceFactor = priceFactor; this.tickets = new Ticket[CAPACITY]; this.eventId = computeSerialNumber(); }
/** * Receives the description and stores that and a price factor of 1.0. * Besides, it assigns a unique id to the event. The constructor also * allocates the array tickets. * * @param description * a description of this Play * */ public Event(String description) { this(description, 1.0); }
/** * Returns the unique id of the play * * @return id of the play * */ public int getEventId() { return eventId; }
/** * Returns the tickets list * * @return the tickets list */ public Ticket[] getTickets() { return Arrays.copyOf(tickets, ticketsSold); }
/** * Sets the price factor for the event. * * @param priceFactor * the new price factor */ public void setPriceFactor(double priceFactor) { this.priceFactor = priceFactor; }
/** * Computes and returns the total proceeds for this event. * * @return total proceeds */
public double getProceeds() { double total = 0; for (int index = 0; index < ticketsSold; index++) { total = total + tickets[index].getPrice(); } return total; }
/** * Compares this Play with object. Follows the semantics of the equals * method in Object. * */ @Override public boolean equals(Object object) { if (this == object) { //this refers to the current object return true; } if (object == null) { return false; } if (getClass() != object.getClass()) { return false; } Event other = (Event) object; if (eventId != other.eventId) return false; return true; }
/** * Returns the description of the Play object * * @return description */ public String getDescription() { return description; }
/** * Returns the price factor * * @return price factor */ public double getPriceFactor() { return priceFactor; }
/** * Setter for description * * @param description * the new description */ public void setDescription(String description) { this.description = description; }
/** * Returns a unique serial number. This is a helper method. * * @return serial number */ private int computeSerialNumber() { return counter++; }
/** * Adds a ticket to the list of tickets sold for this Play object. * * @param ticket * the Ticket object to be added * @return true iff the Ticket object could be added. */
public boolean addTicket(Ticket ticket) { if (ticketsSold < CAPACITY) { tickets[ticketsSold] = ticket; ticketsSold++; return true; } return false; }
/** * Returns a String representation of this Event object */ @Override public String toString() { String string = eventId + " " + description + " " + priceFactor + " "; return string; }
} //THE TICKET CLASS
public class Ticket { private int serialNumber; //for the ticket private double price; //of ticket private static int counter = 1; // counter starts from one private Event event; //play for which ticket is sold private static double PRICE = 10.0; //base price
/** * Creates a ticket for an event. An exception is thrown if there is no * space. * * @param event * the event for which the tickt is being created. * @throws NoSpaceException */ public Ticket(Event event) throws NoSpaceException { if (event.addTicket(this)) { this.event = event; } else { throw new NoSpaceException("no space"); } price = PRICE * event.getPriceFactor(); serialNumber = computeSerialNumber(); }
/** * Returns the price of the ticket * * @return ticket price */ public double getPrice() { return price; }
/** * Generates a String representation of the Ticket. */ @Override public String toString() { return "Ticket serialNumber= " + serialNumber + ", price = " + price; }
/* * Creates a serial number for the ticket. */ private static int computeSerialNumber() { return counter++; } }
public class EventsTester { public int assertd(boolean condition, int score, int increment) { int value = 0; try { assert (condition); value = increment; } catch (Error error) {
} return score + value;
}
public int testEverything(int score) { try { Event play1 = new Play("p1"); Event concert1 = new Concert("c1"); Event meeting1 = new Meeting("m1", 100); for (int index = 0; index < 5; index++) { new Ticket(play1); } try { for (int index = 0; index < 5; index++) { new Ticket(concert1); } } catch (NoSpaceException nse) { score++; } Events events = new Events(); events.add(meeting1); events.add(concert1); events.add(play1); score = score + 2; score = assertd(events.getEvents().length == 3, score, 1); score = assertd(events.getEvents()[0].equals(meeting1), score, 1); score = assertd(events.getEvents()[1].equals(concert1), score, 1); score = assertd(events.getEvents()[2].equals(play1), score, 1); score = assertd(events.getTotalProceeds() == 110.64, score, 2); } catch (Exception e) {
} return score; }
public static void main(String[] args) { int score = 0; EventsTester test = new EventsTester(); score = test.testEverything(score); System.out.println(score + " out of 8"); } } //THE CONCERT CLASS TO BE COMPLITED
public class Concert extends Event { /** * Creates a Concert object with a description and a price factor. * * @param description * the description of the concert * @param priceFactor * the price factor */ // TODO CREATE CONSTRUCTOR
/** * Creates a concert with the given description and a price factor of 1.0. * * @param description * the description of the concert */ // TODO CREATE CONSTRUCTOR
/** * Returns a String representation. * */ // TODO CREATE APPROPRIATE METHOD }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started