Small Calculation Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • H0kage
    New Member
    • Oct 2006
    • 6

    #1

    Small Calculation Help

    I am Suppose To Create A Program That We Enter A Number Of Seconds Like

    95362875

    And Return To Me that number in the following way

    Years:3
    Weeks:1
    Days:1
    Hours:17
    Minutes:41
    Seconds:15

    Its Suppose To Be In GUI Design which i have already made and its working my problem is the calculations i am suppose to enter in the action performed section of the button.
    Dead Line Is Tommorow Morning 9.00am GMT+2

    Thank You In Advance
  • mharrison
    New Member
    • Nov 2006
    • 26

    #2
    Originally posted by H0kage
    I am Suppose To Create A Program That We Enter A Number Of Seconds Like

    95362875

    And Return To Me that number in the following way

    Years:3
    Weeks:1
    Days:1
    Hours:17
    Minutes:41
    Seconds:15

    Its Suppose To Be In GUI Design which i have already made and its working my problem is the calculations i am suppose to enter in the action performed section of the button.
    Dead Line Is Tommorow Morning 9.00am GMT+2

    Thank You In Advance
    normally i wouldn't help as the deadline is so close. but i was bored. The trick is to work out the remainders using modulo (%) and then use a simple division to get the amounts.

    hence:

    Code:
        public void timeConvert(int secs) {
    //         int secs = 95362875;
    
            // first set up the constant values for time.
            int YearSecVal = 60 * 60 * 24 * 7 * 52;
            int WeekSecVal = 60 * 60 * 24 * 7;
            int DaySecVal = 60 * 60 * 24;
            int HourSecVal = 60 * 60;
            int MinSecVal = 60;   
            
            // next work out the remainding seconds from each calc.
            int newWeekSecs = secs % YearSecVal;
            int newDaySecs = newWeekSecs % WeekSecVal;
            int newHourSecs = newDaySecs % DaySecVal;
            int newMinSecs = newHourSecs % HourSecVal;
            int newSecs = newMinSecs % MinSecVal;
    
            // print it all out!
            System.out.println(secs / YearSecVal + " years");
            System.out.println(newWeekSecs / WeekSecVal + " weeks");
            System.out.println(newDaySecs / DaySecVal + " days");
            System.out.println(newHourSecs / HourSecVal + " hours");
            System.out.println(newMinSecs / MinSecVal + " mins");
            System.out.println(newSecs + " secs");
    
        }
    and im also hoping your deadline has passed now :|

    Theres prob a much nicer recursive way to do this, but simplicity was never a strong point.

    Comment

    • H0kage
      New Member
      • Oct 2006
      • 6

      #3
      Thank You M8 your help is appreciated.Yup I missed the deadline but at least i will understand its function.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by H0kage
        Thank You M8 your help is appreciated.Yup I missed the deadline but at least i will understand its function.
        And hopefully you'll post your problems much earlier too. Sorry you did not get a response on time though. I was away myself at the time.

        Comment

        • H0kage
          New Member
          • Oct 2006
          • 6

          #5
          Like you say i will try to post earlier but i wanted it to fight it till the last day
          before the end since i dont like to get ready solutions.I want Tips and guidance :)

          Comment

          Working...