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
erdelf erdelf

2014/1/29

#
not quite sure, but replace the method setValue with this, i think it works
    public void setValue(int replacementValue){  
        if(0<=replacementValue&&replacementValue<=limit)  
            value = replacementValue;  
         }  else if(replacementValue > limit) {
            value = replacementValue - 12;
         }
}
xnefoo xnefoo

2014/1/29

#
Ok I did what erdelf said but now when i set 11:59AM and advance the time by one minute. it turns into 12:00 AM not 12:00 PM. And 24:00 to 12:00PM I believe it has something to do with this.
        if (hour > 11)
        {
            timeZone = 2;
        }
danpost danpost

2014/1/30

#
I figured out your main problem. Your NumberDisplay limit for hours is set to 12 -- set it to 24 so you know AM from PM. Get rid of the 'timeZone' field and use 'if (hours.getValue() < 12)' in your updateDisplay method. Also, add another method for 'getMinuteDisplay' like you have for 'getHourDisplay' (it just needs to return the two digit String of 'minutes.getValue()'); then get rid of the 'getDisplayString' method from your NumberDisplay class.
xnefoo xnefoo

2014/1/30

#
danpost wrote...
I figured out your main problem. Your NumberDisplay limit for hours is set to 12 -- set it to 24 so you know AM from PM. Get rid of the 'timeZone' field and use 'if (hours.getValue() < 12)' in your updateDisplay method. Also, add another method for 'getMinuteDisplay' like you have for 'getHourDisplay' (it just needs to return the two digit String of 'minutes.getValue()'); then get rid of the 'getDisplayString' method from your NumberDisplay class.
When i get rid of the getDisplayString nothing compiles.
danpost danpost

2014/1/30

#
What error are you getting and what code is it highlighting? Give some context of where that line of code is located.
xnefoo xnefoo

2014/1/30

#
Actually nevermind I found out what was wrong. But when i input 23:59 it gives me 11:59AM not 11:59PM Here is my updated code:
/**
 * 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(24);
        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((hours.getValue() < 12)){
            displayString = getHourDisplay()+":"+getMinuteDisplay() + " AM";   
        }
        else if((hours.getValue() > 12)){
            displayString = getHourDisplay()+":"+getMinuteDisplay() + " PM";
        }
    } 

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

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

    public void timeTick(){
        if(minutes.getValue() == 59){
            hours.increment();
            timeZone = 2;
            if(hours.getValue() >= 11)
            {
                timeZone = 1;
            }
        }

        minutes.increment();
        updateDisplay();
    }

    public void setTime(int hour, int minute){
        hours.setValue(hour);
        if (hour > 11)
        {
            timeZone = 2;
        }
        if (hour == 24)
        {
            timeZone = 1;
        }
        if(hour == 11)
        {
            timeZone = 1;
        }
        minutes.setValue(minute);
        updateDisplay();
    }

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

2014/1/30

#
Change line 31 to:
else {
The condition-else-condition is missing 12 as a possibility for 'hours.getValue()'. There should be no need for the 'timeZone' field any longer. All code dealing with it can be removed.
xnefoo xnefoo

2014/1/30

#
Ok I changed it to else but i still get the same problem
danpost danpost

2014/1/30

#
Ok. Remove lines 69 through 80. Remove lines 56 through 60. Remove line 12. Change lines 16 through 18 to
this(12, 0);
Change line 22 to:
hours = new NumberDisplay(24);
Then if you still have a problem, show what you tried and explain what was wrong.
xnefoo xnefoo

2014/1/30

#
Wait Actually I figured out the problem. Thanks Dan!
danpost danpost

2014/1/30

#
Alright. Explain what the problem was and show the final code for the class (so others can learn from it).
danpost danpost

2014/2/1

#
Ok. I will post what I ended up with:
public class TwelveHourClock
{
    public RollingCounter hours;
    public RollingCounter minutes;
    public String displayString;

    public TwelveHourClock()
    {
        this(12,0);
    }

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

    private void updateDisplay()
    {
        displayString = getHourDisplay()+":"+getMinuteDisplay();
        displayString += hours.getValue() < 12 ? " AM" : " PM";
    } 

    public String getHourDisplay()
    {
        int value = (hours.getValue()%12+11)%12+1;
        return ((""+value).length() == 1 ? "0" : "")+value;
    }
    
    public String getMinuteDisplay()
    {
        int value = minutes.getValue();
        return ((""+value).length() == 1 ? "0" : "")+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;
    }

    private class RollingCounter
    {
        private int value;
        private int limit;
    
        private RollingCounter(int rollOverLimit)
        {
            limit = rollOverLimit;
        }
        
        public int getValue()
        {
            return value;
        }
        
        public void setValue(int replacementValue)
        {
            if (0 <= replacementValue && replacementValue < limit) value = replacementValue;
        }
        
        public void increment()
        {
            value = (value+1)%limit;
        }
    }
}
xnefoo xnefoo

2014/2/2

#
Yes that code is what I got also.
You need to login to post a reply.
1
2