# Calculating download time

K Parsons

Greenhorn

Posts: 4

posted 8 years ago

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:

import java.util.Scanner;

import java.math.BigDecimal;

public class DownloadTime

{

public static void main( String parameters[] )

{

final double KB_PER_SEC = 5.2;

double hours = 0;

double minutes = 0;

double seconds = 0;

double megabytes = 0;

double kilobytes = 0;

double totalSeconds = 0;

Scanner sc = new Scanner( System.in );

String choice = "n";

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 ) );

// System.out.println( "\nA 56K modem will take " + bdHours + " hours, " + bdMinutes + " minutes, and " + bdSeconds + " seconds." );

// System.out.print( "\nContinue?(y/n): " );

// choice = sc.next();

System.out.println( "\nA 56K modem will take " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds." );

System.out.print( "\nContinue?(y/n): " );

choice = sc.next();

}

while ( choice.equalsIgnoreCase( "y" ) );

}

}

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>

import java.util.Scanner;

import java.math.BigDecimal;

public class DownloadTime

{

public static void main( String parameters[] )

{

final double KB_PER_SEC = 5.2;

double hours = 0;

double minutes = 0;

double seconds = 0;

double megabytes = 0;

double kilobytes = 0;

double totalSeconds = 0;

Scanner sc = new Scanner( System.in );

String choice = "n";

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 ) );

// System.out.println( "\nA 56K modem will take " + bdHours + " hours, " + bdMinutes + " minutes, and " + bdSeconds + " seconds." );

// System.out.print( "\nContinue?(y/n): " );

// choice = sc.next();

System.out.println( "\nA 56K modem will take " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds." );

System.out.print( "\nContinue?(y/n): " );

choice = sc.next();

}

while ( choice.equalsIgnoreCase( "y" ) );

}

}

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

Posts: 4

Campbell Ritchie

Sheriff

Posts: 48954

60

K Parsons

Greenhorn

Posts: 4

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

The formula I have works. If I use the example of 4206 seconds instead of reading a value in and doing a division it works out the following way:

(1) There are 3600 seconds in one hour so divide by 3600.

4206 / 3600 = 1 ->This is the number of hours

4206 % 3600 = 606 -> This is the remainder from the division

(2) There are 60 seconds in one minute so divide 606 by 60 to get the minutes

606 / 60 = 10 -> This is the number of minutes

606 % 60 = 6 -> This remainder is the number of seconds.

(3) To get the number of seconds I used the following formula:

(4206 % 3600) - (60 * 10)

(totalNumberOfSeconds % secondsPerHour) - (secondsPerMinute * minutes)

KP>

(1) There are 3600 seconds in one hour so divide by 3600.

4206 / 3600 = 1 ->This is the number of hours

4206 % 3600 = 606 -> This is the remainder from the division

(2) There are 60 seconds in one minute so divide 606 by 60 to get the minutes

606 / 60 = 10 -> This is the number of minutes

606 % 60 = 6 -> This remainder is the number of seconds.

(3) To get the number of seconds I used the following formula:

(4206 % 3600) - (60 * 10)

(totalNumberOfSeconds % secondsPerHour) - (secondsPerMinute * minutes)

KP>