www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Clock Display

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    London
    Posts
    23

    Clock Display

    HI all ok here I get a problem I am trying to change a 24 clock display to 12 hours displays but some how my code dosen't compile help need it if anyone know how to do this please let ok here is my code I just don't know whats wrong with it

    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();
    }

    /**
    * 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();
    }

    public function clock() {
    if (getDisplay) return;
    hours = getHours();
    minutes = getMinutes();
    amOrPm = "AM";
    if (hours > 11) amOrPm = "PM";
    if (hours > 12) hours = hours - 12;
    if (hours == 0) hours = 12;
    if (minutes <= 9) minutes = "0" + minutes;
    if (seconds <= 9) seconds = "0" + seconds;
    dispTime = "<span class='datetime'>" + hours + ":" + minutes + ":" + seconds + " " + amOrPm + "</span>";
    if (document.layers) {
    document.layers.pendule.document.write(dispTime);
    document.layers.pendule.document.close();
    }
    else
    if (document.all)
    pendule.innerHTML = dispTime;
    setTimeout("clock()", 1000);
    }
    }
    }

    then I created another class for the number display here is code for it this one works fine:

    public class NumberDisplay
    {
    private int limit;
    private int value;

    /**
    * Constructor for objects of class Display
    */
    public NumberDisplay(int rollOverLimit)
    {
    limit = rollOverLimit;
    value = 0;
    }

    /**
    * Return the current value.
    */
    public int getValue()
    {
    return value;
    }

    /**
    * Return the display value (that is, the current value as a two-digit
    * String. If the value is less than ten, it will be padded with a leading
    * 12).
    */
    public String getDisplayValue()
    {
    if(value < 10)
    return "0" + value;
    else
    return "" + value;
    }

    /**
    * Set the value of the display to the new specified value. If the new
    * value is less than zero or over the limit, do nothing.
    */
    public void setValue(int replacementValue)
    {
    if((replacementValue >= 0) && (replacementValue < limit))
    value = replacementValue;
    }

    /**
    * Increment the display value by one, rolling over to zero if the
    * limit is reached.
    */
    public void increment()
    {
    value = (value + 1) % limit;
    }
    }

    Thats all I done and I still don't get it why it dosen't compile any help would be great thank you.
    Be good coz its good for everyone Dark Orange

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    I see major problems..however, why don't you post the errors and then some one of us can walk you thru
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Nov 2005
    Location
    London
    Posts
    23
    The error is this 'class' or 'interface' expected
    Be good coz its good for everyone Dark Orange

  4. #4
    Join Date
    Nov 2005
    Location
    The best country in the world, Texas!
    Posts
    251
    ya know...that code looks suspiciously like one of my homework assignments from this last semester...I might venture to say it looks exactly the same, in fact, down to the text of the comments used.

    well...except for that public function clock() section, which is written in JavaScript instead of Java, and isn't in my copy of the program ^_^

    take out that junk function and the extra } at the end and it should work

  5. #5
    Join Date
    Nov 2005
    Location
    London
    Posts
    23
    What you studying Cytael ? I am doing HND in computing and I get this clock thing from Java Blue J bood thats what we studying and this is one of our objective to understand how clocks works in java.

    BTW what you mean remove the exter } if I do that I get this error ''connot resolve smboly - class function.

    Junk function ?
    Be good coz its good for everyone Dark Orange

  6. #6
    Join Date
    Nov 2005
    Location
    London
    Posts
    23
    bood = book sorry
    Be good coz its good for everyone Dark Orange

  7. #7
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    public function clock() {

    The keyword in red above is a javascript keyword thats what he is refering to and you have to look for an extra curly brace in ur code
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  8. #8
    Join Date
    Sep 2005
    Posts
    1
    I think you've missed an open curly brace { for the else part in public function clock(). Include it and try to compile..of course, after changing the return type of clock().

    regs
    bhu

  9. #9
    Join Date
    Nov 2005
    Location
    London
    Posts
    23
    HI guys again ok after going thourgh this again and again I still can't compile it. I changed alot of things but still won't compile ok here is what I have done so far:

    public class ClockDisplay
    {
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString; // simulates the actual display
    private String amOrpm;
    private String hour;

    /**
    * 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();
    }

    /**
    * 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;
    }

    /**
    * Get display in the format HH:MM.
    */
    public String getDisplay()
    {
    return displayString;
    }

    /**
    * Get Hour in the format HH:.
    */
    public int getHours()
    {
    return hours.getValue();
    }


    /**
    * Get Minutes in the format :MM.
    */
    public int getMinutes()
    {
    return minutes.getValue();
    }
    /**
    * Update the internal string that represents the display.
    */
    private void updateDisplay()
    {
    displayString = hours.getDisplayValue() + ":" +
    minutes.getDisplayValue();
    }

    public void clock()
    {
    if (hours > 11) amOrPm = "PM";
    }
    else {
    if (hours > 12) hours = hours - 12;
    }
    else {
    if (hours == 0) hours = 12;
    }
    else {
    if (minutes <= 9) minutes = "0" + minutes;
    dispTime = "<span class='datetime'>" + hours + ":" + minutes + ":" + amOrPm + "</span>";
    }
    }
    }


    Everything looks ok but still I get these stupid errors
    Be good coz its good for everyone Dark Orange

  10. #10
    Join Date
    Nov 2005
    Location
    The best country in the world, Texas!
    Posts
    251
    I'm currently almost finished with a course in object-oriented analysis and design using that blueJ book as the main text.

    check out that last function:
    PHP Code:
    public void clock() 
    {
    // first off, this is JavaScript, not Java...note how the variables aren't declared
       
    if (hours 11amOrPm "PM"
    // <---- this guy is misplaced
    else {
    if (
    hours 12hours hours 12;
    }
    else {
    if (
    hours == 0hours 12;
    }
    else {
    if (
    minutes <= 9minutes "0" minutes;
    dispTime "<span class='datetime'>" hours ":" minutes ":" amOrPm "</span>"// <-- HTML doesn't work in Java :)
    }
    }
    // <-- that bracket isn't paired up with anything 
    take out that entire last part of your code and see what happens

  11. #11
    Join Date
    Nov 2005
    Location
    London
    Posts
    23
    Hi Cytael and thx for the information ok here what I done but I still get this stupid error '' illegal start of type''



    public void clock()
    {
    if (hours > 11) amOrPm = "PM";
    }
    else {
    if (hours > 12) hours = hours - 12;
    }
    else {
    if (hours == 0) hours = 12;
    }
    else {
    if (minutes <= 9) minutes = "0" + minutes;
    }
    }
    Be good coz its good for everyone Dark Orange

  12. #12
    Join Date
    Nov 2005
    Location
    The best country in the world, Texas!
    Posts
    251
    take that function out of your program completely

  13. #13
    Join Date
    Nov 2005
    Location
    London
    Posts
    23
    If I take that out the clock won't change to am to pm
    Be good coz its good for everyone Dark Orange

  14. #14
    Join Date
    Dec 2005
    Posts
    1

    Exclamation

    Hello all, first time here ! ( Nice to meet all u happy bunch)

    Nez2003 it looks like me and u are doing the same thing.

    I also have to convert a 24HR clock in AM / PM
    Also i have to create a alarm clock

    So if u can help me that would be great !

    And also to other people, Please help a brother out !

    I have been working on this for 1 week now and i dont understant what the hell to do ( pulling my hair out ) and my coursework is in for tommorrow December 09 2005, so please people i am begging you, PLEASE HELP ME !

    Any help giving will be apperciated !

  15. #15
    Join Date
    Dec 2005
    Location
    India
    Posts
    3

    Thumbs up Hai

    Hai Nez,
    ur problem solved or not?

    u need more home work before doing any program.
    I rearranged ur program.If u want send an ack msg to me.

    have a nice day

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles