-1

I have been tasked with linking two classes together. The first class is a ticket machine which allows the person to buy a ticket and then print it out (via System.out.println). The second class is a clock display which displays the time.

My task is to make the ticket class print the time currently displayed in the clock display class. I have been told I do not need to edit either the NumberDisplay class or the ClockDisplay class.

My initital thoughts were to create a new ClockDisplay field within my ticketmachine class, and then use

System.out.println("Time:" + ClockDisplay.displayString);

as displayString is what I use to find the value within the clockdisplay class. However, as the field is private and I cannot edit the clockdisplay class, I cannot do this. Any thoughts?

Thank you. Here is my code so far, with the aforementioned piece of code in the TicketMachine class.

NumberDisplay

public class NumberDisplay
{
private int limit;
private int value;

/**
 * Constructor for objects of class NumberDisplay.
 * Set the limit at which the display rolls over.
 */
public NumberDisplay(int rollOverLimit)
{
    limit = rollOverLimit;
    value = 0;
}

/**
 * Return the current value.
 */
public int getValue()
{
    return value;
}

/**
 * Return the display value (that is, the current value as a two-digit
 * String. If the value is less than ten, it will be padded with a leading
 * zero).
 */
public String getDisplayValue()
{
    if(value < 10) {
        return "0" + value;
    }
    else {
        return "" + value;
    }
}

/**
 * Set the value of the display to the new specified value. If the new
 * value is less than zero or over the limit, do nothing.
 */
public void setValue(int replacementValue)
{
    if((replacementValue >= 0) && (replacementValue < limit)) {
        value = replacementValue;
    }
}

/**
 * Increment the display value by one, rolling over to zero if the
 * limit is reached.
 */
public void increment()
{
    if ((value +1) >= limit) {
        value = 0;
    }
    else {
        value = value + 1;
    }
}
}

ClockDisplay

public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private NumberDisplay seconds;
private String displayString;    // simulates the actual display

/**
 * Constructor for ClockDisplay objects. This constructor 
 * creates a new clock set at 12:00:00.
 */
public ClockDisplay()
{
    hours = new NumberDisplay(12); // changed from 24hour to 12 hour
    minutes = new NumberDisplay(60);
    seconds = new NumberDisplay(60);
    updateDisplay();
}

/**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at the time specified by the 
 * parameters.
 */
public ClockDisplay(int hour, int minute, int second)
{
    hours = new NumberDisplay(12); //changed from 24hour to 12 hour
    minutes = new NumberDisplay(60);
    seconds = new NumberDisplay(60);
    setTime(hour, minute, second);
}

/**
 * This method should get called once every minute - it makes
 * the clock display go one minute forward.
 */
public void timeTick()
{
    minutes.increment();
    if(minutes.getValue() == 0) {  // it just rolled over!
        hours.increment();
    }
    updateDisplay();
}

/**
 * Set the time of the display to the specified hour and
 * minute and second.
 */
public void setTime(int hour, int minute, int second)
{
    if (hour == 12) { //changing the display from '00:00' to '12:00'
        hour = 0;
    }
    hours.setValue(hour);
    minutes.setValue(minute);
    seconds.setValue(second);
    updateDisplay();
}

/**
 * Return the current time of this display in the format HH:MM:SS.
 */
public String getTime()
{
    return displayString;
}

/**
 * Update the internal string that represents the display.
 */
private void updateDisplay()
{
    int hour = hours.getValue(); //changes the display to from showing '00:00' to '12:00'
    if (hour == 0) {
        hour = 12;
    }
    displayString = hour + ":" + 
                    minutes.getDisplayValue() + ":" + seconds.getDisplayValue();
}

}

Ticket Machine

