3.2 The Concert Class 1. It will have two constructors that are identical as far
ID: 3744042 • Letter: 3
Question
3.2 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.
Events
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.
*
* @author Brahma Dathan
*
*/
public class Event {
private String description;
public static final int CAPACITY = 5;
protected int ticketsSold;
private int eventId;
private double priceFactor;
private static int counter = 1;
private Ticket[] tickets;
/**
* 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 thos 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) {
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;
}
}
Another Events
import java.util.Arrays;
/**
* Collection of all events
*
* @author Brahma Dathan
*
*/
public class Events {
private Event[] events = new Event[10];
private int numberOfEvents;
/**
* Adds an event to the list of events.
*
* @param event
* the new event
*/
public void add(Event event) {
events[numberOfEvents++] = event;
}
/**
* Getter for the events
*
* @return getter
*/
public Event[] getEvents() {
return Arrays.copyOf(events, numberOfEvents);
}
/**
* Computes and returns the total number of tickets sold for the events.
*
* @return number of tickets sold
*/
public double getTotalProceeds() {
double proceeds = 0;
return proceeds;
}
/**
* Generates a String representation of the object
*/
@Override
public String toString() {
return "Events events=" + events;
}
}
Event Tester
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");
}
}
Need Help
Concert
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
}
Explanation / Answer
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
public Concert(String description, double priceFactor) {
super(description, priceFactor);
}
/**
* Creates a concert with the given description and a price factor of 1.0.
*
* @param description
* the description of the concert
*/
// TODO CREATE CONSTRUCTOR
public Concert(String description) {
super(description);
}
/**
* Returns a String representation.
*
*/
// TODO CREATE APPROPRIATE METHOD
@Override
public String toString() {
return "Concert = " + super.toString();
}
}
Thanks, let me know if there is any concern.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.