Results 1 to 6 of 6
  1. #1
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default So I'm supposed to convert seconds to hours...

    I went to class yesterday and the teacher says you should be on each lab for about 30 minutes and you should be done, but it takes me at least 4-5 hours to get everything together... I'm doubting whether this is right for me...I like working on this but man I take too much time?

    having said that, I'm having problems dividing seconds into hours to get it in the standard time format and the am/pm time format. It looks easy, since you divide 60 by the seconds entered to get hours, divide by 60 again to get the minute then again to get seconds...but it comes out weird?

    1. Divide the seconds entered by the user by the number of seconds in an hour
    (SECONDS_X_HOUR), the result will be the hours in standard time.
    2. Compute the remainder of seconds entered by the user by the number of seconds
    in an hour (SECONDS_X_HOUR).
    3. Divide the remainder obtained in the previous step by the number of seconds in a
    minute (SECONDS_X_MINUTE), the result of this division will be the minutes.
    4. The remainder of this last division will be the seconds.
    int SECONDS_X_HOUR = 3600;
    int SECONDS_X_MINUTE = 60;
    int AM_PERIOD = 43199;

    System.out.print("Please enter the time in seconds: ");
    long Answer = keyboard.nextLong();

    if (Answer < 0){
    System.out.println("You must enter a number bigger than 0!");
    }
    else {


    // Standard Format

    double standardHour = (Answer / SECONDS_X_HOUR);
    double standardMinute = ((standardHour%) /SECONDS_X_MINUTE);
    double standardSecond = ((standardMinute%) / SECONDS_X_MINUTE);

    System.out.println("Standard Format: " + standardHour + "h., " + standardMinute + "m., " + standardSecond + "s.");
    and here's what I've come with, and there is an error on the standardMinute and standardSecond wit the % (Syntax error). at first I just divided the standardHour by seconds_x_minute and standardminute by seconds_x minute but number for hours, minutes and seconds come out the same (figured I pretty much divided the same number for all 3 cases but i'm not too sure). Also I'm not even sure if I'm using the correct data type?

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    As far as I know, your only mistake is in the use of the '%'-operator, do you know how that operator works, what it does?

  3. #3
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    remainder, supposed to take the remainder of the divided number between the x % y, isn't it? also sometimes the answer comes out to be a loooong decimal, thinking about it now I'm not sure if decimals should happen at all... is that right?

    ohohhhh i got it thanks I was on this lab for like 4 hours now and was really frustrated :< confused the definition of remainder with the decimal points you get when you divide with like a calculator or something
    Last edited by nope; 02-13-2011 at 09:16 PM.

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    So is it working now?

  5. #5
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    At least that part, yes, working on figuring out the AM/PM part now.

    Got everything but the last part: The supposed answer should have returned without any decimals, but I see .0 after all the hr/min/sec. I haven't learned about math.round nor BigInteger yet and I vaguely remember %2 or something like that (formatter apparently), how do I set it up so no decimals show? "%.2f" is supposed to be with 1 decimal place, isn't it?
    Last edited by nope; 02-13-2011 at 11:18 PM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    How about declaring your variables as int instead of double, then you don't get the fractional part.

Similar Threads

  1. Replies: 4
    Last Post: 09-09-2010, 11:15 AM
  2. small method isn't supposed to go under 0
    By senca in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 01:23 PM
  3. Replies: 3
    Last Post: 04-05-2010, 01:08 PM
  4. Convert microseconds to seconds
    By Basit56 in forum New To Java
    Replies: 4
    Last Post: 12-31-2009, 05:54 AM
  5. Help interpreting what a class is supposed to do
    By bornwithnoname in forum New To Java
    Replies: 2
    Last Post: 11-20-2008, 12:19 AM

Posting Permissions

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