Question
3.1 The Play Class The changes would be as follows. 1. You need to add two constructors, like in Event 2. As tickets are sold,
3.1
The Play Class The changes would be as follows.
1. You need to add two constructors, like in Event
2. As tickets are sold, the price factor is adjusted automatically. If it is found that 60% of the tickets have been sold, the price factor is increased by a factor of 1.2. This new price factor applies until it is observed that 80% of the tickets have been sold. Then the price factor is increased once more by a factor of 1.2. For example, assume that the capacity is 100 and that the price factor is 1.0 initially. Let us assume that this was not changed for the first 60 tickets. (Remember the setPriceFactor method, which could be used to change price factor.) When a ticket is requested now (that is, after 60 tickets have been sold), the price factor is increased to 1.2 1.0, that is 1.2. This price factor applies for tickets 61 through 80. After 80 tickets have been sold, if a new ticket is to be sold, one more hike is made to the price factor when selling the 81st ticket. The price factor is now 1.2 1.2, which is 1.44. Of course, a client could override the above by manually changing the price factor. 3 Note that price factor is automatically adjusted exactly once after 60 percent of tickets have been sold until but not including 81 percent. Similarly, price factor is automatically adjusted exactly once after 80 percent of tickets have been sold.
3. When the toString method is invoked, it must return the string with the word Play 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.
Play public class Play extends Event {
/** * Creates a Play object with the description and price factor. * * @param description * the description of the play * @param priceFactor * the price factor for the play */
// TODO Create a constructor
/** * Creates a play with the given description and a price factor of 1.0. * * @param description * the description of the play */
// TODO Create a constructor
/** * Returns a String representation. * */ @Override public String toString() { return "Play " + /* TODO */; } }
Test Player
import java.util.StringTokenizer;
public class PlayTester { 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 testConstructorAndGetters(int score) { Play play1 = null; Play play2 = null; try { play1 = new Play("p1", 2.0); play2 = new Play("p2"); score += 2; } catch (Exception e) { System.out.println("constructor...issues"); }
try { score = assertd(play1.getDescription().equals("p1"), score, 1); } catch (Exception e) { System.out.println("constructor...issues"); }
try { score = assertd(play1.getEventId() == 1, score, 1); score = assertd(play2.getEventId() == 2, score, 1); } catch (Exception e) { System.out.println("constructor...issues"); }
try { score = assertd(play1.getPriceFactor() == 2.0, score, 1); play2.setPriceFactor(3.0); play2.setDescription("play2"); score = assertd(play2.getPriceFactor() == 3.0, score, 1); score = assertd(play2.getDescription().equals("play2"), score, 1); } catch (Exception e) { System.out.println("constructor...issues"); }
return score; }
public int testToString(int score) { double value = 0.0; try { Play play1 = null; Play play2 = null; try { play1 = new Play("p1", 2.0); play2 = new Play("p2"); score += 1; } catch (Exception e) { } String string = play1.toString(); System.out.println(string); StringTokenizer tokenizer = new StringTokenizer(string); String token = tokenizer.nextToken(); if (token.equals("Event")) { value = value + 0.25; } System.out.println(token + " " + value);
token = tokenizer.nextToken(); if (token.equals("3")) { value = value + 0.25; } System.out.println(token + " " + value); token = tokenizer.nextToken(); if (token.equals("p1")) { value = value + 0.25; } System.out.println(token + " " + value);
} catch (Exception e) { System.out.println("string issues"); } // System.out.println("Value " + value); score = score + Math.round((float) value); return score; }
public int testEquals(int score) { try { Play play1 = new Play("p1"); Play play2 = new Play("p2"); score = assertd(!play1.equals(play2), score, 1); score = assertd(play1.equals(play1), score, 1); } catch (Exception e) { } return score; }
public int testAddTickets(int score) { try { Play play1 = new Play("p1"); Ticket[] tickets = new Ticket[5]; for (int index = 0; index < tickets.length; index++) { tickets[index] = new Ticket(play1); } score = score + 3; Ticket[] retrievedTickets = play1.getTickets(); for (int index = 0; index < retrievedTickets.length; index++) { assert (retrievedTickets[index].equals(tickets[index])); } Ticket tExtra = null; try { tExtra = new Ticket(play1);
} catch (Exception e) { System.out.println("couldn't add"); retrievedTickets = play1.getTickets(); for (int index = 0; index < retrievedTickets.length; index++) { assert (!retrievedTickets[index].equals(tExtra)); } score += 2; } } catch (Exception e) { System.out.println("issues " + e); }
return score; }
public static void main(String[] args) { int score = 0; PlayTester test = new PlayTester(); score = test.testConstructorAndGetters(score); score = test.testToString(score); score = test.testEquals(score); score = test.testAddTickets(score); System.out.println(score + " out of 17"); } }
Ticket
/**
* Implements a single Ticket for any event.
*
* @author Brahma Dathan
*
*/
public class Ticket {
private int serialNumber;
private double price;
private static int counter = 1;
private Event event;
private static double PRICE = 10.0;
/**
* 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++;
}
}
Ticket Test
import java.util.StringTokenizer;
public class TicketTester {
private Play event = new Play("E1", 1.5);
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 testConstructorAndGetters(int score) {
Ticket ticket = null;
try {
ticket = new Ticket(event);
score += 1;
} catch (Exception e) {
}
try {
score = assertd(ticket.getPrice() == 10.0 * event.getPriceFactor(), score, 1);
} catch (Exception e) {
}
try {
score = assertd(ticket.getSerialNumber() == 1, score, 1);
Ticket ticket2 = new Ticket(event);
score = assertd(ticket.getSerialNumber() == 2, score, 1);
} catch (Exception e) {
}
try {
score = assertd(ticket.getEvent().equals(event), score, 1);
} catch (Exception e) {
}
return score;
}
public int testToString(int score) {
double value = 0.0;
try {
Ticket ticket1 = new Ticket(event);
String string = ticket1.toString();
System.out.println(string);
StringTokenizer tokenizer = new StringTokenizer(string);
String token = tokenizer.nextToken();
if (token.equals("Ticket")) {
value = value + 0.25;
}
System.out.println(token + " " + value);
token = tokenizer.nextToken();
if (token.equals("serialNumber")) {
value = value + 0.25;
}
System.out.println(token + " " + value);
token = tokenizer.nextToken();
if (token.equals("3")) {
value = value + 0.25;
}
System.out.println(token + " " + value);
token = tokenizer.nextToken();
if (token.equals("price")) {
value = value + 0.25;
}
System.out.println(token + " " + value);
token = tokenizer.nextToken();
if (token.equals("15.0")) {
value = value + 0.25;
}
} catch (Exception e) {
}
System.out.println("Value " + value);
score = score + Math.round((float) value);
return score;
}
public int testEquals(int score) {
try {
Ticket ticket1 = new Ticket(event);
Ticket ticket2 = new Ticket(event);
score = assertd(!ticket1.equals(ticket2), score, 1);
score = assertd(ticket1.equals(ticket1), score, 1);
} catch (Exception e) {
}
return score;
}
public static void main(String[] args) {
int score = 0;
TicketTester test = new TicketTester();
score = test.testConstructorAndGetters(score);
score = test.testToString(score);
score = test.testEquals(score);
System.out.println(score + " out of 8");
}
}
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");
}
}
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