This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

Hi. I new to Java and learning on my own from a book. One of the exercises is to calculate the download time of a 50MB file. I have figured out the hours and minutes but my seconds are off. Here is the code:

do { System.out.print( "\n\nEnter the size of the file in megabytes(MB): " ); megabytes = sc.nextDouble();

// Convert megabytes to kilobytes because the value to divide must be in seconds. The division of // kb and KB_PER_SEC will cancel out the kilobytes and leave only seconds, which is what we want. kilobytes = megabytes * 1024; totalSeconds = kilobytes / KB_PER_SEC;

// There are 3600 seconds in one hour so divide the total // number of seconds by 3600 to get the number of hours. hours = totalSeconds / 3600;

// The minutes are the remainder that's left after getting the hours. Since // there are 60 seconds in one minute divide that remainder by 60. minutes = ( totalSeconds % 3600 ) / 60;

// The seconds are what's left over after getting the hours and minutes. seconds = ( totalSeconds % 3600 ) - ( minutes * 60 );

//// Convert megabytes to kilobytes because the value to divide must be in seconds. The division of //// kb and KB_PER_SEC will cancel out the kilobytes and leave only seconds, which is what we want. //kilobytes = megabytes * 1024; //BigDecimal bdKilobytes = new BigDecimal( Double.toHexString( kilobytes ) ); //bdKilobytes = bdKilobytes. //totalSeconds = kilobytes / KB_PER_SEC; // //// There are 3600 seconds in one hour so divide the total //// number of seconds by 3600 to get the number of hours. //hours = totalSeconds / 3600; //BigDecimal bdHours = new BigDecimal( Double.toString( hours ) ); // //// The minutes are the remainder that's left after getting the hours. Since //// there are 60 seconds in one minute divide that remainder by 60. //minutes = ( totalSeconds % 3600 ) / 60; //BigDecimal bdMinutes = new BigDecimal( Double.toString( minutes ) ); // //// The seconds are what's left over after getting the hours and minutes. //seconds = ( totalSeconds % 3600 ) - ( minutes * 60 ); //BigDecimal bdSeconds = new BigDecimal( Double.toString( seconds ) );

I think that I am loosing something when I do the division for totalSeconds. I should point out that when I used a an int value such as 4206 seconds instead of doing a calculation I get the correct answer(1 hour, 10 mins 6 secs)

KP>

Until lions have their historians tales of the hunt will always glorify the hunter.<br /> <br />-African proverb

K Parsons
Greenhorn

Joined: Sep 08, 2007
Posts: 4

posted

0

I forgot to add that in the book it shows the result for a 50MB file as 2 hours 44 minutes and 6 seconds. I got the 2 and 44 but I'm getting 0 seconds.