Tre Tre - 6 months ago 22
Java Question

How do I return time; from ClockDisplay() object in printDetails?

I need to create a historical moment object which returns the time from the

ClockDisplay()
object and I am wondering how do I return time; from
ClockDisplay()
object? When I compile it, it has no errors but when I create a
ClockDisplay
object with the time value of 11:00 and I create a
HistoricalMoment
object and I inspect it, it returns a null value when I
printDetails
.

Here is my code:

/**
* @return the time
*/
public ClockDisplay getTime()
{
return time;
}


Here is my printDetails:

/**
* the print details of time and event
*/
public void printDetails()
{
System.out.println("At " + getTime() + "," + getEventName());
}


This is my HistoricalMoment class:

public class HistoricalMoment{

private String eventName;
private ClockDisplay timeOfEvent;
private ClockDisplay time;

public static final int MIDNIGHT_HOUR = 00;
public static final int MINUTE_ZERO = 00;

public static final int ELVENTH_HOUR = 11;
public static final int TWO_MINUTES = 02;
public static final int FORTY_MINUTES = 40;
public static final int NINTH_HOUR = 9;
public static final int FOUR_MINUTES = 04;

/**
* Default Constructor
*/
public HistoricalMoment(){
eventName = "untitled event";
timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);

}

/**
* @param nameOfTheEvent the name of the event; "untitled event" if the name of the event is null or ""
*/
public HistoricalMoment(String nameOfTheEvent){
if ((nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
eventName = "untitled event";
timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
}
else {
eventName = nameOfTheEvent;
timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
}

}

/**
* @param the name and time of the event
*/
public HistoricalMoment(String nameOfTheEvent, ClockDisplay theTime)
{
if ( (nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
eventName = "untitled event";
timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
}
else{
eventName = nameOfTheEvent;
timeOfEvent = theTime;
}
}

/**
* @return the time of event
*/
public ClockDisplay getTime()
{
return timeOfEvent;
}


/**
* @return the eventName;
*/
public String getEventName()
{
return eventName;
}

/**
* @return the time of the event incremented
*/
public void addMinuteToTimeOfEvent(){
timeOfEvent.timeTick();
}

/**
* the print details of time and event
*/
public void printDetails()
{
System.out.println("At " + getTime() + "," + getEventName());
}

}


Here is my ClockDisplay class:

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

public static final int FIRST_MORNING_HOUR = 0;
public static final int LAST_MORNING_HOUR = 11;
public static final int FIRST_EVENING_HOUR = 12;
public static final int LAST_EVENING_HOUR = 23;
public static final int MINUTES_PER_HOUR = 60;
public static final int MINUTES_ZERO = 0;
public static final String MORNING_SUFFIX = "a.m.";
public static final String EVENING_SUFFIX = "p.m.";
public static final int MIDNIGHT_HOUR = 0;
public static final int HOURS_PER_DAY = 0;
public static final int TWENTY_FOUR_HOURS = 24;
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(TWENTY_FOUR_HOURS);
minutes = new NumberDisplay(MINUTES_PER_HOUR);
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)
{
hours = new NumberDisplay(TWENTY_FOUR_HOURS);
minutes = new NumberDisplay(MINUTES_PER_HOUR);
setTime(hour, minute);
}

/**
* 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() == MINUTES_ZERO){ // it just rolled over!
hours.increment();
}
updateDisplay();
}

/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}

/**
* @retun the time from displayString
*/
public String getTime()
{
return displayString;
}

/**
* the updated display
*/
private void updateDisplay()
{
if(hours.getValue() < FIRST_EVENING_HOUR){
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue() + " am";
}

else if(hours.getValue() > FIRST_EVENING_HOUR && hours.getValue() <LAST_EVENING_HOUR){
displayString = Integer.toString(hours.getValue() - FIRST_EVENING_HOUR) + ":" +
minutes.getDisplayValue() + " pm";
}
else if(hours.getValue() == MIDNIGHT_HOUR){
hours.setValue(FIRST_EVENING_HOUR);
displayString = hours.getDisplayValue() + ":"+
minutes.getDisplayValue() + " am (midnight)";
}
else{
hours.setValue(FIRST_EVENING_HOUR);
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue() + " pm (noon)";
}
}
}

Tre Tre
Answer

So I figured it out after quite some time thinking it over what I had to do is call the timeOfEvent get value to coincide with the ClockDisplay value.

Here is the coding for the printDetails I was having issues with. Hope it will help with anyone else in the future.

public void printDetails()
    {
        System.out.println("At " + timeOfEvent.getTime() + ", " + eventName);
    }

Here is the full coding for HistoricalMoments class with corrections:

public class HistoricalMoment{

private String eventName;
private ClockDisplay timeOfEvent;

public static final int MIDNIGHT_HOUR = 00;
public static final int MINUTE_ZERO = 00;

public static final int ELVENTH_HOUR = 11;
public static final int TWO_MINUTES = 02;
public static final int FORTY_MINUTES = 40;
public static final int NINTH_HOUR = 9;
public static final int FOUR_MINUTES = 04;

/**
 * Default Constructor
 */
public HistoricalMoment(){
    eventName = "untitled event";
    timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);

}

/**
 * @param nameOfTheEvent the name of the event; "untitled event" if the name of the event is null or ""
 */
public HistoricalMoment(String nameOfTheEvent){
    if ((nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
        eventName = "untitled event";
        timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
    }
    else {
        eventName = nameOfTheEvent;
        timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
    }

}

/**
 * @param the name and time of the event
 */
public HistoricalMoment(String nameOfTheEvent, ClockDisplay theTime)
{
    if ( (nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
        eventName = "untitled event";
        timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
    }
    else{
        eventName = nameOfTheEvent;
        timeOfEvent = theTime;
    }
}

/**
 * @return the time of the event incremented
 */
public void addMinuteToTimeOfEvent(){
    timeOfEvent.timeTick();
}

/**
 * the print details of time and event
 */
public void printDetails()
{
    System.out.println("At " + timeOfEvent.getTime() + ", " + eventName);
}

}
Comments