Consider the class Die shown below, that represents a controller for a die.
import java.util.Random;
public class Die {
final private Random rand;
private int curValue;
public Die() {
rand = new Random();
toss();
}
public int getValue() {
return curValue;
}
public void toss() {
curValue = rand.nextInt(6);
}
}
Using classes already aware of model/view/controller (as we did
in Week 1 and in
Studio 1),
show how you would modify Die to implement
model/view/controller:
Consider the following ticket manager (TM) class that manages integers given out for a take-a-number system. Such systems manage crowds of customers by having each customer take a ticket, which bears a number. When the number is called, the customer is served. For this problem, do not concern yourself with limits on the value an int can assume.
public class TM {
private int currentlyServed; // ticket number now being served
private int lastCustomer; // last ticket given out so far
/**
* currentlyServed >= lastCustomer means nobody is waiting
*/
public TM() {
currentlyServed = 0;
lastCustomer = -1;
}
public Ticket takeNumber() {
lastCustomer = lastCustomer + 1;
return new Ticket(this, lastCustomer);
}
// Number now being served
public int nowServing() {
return currentlyServed;
}
public void returnTicket(Ticket t) {
if (t.getNumber() == currentlyServed)
advance();
else throw new Error("Security violation! (assume this won't happen)");
}
protected void advance() {
currentlyServed = currentlyServed + 1;
}
}
The above class should meet the following conditions:Numbers are given out in sequence and no two people get the same number.
Do not make any other modifications to TM in your response for this question.
public class Ticket {
private final TM tm;
private final int num;
public Ticket(TM tm, int myNumber) {
this.tm = tm;
this.num = myNumber;
}
// You must implement the method below according to the following:
// When it's my turn to be served now, return true
// If I am too late ever to be served, return false
// otherwise wait until one of the above conditions holds
public boolean waitForMyTurn() {
}
// When I am done being served, I must call done
public void done() {
tm.returnTicket(this);
}
// No need to change this
public boolean invokeWhenMyTurn(Runnable r) {
if (waitForMyTurn()) {
try {
r.run();
}
finally {
done();
}
return true;
}
else return false;
}
public int getNumber() {
return num;
}
}
Your response must be restricted to the code we both see (based on your answers) for the TM and Ticket classes.
How does this affect the operation of the system?
Consider a grocery store that has multiple take-a-number stations (for example, the deli, bakery, fish market). Each station operates by calling numbers from its own TM and waiting on its own customers.
Consider a Customer object that is instantiated with an array of TM objects. The Customer constructor can take at most one number from each of the TMs provided.
Because each station's service times are unpredictable, do not base your response on the apparent time remaining before a given ticket might be called.