3.1 The Play Class The changes would be as follows. 1. You need to add two const
ID: 3743770 • Letter: 3
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, 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");
}
}
Explanation / Answer
In order to provide a more precise solution, you would need to provide me the implementation of class Event. Once you do so, I can provide you a more correct definition of class Play.
But for time being, here is a sample implementation of class Play.
Play.java
************************
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
public Play(String description, double priceFactor) {
super(description, priceFactor);
}
/**
* 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
public Play(String description) {
super(description, 1.0);
}
/**
* Returns a String representation.
*
*/
@Override
public String toString() {
return "Play " + super.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.