-
October 22nd, 2011, 12:42 AM
#1
JAVA 1 Class, NEED HELP
I am looking for advise the answer.
I am in a JAVA 1 programing class online and right now we are working on a 24 to 12 hour clock conversion. We are only able to change the timeTick method, the constructors and the setTime
Here is where I am at
Code:
/**
* The ClockDisplay class implements a digital clock display for a
* 12 hour clock. The clock shows hours and minutes. The
* range of the clock is 12:00am (midnight) to 11:59pm (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kolling and David J. Barnes
* @version 2008.03.30
*
* Tyler Hord
* Modafications:
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 12:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(12);
minutes = 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)
{
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
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() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
if(updateDisplay() == "00:00"){
return "12:00";}
}
/**
* 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();
}
/**
* Return the current time of this display in the format HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}
Please help. Advise or ideas only please no answers.
-
October 22nd, 2011, 06:01 AM
#2
Re: JAVA 1 Class, NEED HELP
What exactly is your problem, what are you stuck on?
-
October 22nd, 2011, 02:07 PM
#3
Re: JAVA 1 Class, NEED HELP
I am unable to get the display to read 12 instead of 00:00 for noon and midnight. Every other hour works as a 12 hour clock. I have tried making several changes in timeTick but every time I make a change a piece of timeTick stops working.
-
October 22nd, 2011, 06:05 PM
#4
Re: JAVA 1 Class, NEED HELP
If you can only change that limited set of methods mentioned in your first post there is no sensible way of doing it. The problem is purely a data representation issue and it should be handled in the updateDisplay method.
However, you can achieve the effect you want with a bodge by constructing the hours NumberDisplay object with a value of 13 and then in the timeTick method check for the hour rolling over to 0 and immediately set it to 1.
-
October 22nd, 2011, 06:18 PM
#5
Re: JAVA 1 Class, NEED HELP
Is this how it should look?
Code:
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
if(updateDisplay() == 0)
return 1;
}
-
October 24th, 2011, 12:08 PM
#6
Re: JAVA 1 Class, NEED HELP
Have you followed my full instructions and does it compile and work?
-
October 24th, 2011, 01:25 PM
#7
Re: JAVA 1 Class, NEED HELP
I am not able to change the updateDisplay method due to my teachers instructions so I added the if statement to the timeTick method's updateDislay and when I try to compile it the new if statement is highlighted and I get an error saying 'void' type no allowed here. The brackets after updateDisplay are in a red box. What am I doing wrong?
-
October 24th, 2011, 01:37 PM
#8
Re: JAVA 1 Class, NEED HELP
updateDisplay() doesn't return anything (string or integer), and you're trying to compare it's return value to 0. Instead, you could try checking to see what the time is with a method that returns the time, getTime(), changing/setting the time as necessary, before updating the display.
-
October 24th, 2011, 01:41 PM
#9
Re: JAVA 1 Class, NEED HELP
I am not aloud at add any new methods.
-
October 24th, 2011, 03:50 PM
#10
Re: JAVA 1 Class, NEED HELP
True, but the getTime() method you included in the code returns a string value that represents the time and the setTime() method allows you to change the time.
-
October 25th, 2011, 09:22 PM
#11
Re: JAVA 1 Class, NEED HELP
Thank you all for your help. I got it all worked out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|