Results 1 to 13 of 13
  1. #1
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default clock class program

    my code is giving me errors and i am stuck...someone please help me.

    my code is based on these UML Specs.

    Clock

    - sec : int
    - min : int
    - hr : int
    - day : int
    - mon : int
    - yr : int

    + Clock( )
    + getSeconds( ) : int
    + getMinutes( ) : int
    + getHours( ) : int
    + getDays( ) : int
    + getMonths( ) : int
    + getYears( ) : int
    + setTimeDate(int, int, int, int, int, int)
    + tick( )
    + toString( ) : String

    -------------------------------------------------------------
    The hr instance variable records time on a 24 hour clock: 0 is midnight, 6 is 6am, 12 is noon and 23 is 11pm.

    The constructor should create object with instance variables initialized to the time and date 0:00:00 January 1, 1 A.D.

    The setTimeDate method should initialize the values of the instance variables. You choose the order of the parameters. You also decide what to do if any values are out of range.

    When the tick method is executed, add one to sec. If the resulting value of sec is 60, set is back to 0 and update the other instance variables as necessary.


    2. Write a TestClock class that tests all of the methods in your Clock class.
    At a minimum, include these dates when testing the tick method: January 31, April 30, February 28 for leap and non-leap years, February 29 for leap and non-leap years, December 31.


    3. Add code to your TestClock class that creates a Clock object initialized to the date and time 12 noon, January 24, 2008. Compute the dates 1 million and 10 million seconds in the future by repeatedly calling the tick method with for loops.

    -----------------------------------------------------------------------
    HERE IS MY CODE


    public boolean isLeapYear( )
    {
    return yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0);
    }

    public void tick( )
    {
    sec++;

    if (sec == 60)
    {
    sec = 0;
    min++;
    }

    if (min == 60)
    {
    min = 0;
    hr++;
    }

    if (hr == 24)
    {
    hr = 0;
    day++;
    }

    switch (mon)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    if (day == 32)
    {
    day = 1;
    mon++;
    }
    break;

    case 4:
    case 6:
    case 9:
    case 11:
    if (day == 31)
    {
    day = 1;
    mon++;
    }
    break;

    case 2:
    if ((isLeapYear( ) && day == 30) ||
    (!isLeapYear( ) && day == 29))

    {
    day = 1;
    mon++;
    }
    break;

    default:
    System.out.println("Illegal month.");
    break;
    }
    }

    public String getMonthName( )
    {
    switch(mon)
    {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";
    default: return "Illegal month";
    }

    if (mon == 13)
    {
    mon = 1;
    year++;
    }
    }

    public String toString( )
    {
    if (hr == 0)
    return String.format("%s %d, %d 12:%02d:%02d AM",
    getMonthName( ), day, yr, min, sec);
    else if (hr < 12)
    return String.format("%s %d, %d %02d:%02d:%02d AM",
    getMonthName( ), day, yr, hr, min, sec);
    else if (hr == 12)
    return String.format("%s %d, %d 12:%02d:%02d PM",
    getMonthName( ), day, yr, min, sec);
    else if (hr > 12 && hr < 24)
    return String.format("%s %d, %d %02d:%02d:%02d PM",
    getMonthName( ), day, yr, hr % 12, min, sec);
    else
    return "Illegal month";
    }

  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    You need to post the full source code of your class, this is only part of it. Also to make it readable please use code tags around your code. Finally what are the problems you are having?
    -- Hope that helps

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Compiles okay now.
    Java Code:
    public class ClockRx {
        int sec; 
        int min;
        int hr;
        int day;
        int mon;
        int yr;
    
        public boolean isLeapYear( ) {
            return yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0);
        }
    
        public void tick( ) {
            sec++;
            if (sec == 60) {
                sec = 0;
                min++;
            }
    
            if (min == 60) {
                min = 0;
                hr++;
            }
    
            if (hr == 24) {
                hr = 0;
                day++;
            }
    
            switch (mon) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if (day == 32) {
                        day = 1;
                        mon++;
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    if (day == 31) {
                        day = 1;
                        mon++;
                    }
                    break;
                case 2:
                    if ((isLeapYear( ) && day == 30) ||
                        (!isLeapYear( ) && day == 29)) {
                        day = 1;
                        mon++;
                    }
                    break;
                default:
                    System.out.println("Illegal month.");
            }
        }
    
        public String getMonthName( ) {
            switch(mon) {
                case 1: return "January";
                case 2: return "February";
                case 3: return "March";
                case 4: return "April";
                case 5: return "May";
                case 6: return "June";
                case 7: return "July";
                case 8: return "August";
                case 9: return "September";
                case 10: return "October";
                case 11: return "November";
                case 12: return "December";
                default: return "Illegal month";
            }
    /*
            if (mon == 13) {
                mon = 1;
                // "year" has not been declared
    //            year++;
                // "yr" has
                yr++;
            }
    */
        }
    
        public String toString( ) {
            if (hr == 0)
                return String.format("%s %d, %d 12:%02d:%02d AM",
                                      getMonthName( ), day, yr, min, sec);
            else if (hr < 12)
                return String.format("%s %d, %d %02d:%02d:%02d AM",
                                      getMonthName( ), day, yr, hr, min, sec);
            else if (hr == 12)
                return String.format("%s %d, %d 12:%02d:%02d PM",
                                      getMonthName( ), day, yr, min, sec);
            else if (hr > 12 && hr < 24)
                return String.format("%s %d, %d %02d:%02d:%02d PM",
                                      getMonthName( ), day, yr, hr % 12, min, sec);
            else
                return "Illegal month";
        }
    }

  4. #4
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    thanks....what do i need to do with it to make it run...It is giving me a error in the terminal window saying

    exception in thread main java.lang.nosuchmethoderror..........

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    exception in thread main java.lang.nosuchmethoderror
    Means that the jvm could not find a main method in the class.
    Java Code:
    public class ClockRx {
        ... everything same as before ...
        /** add a main method */
        public static void main(String[] args) {
            ClockRc clock = new ClockRx();
            // now you can access fields and call
            // methods in the ClockRx class.
        }
    }

  6. #6
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    i dont really know how to do that could you help me?.....so are you saying that once we figure out how to do that main method, my program will work? and give a 24 hour clock?
    Last edited by jvasilj1; 01-31-2008 at 06:36 AM. Reason: mistake

  7. #7
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    Yes that is what hardwired is saying. Whenever you try to run a java class the system looks for a method with the signature

    Java Code:
        public static void main(String[] args)
    and that is where your code starts executing. What you need to do is instantiate your clock class and then call the methods as you need them

    Java Code:
        public static void main(String[] args) {
            ClockRx clock = new ClockRx();
            // now you can access fields and call
            // methods in the ClockRx class. for example
    
           // print details of clock
           System.out.println(clock.);
    
          // tick the clock
           clock.tick();
    
           // print details of clock
           System.out.println(clock.);
        }
    -- Hope that helps

  8. #8
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    can u please add that to my code in the post above, so it will work for me?

  9. #9
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    According to your original spec the testing should be in a separate class called TestClock so:

    Java Code:
    public class TestClock {
    
        public static void main(String[] args) {
            ClockRx clock = new ClockRx();
            // now you can  call methods in the ClockRx class. for example
    
           // print details of clock
           System.out.println(clock);
    
           // tick the clock
           clock.tick();
    
           // print the current month name of clock
           System.out.println(clock.getMonthName());
    
           // call any other methods that you want to call
    
        }
    
    }
    Last edited by jelly; 02-01-2008 at 05:55 PM.
    -- Hope that helps

  10. #10
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    yes i need the test clock...how does that work...so i have my one program called clockRX and a completely seperate program called TestClock...

    can you post the completed code for both programs, since i dont understand where to go with this.

  11. #11
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    The code I posted above is a skeleton for the TestClock program there are three calls to your ClockRx program, you should be able to work out some others. Have a go at completing some code, post what you get an dpoeple will help but posting completed code will not help you. When you get into a workplace you will need to write your own so best to start now
    -- Hope that helps

  12. #12
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    ok well the lets go back to my orignal program of the ClockRx and why it is giving me the main error but it compiles....what do i need to add to it for it to run and count the time?...

  13. #13
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    Your spec at the beginning says you should do that by creating a separate test class called TestClock - that's given in my earlier post. The spec also says that your original Clock class should have a constructor that does the following:

    "The constructor should create object with instance variables initialized to the time and date 0:00:00 January 1, 1 A.D."

    You currently only have the default constructor so you will need to override that and set your instance variable to the correct values for the date given.

    Do that first and then work down the instructions in your spec - it is a pretty clear spec - shout if you get code problems.

    Just a query. why did you call your class ClockRx?
    -- Hope that helps

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 09:48 PM
  2. Could not find the main class, program will exit.
    By aryubi in forum New To Java
    Replies: 39
    Last Post: 02-19-2010, 10:02 AM
  3. Help please in digital clock
    By jaidod in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:05 PM
  4. Pogo clock problem
    By tampacurt in forum New To Java
    Replies: 0
    Last Post: 11-17-2007, 02:18 AM
  5. dynamically updating clock
    By malakaherath in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 07:57 PM

Posting Permissions

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