This site requires JavaScript, please enable it in your browser!
Greenfoot back
xnefoo
xnefoo wrote ...

2014/1/29

Bluej 12 Hour Clock Help

1
2
xnefoo xnefoo

2014/1/29

#
I have converted a 24 Hour Clock to the 12 hour clock. But for some reason when I put in 0:30 in it outputs it as 1:30am (Supposed to go to 12:30am) And when i put in 12:30 it goes to 1:30AM (Supposed to go to 12:30PM). Can anyone help me with my problem? Thanks!
/**
 * Write a description of class TwelveHourClock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TwelveHourClock
{
    public NumberDisplay hours;
    public NumberDisplay minutes;
    public String displayString;
    private int timeZone = 1;

    public TwelveHourClock()
    {
        hours = new NumberDisplay(12);
        minutes = new NumberDisplay(60); 
        setTime(12,0);
    }

    public TwelveHourClock(int hour, int minute){
        hours = new NumberDisplay(12);
        minutes = new NumberDisplay(60);
        setTime(hour, minute);
    }

    private void updateDisplay(){
        if(timeZone == 1){
            displayString = getHourDisplay()+":"+minutes.getDisplayString() + " AM";   
        }
        else if(timeZone == 2){
            displayString = getHourDisplay()+":"+minutes.getDisplayString() + " PM";
        }
    } 

    public String getHourDisplay(){
        int value = hours.getValue()+1;
        if(String.valueOf(value).length() == 1){
            return "0"+value;
        }
        return value+"";
    }

    public void timeTick(){
        if(minutes.getValue() == 59){
            hours.increment();
        }
        minutes.increment();
        updateDisplay();
    }

    public void setTime(int hour, int minute){
        hours.setValue(hour);
        minutes.setValue(minute);
        updateDisplay();
    }

    public String getTime(){
        return displayString;
    }
}
danpost danpost

2014/1/29

#
Without the NumberDisplay class code, it would be difficult to determine the problem. Also, if this is to be a 12-hour clock the 'AM/PM' part can be eliminated and there is no need for the 'timeZone' field.
danpost danpost

2014/1/29

#
I am not sure, but maybe line 37 should be:
int value = (hours.getValue()+11)%12+1;
This will change the value of zero to twelve and not alter any other value.
xnefoo xnefoo

2014/1/29

#
The number display class.
/**
 * Write a description of class NumberDisplay here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class NumberDisplay
{
    public int value;
    public int limit;
    /**
     * Constructor for objects of class NumberDisplay
     */
    public NumberDisplay(int rollOverLimit)
    {
        value = 0;
        limit = rollOverLimit;
    }
    
    public int getValue(){
        return value;
    }
    
    public void setValue(int replacementValue){
        if(0<=replacementValue&&replacementValue<limit)
            value = replacementValue;
    }
    
    public String getDisplayString(){
        if(String.valueOf(value).length() == 1){
            return "0"+value;
        }
        return value+"";
    } 
    
    public void increment(){
        value = (value+1)%limit;
    }
}
xnefoo xnefoo

2014/1/29

#
Also I the 12 hour clock is supposed to do this: For example. It takes the input for 14 and turns it into 2pm. Or 2 would be 2AM. So i don't think removing the timezone would help.
danpost danpost

2014/1/29

#
I think the problem is what I stated in my second post above.
danpost danpost

2014/1/29

#
xnefoo wrote...
Also I the 12 hour clock is supposed to do this: For example. It takes the input for 14 and turns it into 2pm. Or 2 would be 2AM. So i don't think removing the timezone would help.
Ok. I see what you are doing. My bad!
xnefoo xnefoo

2014/1/29

#
        if (hour > 12)
        {
            timeZone = 2;
        }
Ok that solves my AM problem but any number above twelve defualts to 12. I also made the change that you posted. And made another one myself beginning at line 21.
danpost danpost

2014/1/29

#
danpost wrote...
I am not sure, but maybe line 37 should be:
int value = (hours.getValue()+11)%12+1;
This will change the value of zero to twelve and not alter any other value.
I guess I should have qualified this at 'any other value below twelve. Try this:
int value = (hours.getValue()%12+11)%12+1;
xnefoo xnefoo

2014/1/29

#
Thanks but, that doesn't seem to work.
danpost danpost

2014/1/29

#
xnefoo wrote...
Thanks but, that doesn't seem to work.
How is it not working?
danpost danpost

2014/1/29

#
xnefoo wrote...
if (hour > 12)
{
    timeZone = 2;
}
Should not the condition be 'if (hour > 11)'? or just say 'timeZone = hour/12+1;'.
xnefoo xnefoo

2014/1/29

#
danpost wrote...
xnefoo wrote...
Thanks but, that doesn't seem to work.
How is it not working?
When i input 14:30 it goes to 12:30PM not 2:30PM
danpost danpost

2014/1/29

#
danpost wrote...
Try this:
int value = (hours.getValue()%12+11)%12+1;
This should work. Did you not notice the slight difference from the original statement?
xnefoo xnefoo

2014/1/29

#
Ok I switched it to that and hour > 11. But when it still gives me 12:30PM when i put in 14:30
There are more replies on the next page.
1
2