我需要创建一个历史时刻对象,它从 ClockDisplay()
对象返回时间,我想知道如何返回时间;来自 ClockDisplay()
对象?当我编译它时,它没有错误,但是当我创建一个时间值为 11:00 的 ClockDisplay 对象并创建一个 HistoricalMoment 对象并检查它时,当我 printDetails 时它返回一个空值。
这是我的代码:
/**
* @return the time
*/
public ClockDisplay getTime()
{
return time;
}
这是我的打印详细信息:
/**
* the print details of time and event
*/
public void printDetails()
{
System.out.println("At " + getTime() + "," + getEventName());
}
这是我的 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());
}
}
这是我的 ClockDisplay 类:
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)";
}
}
}
最佳答案
所以经过相当长的时间思考后我发现我必须做的就是调用 timeOfEvent 获取值以与 ClockDisplay 值一致。
这是我遇到问题的 printDetails 的编码。希望它能对其 future 的其他人有所帮助。
public void printDetails()
{
System.out.println("At " + timeOfEvent.getTime() + ", " + eventName);
}
以下是带有更正的 HistoricalMoments 类的完整编码:
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);
}
}
关于java - 我如何返回时间;来自 printDetails 中的 ClockDisplay() 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590424/