Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help with Clock (advance and reverse day,hour,minute)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Clock (advance and reverse day,hour,minute)

    FYI - This is my first post. This is an assignment for a JAVA I class. I cannot import anything or change the public class. My problem right now focusing on the ADVANCE method, I don't know how to advance the day. So my question is how should I get a if/then, for, or while component in there to anchor my advancing minutes to my days?

    public class ClockDriver {
     
        public static void main(String[] args) {
            Clock MyClock = new Clock("Thursday", 12,0); // Instantiate a clock object
     
    	System.out.println(MyClock); // Should display: Thursday, 12:00 p.m.
            System.out.println(); // print blank line
     
            MyClock.advance(150); // advance time 150 minutes (2 hours 30 minutes)
    	System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.
            System.out.println();
     
            MyClock.advance(585); // advance time by 585 minutes (9 hours 45 minutes)
    	System.out.println(MyClock); // Should display: Friday, 12:15 a.m.
            System.out.println();
     
            MyClock.advance(10080); // advance time by 10080 minutes (7 days)
    	System.out.println(MyClock); // Should display: Friday, 12:15 a.m.
            System.out.println();
     
            MyClock.reverse(585); // reverse time by 585 minutes (9 hours 45 minutes)
    	System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.
            System.out.println();
     
            MyClock.reverse(17280); // reverse time by 17280 minutes (12 days)
    	System.out.println(MyClock); // Should display: Saturday, 2:30 p.m.
            System.out.println(); 
        } // end of main
     
    } // end of class ClockDriver
     
    class Clock{
        /***************************************************************
         * Objects of this class represent time (in hours and minutes) *
         * The time data is stored in 24 hour (HHMM military format)   *
         ***************************************************************/
        private int day_of_week; // valid range of values is 1 thru' 7
        private int hours; // range of values is 0 thru' 23
        private int minutes; // range of values is 0 thru' 59
        private String[] dow = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     
        public Clock(String s, int h, int m){
           //Initialize day_of_week, hours and minutes  - 20 points
            if (s.equals("Sunday")) {
                day_of_week = 1; 
            }
            else if (s.equals("Monday")) {
                day_of_week = 2;
            }
            else if (s.equals("Tuesday")) {
                day_of_week = 3;
            }
            else if (s.equals("Wednesday")) {
                day_of_week = 4;  
            }
            else if (s.equals("Thursday")) {
                day_of_week = 5;
            }
            else if (s.equals("Friday")) {
                day_of_week = 6;
            }
            else if (s.equals("Saturday")) {
                day_of_week = 7;    
            } else { 
                day_of_week = -1;
                        }
            hours = h;
            minutes = m;
     
        }
     
        public void advance(int m){
            // advance day_of_week, hours and minutes - 30 points
     
            int allMinA = (hours * 60) + minutes + m;
            minutes = allMinA % 60;
            hours = (allMinA / 60) % 24;
        }
     
        public void reverse(int m){
    	// reverse day_of_week, hours and minutes - 30 points   
     
            int allMinB = (hours * 60) + minutes - m;
            minutes = allMinB % 60;
            hours = (allMinB / 60) % 24;
        }
     
        public String toString(){
    	//return Day of the week, hours and minutes as a string to be displayed - 20 points
     
            if (hours >12){
                hours = hours - 12;
            }
            String result = dow[day_of_week - 1] + ", " + String.format("%2d", hours) + ":" + String.format("%02d", minutes) + " amORpm > ";
     
            return result;
        }
    } // end of class Clock


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    You seem to be figuring out how to advance the hours based on the minutes, so can't you do the same thing, advance the days based on the hours?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    I can visualize a set of total minutes that I can divide by to find out how many days, hours, and minutes but... the way these methods are set up I am having a hard time getting total minutes. Because hours needs to be set to zero after the first advance otherwise it adds unnecessarily to totalMinutes. So below I have a new approach. Once again any suggestions would be appreciated.

     public void advance(int m){
            // advance day_of_week, hours and minutes - 30 points
     
            minutes = minutes + m;
            if (minutes < 60){
     
            } else {
     
                hours = hours + (minutes / 60);
                minutes = minutes % 60;
     
                if (hours < 24){
                } else {
                    int addDays = day_of_week + (hours / 24);
                    if (addDays < 8){
                        day_of_week = addDays;
                    } else {
                        int weekAdds = addDays - 8;
                        day_of_week = weekAdds;
     
                    }
     
                }
            }
     
        }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    So is that working now? If not, what happens? What did you expect to happen?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    No, the code in my previous post doesn't work. I can't keep track of the days in any consistent fashion with that approach. My problem is I don't understand the best approach to keep track of time in this program. I need a counter that knows the starting point and can keep track of minutes (0-59) and hours (0-23) and days (1-7). I think I am now going to stop keeping track of total minutes and try to keep track of total hours. Hours are what I need to know when I need to add ++ to days.
    ----------
    For example, if I start at Thursday at 12:00pm and time advances by 10 days (240 hrs or 14,400 mins) then the clock would stay at 12:00pm but the day should be Sunday. If Sun = 1, Mon = 2, .... Sat = 7, then I have (Thursday) 5 + 10 (days) = 15. But how does that 15 help me?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    Think of Sunday as zero, and Saturday as six.

    That means Thursday is 4. Add 10 days, and you're at 14. What index do you want? What if you had added 15 days? 16? Are you starting to see a pattern?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    Quote Originally Posted by KevinWorkman View Post
    Think of Sunday as zero, and Saturday as six.

    That means Thursday is 4. Add 10 days, and you're at 14. What index do you want? What if you had added 15 days? 16? Are you starting to see a pattern?
    I'm with you having it start at 0 but in his instructions he says that valid ranges for the days are 1 - 7. But I understand your point.

    If Thursday is 4 and I add 10 days I'm at 14 for the number of days. The actual day would be Sunday.

    4-thurs(4)
    5-fri(5)
    6-sat(6)
    0-sun(7)
    1-mon(8)
    2-tues(9)
    3-wed(10)
    4-thurs(11)
    5-fri(12)
    6-sat(13)
    0-sun(14)

    How would I get the "0" (for Sunday) I need from the integer 14?
    Last edited by whyld4900; November 8th, 2011 at 05:56 PM.

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    I'm much happier with this method for keeping track of the correct hours but I am still having issues keeping track of days. Any help out there? Kevin? Anyone! lol

     public void advance(int m){
            // advance day_of_week, hours and minutes - 30 points
     
            minutes = minutes + m;
     
     
            if(minutes > 59){
                int spillOverMins = minutes % 60;
                int spillOverHours = minutes / 60;
                hours = hours + spillOverHours;
                minutes = spillOverMins;
            } else {
              // not sure if I need something here
            }
     
            if (hours > 23){
                int spillOverHours2 = hours % 24;
                int spillOverDays = hours / 24;
                hours = hours + spillOverHours2;
     
              if(spillOverDays + day_of_week > 7){
     
                    // what to put here that will account for the original day_of_week
              } else {
     
                  day_of_week = day_of_week + spillOverDays;
     
              }  
            }
     
        }

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    Have you considered the % operator?

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  10. The Following User Says Thank You to Freaky Chris For This Useful Post:

    KevinWorkman (November 9th, 2011)

  11. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Clock (advance and reverse day,hour,minute)

    The advance method works:
    public void advance(int m){
            // advance day_of_week, hours and minutes - 30 points
     
            minutes = minutes + m;
     
              if(minutes > 59){
                int spillOverMins = minutes % 60;
                int spillOverHours = minutes / 60;
     
                hours += spillOverHours;
                minutes = spillOverMins;
            } // END if(minutes > 59){
     
            if (hours > 23){
                int spillOverHours2 = hours % 24;
                int spillOverDays = hours / 24;
     
                hours = spillOverHours2;
                day_of_week += spillOverDays;
            } // END if (hours > 23){
     
     
            if (day_of_week > 7) {
                day_of_week = day_of_week%7;
                if (day_of_week == 0) day_of_week = 7;
            }// END if (day_of_week > 7) {
     
        } // END advance()

    but the reverse is not as easy to figure out. What is best way to get keep of time going backwards, in this case?

     public void reverse(int m){
    	// reverse day_of_week, hours and minutes - 30 points   
     
             int backMins = m % 60;
             int backHrs = m / 60;
             int backDays = backHrs / 24;
     
             int addBackHr = 0;
             int addBackDay = 0;
     
              int minCheck = minutes - backMins;
              if (minCheck < 0) {
                  addBackHr = 1;
                  minutes = minCheck * -1;
     
              } else {
                  minutes = minCheck;
              } // END   if (minCheck < 0) {
     
              int hrCheck = hours - (backHrs + addBackHr);
     
          if (hrCheck < 0) {
     
              hrCheck *= -1;
     
          if (hrCheck > 24) hrCheck = (backHrs + addBackHr) % 24;
          if (hours == 0) hours = 24;
          hours = hours - hrCheck;
          } else {
              hours = hours - hrCheck;
          }// END  if (tmpHoldHrs < 0) {
     
          day_of_week -= (backDays + addBackDay);
     
           if(day_of_week == 0) day_of_week = 1;
          if (day_of_week < 0){
     
     
           day_of_week *= -1;
           if(day_of_week > 7) day_of_week = day_of_week % 7;
     
          }
     
        } // END reverse()

Similar Threads

  1. Array month/day/hour/min/sec incomplete java code
    By mightyking in forum Collections and Generics
    Replies: 12
    Last Post: September 18th, 2011, 08:46 PM
  2. Pls guide me in advance java
    By deepaks50 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 19th, 2011, 08:55 AM
  3. Help with looping etc THANKS ALOT IN ADVANCE
    By moesom in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 15th, 2011, 10:43 AM
  4. Replies: 3
    Last Post: February 22nd, 2011, 07:43 PM
  5. help with clock class
    By collinsislee in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 01:53 PM