CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2011
    Posts
    8

    Question 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.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JAVA 1 Class, NEED HELP

    What exactly is your problem, what are you stuck on?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2011
    Posts
    8

    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.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Oct 2011
    Posts
    8

    Question 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;
        }

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JAVA 1 Class, NEED HELP

    Have you followed my full instructions and does it compile and work?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Oct 2011
    Posts
    8

    Question 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?

  8. #8
    Join Date
    Oct 2011
    Posts
    2

    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.

  9. #9
    Join Date
    Oct 2011
    Posts
    8

    Re: JAVA 1 Class, NEED HELP

    I am not aloud at add any new methods.

  10. #10
    Join Date
    Oct 2011
    Posts
    2

    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.

  11. #11
    Join Date
    Oct 2011
    Posts
    8

    Resolved 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
  •  





Click Here to Expand Forum to Full Width

Featured