public class TicketMachine
{
// The price of a ticket from this machine.
private int price;
// The amount of money entered by a customer so far.
private int balance;
// The total amount of money collected by this machine.
private int total;
// The time from the clockdisplay class
private ClockDisplay time;

/**
 * Create a machine that issues tickets of the given price.
 */
public TicketMachine(int cost)
{
    price = cost;
    balance = 0;
    total = 0;
}

/**
 * @Return The price of a ticket.
 */
public int getPrice()
{
    return price;
}

/**
 * Return The amount of money already inserted for the
 * next ticket.
 */
public int getBalance()
{
    return balance;
}

/**
 * Receive an amount of money from a customer.
 * Check that the amount is sensible.
 */
public void insertMoney(int amount)
{
    if(amount > 0) {
        balance = balance + amount;
    }
    else {
        System.out.println("Use a positive amount rather than: " +
                           amount);
    }
}

/**
 * Print a ticket if enough money has been inserted, and
 * reduce the current balance by the ticket price. Print
 * an error message if more money is required.
 */
public void printTicket()
{
    if(balance >= price) {
        // Simulate the printing of a ticket.
        System.out.println("##################");
        System.out.println("# The BlueJ Line");
        System.out.println("# Ticket");
        System.out.println("# " + price + " cents.");
        System.out.println("##################");
        System.out.println();

        // Update the total collected with the price.
        total = total + price;
        // Reduce the balance by the prince.
        balance = balance - price;
        // Print the current time from the NumberDisplay class.
        System.out.println("Time:" + ClockDisplay.displayString);
    }
    else {
        System.out.println("You must insert at least: " +
                           (price - balance) + " more cents.");

    }
}

/**
 * Return the money in the balance.
 * The balance is cleared.
 */
public int refundBalance()
{
    int amountToRefund;
    amountToRefund = balance;
    balance = 0;
    return amountToRefund;
}

}

user3455584
  • 5
  • 1
  • 10

3 Answers3

0

Use the provided method getTime() to get it. Like,

System.out.println("Time:" + ClockDisplay.getTime());

Also (by convention), Java variables should start with a lower-case letter.

ClockDisplay clockDisplay;
// Set-up the variable....
System.out.println("Time:" + clockDisplay.getTime());
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • It says non-static method getTime() cannot be referenced from a static context when I try to do that? – user3455584 Dec 08 '14 at 19:13
  • Correct. You need an instance in order to get any [non-static](http://stackoverflow.com/a/20592547/2970947) field. – Elliott Frisch Dec 08 '14 at 19:14
  • Ah yes. I have instantiated the ClockDisplay class in my constructor and it now works. Thanks for all your help. I also will correct my variables. Thanks again – user3455584 Dec 08 '14 at 19:19
0

In your TicketMachine class, you forgot to instantiate ClockDisplay. Try this in your printTicket() method in TicketMachine:

time = new ClockDisplay();        
System.out.println("Time:" + time.getTime());

UPDATE

Here is how you can set the time:

time = new ClockDisplay();
time.setTime(3,9,34);
System.out.println("Time: " + time.getTime());

Here is the output:

##################
# The BlueJ Line
# Ticket
# -2 cents.
##################

Time: 3:09:34
cno
  • 184
  • 7
  • Gosh, how silly. It's amazing how easy it is to miss something like that. It's working fine now, thankyou!Edit - infact, it's not working completely - it displays the time as 12:00:00 regardless of whether I change the time using the setTime method - what could be the reason for this? – user3455584 Dec 08 '14 at 19:20
  • I thought you were getting the `displayString` from the `getTime()` method? What else do you need to do? – cno Dec 08 '14 at 19:29
  • I'm not sure. The only edit I made to my code was to implement the line you told me to put in the printTicket() method. – user3455584 Dec 08 '14 at 19:37
  • You can call `setTime(int hour, int minute, int second)` method to specifically set a certain time- like this: `time.setTime(2,43,13)` then call `time.getTime()` to print it. Can you be more specific in what you're looking for? – cno Dec 08 '14 at 19:44
  • Perhaps I haven't been clear enough trying to explain, or maybe i'm just being silly! In my clockdisplay class, if I use the setTime method and set it for eg 11:11:11 and THEN go to print a ticket, the time returned in the terminal remains at 12:00:00 - I want it to display 11:11:11 or whatever number I change it to should i use the setTime method in my other class. Thanks – user3455584 Dec 08 '14 at 19:51
0

If you notice only field are private. To access these private fields you have public getter-setter methods. This is one of the fundamental OOP concept - Encapsulation.

To access your getter setter, You have to instantiate your object, as you can see, these are not static methods.

You can create object of ClockDisplay in the class where you want to print time.

ClockDisplay clockDisplay = new ClockDisplay();
System.out.print(clockDisplay.getTime());
aadi53
  • 439
  • 4
  • 17