Eric Cochran
Eric Cochran

Reputation: 1

Showing Remainders instead of Entirety

So I need to write a program that tells how long a specific-sized file will download at a specific rate.

// get user input 
System.out.print("Enter file size (MB): "); 
double fileSize = sc.nextDouble(); 

//get user input 
System.out.print("Enter download speed (MB/sec): "); 
double downloadSpeed = sc.nextDouble(); 
.
.
code body is here
.
.
String message = 
"This download will take approximately\n" + number.format(totalHours) 
    + " hours\n" + number.format(totalMinutes) + " minutes\n" 
    + number.format(totalSeconds) + " seconds\n" ; 
System.out.println(message); 

So that's the basic idea. you enter the desired file size and the desired download rate and it tells you how long it will take to download at the given rate. So I want to know how to make it do i don't get results like:

Welcome to the Download Time Estimator

Enter file size (MB): 20000
Enter download speed (MB/sec): 5.0
This download will take approximately
1 hours
67 minutes
4,000 seconds

Continue? (y/n): 

How can I make it so it gives a remainder in the results? (60 minutes in an hour and 60 seconds in a minute) Does this make sense? here is my code right now:

import java.util.Scanner;
import java.text.NumberFormat;

public class DownloadTimeApp
{

   public static void main(String[] args)
   {
       // welcome the user to the program 
       System.out.println("Welcome to the Download Time Estimator"); 
       System.out.println(); // print a blank line 

       // create Scanner object and start a while loop 
       Scanner sc = new Scanner(System.in); 
       String choice = "y"; 
       while (choice.equalsIgnoreCase("y")) 
       { 
           // get user input 
           System.out.print("Enter file size (MB): "); 
           double fileSize = sc.nextDouble(); 

           //get user input 
           System.out.print("Enter download speed (MB/sec): "); 
           double downloadSpeed = sc.nextDouble(); 

           // calculate the hour, minutes and seconds 
           double totalSeconds; 
           double totalMinutes; 
           double totalHours; 
           int hour; 
           int minutes = 60; 
           int seconds = 60; 


           totalSeconds = fileSize / downloadSpeed; 
           totalMinutes = (totalSeconds)/60; 
           totalHours = (totalSeconds)/3600; 

           NumberFormat number = NumberFormat.getNumberInstance(); 
           number.setMaximumFractionDigits(0); 

           String message = 
           "This download will take approximately\n" + 
           number.format(totalHours) 
           + " hours\n" + number.format(totalMinutes) + " minutes\n" 
           + number.format(totalSeconds) + " seconds\n" ; 
           System.out.println(message); 

           // see if the user wants to continue 
           System.out.print("Continue? (y/n): "); 
           choice = sc.next(); 
           System.out.println();
       }
   }
}

Upvotes: 0

Views: 45

Answers (3)

notphilphil
notphilphil

Reputation: 385

Use some while statements to break down each totalSeconds, totalMinutes, and totalHours into numbers that reflect the whole download time.

Using some in your code would look like

import java.util.Scanner;
import java.text.NumberFormat;

public class DownloadTimeApp
{

   public static void main(String[] args)
   {
       // welcome the user to the program 
       System.out.println("Welcome to the Download Time Estimator"); 
       System.out.println(); // print a blank line 

       // create Scanner object and start a while loop 
       Scanner sc = new Scanner(System.in); 
       String choice = "y"; 
       while (choice.equalsIgnoreCase("y")) 
       { 
           // get user input 
           System.out.print("Enter file size (MB): "); 
           double fileSize = sc.nextDouble(); 

           //get user input 
           System.out.print("Enter download speed (MB/sec): "); 
           double downloadSpeed = sc.nextDouble(); 

           // calculate the hour, minutes and seconds 
           int totalSeconds = 0; 
           int totalMinutes = 0; 
           int totalHours = 0; 
           /*int hour; 
           int minutes = 60; 
           int seconds = 60;*/
           //^^ Useless information


           totalSeconds = fileSize / downloadSpeed; 
           while (totalSeconds >= 60) {
              totalMinutes = totalMinutes + 1;
              totalSeconds = totalSeconds - 60;
           }
           while (totalMinutes >= 60) {
              totalHours = totalHours + 1;
              totalMinutes = totalMinutes - 60;
           }
           //Add same thing for days if you'd like

           /*NumberFormat number = NumberFormat.getNumberInstance(); 
           number.setMaximumFractionDigits(0);*/
           //^^ Useless formatting

           String message = 
           "This download will take approximately\n" + 
           totalHours + " hours\n" +
           totalMinutes + " minutes\n" +
           totalSeconds + " seconds\n" ; 
           System.out.println(message); 

           // see if the user wants to continue 
           System.out.print("Continue? (y/n): "); 
           choice = sc.next(); 
           System.out.println();
       }
   }
}

If you put in fileSize as 1000MB and downloadSpeed as 10MB/sec, then you would have

totalSeconds = 40;
totalMinutes = 1;
totalHours   = 0;

Upvotes: 1

Krzysztof Cichocki
Krzysztof Cichocki

Reputation: 6412

is this what you are looking for?:

import java.util.Scanner;
import java.text.NumberFormat;

public class DownloadTimeApp
{

   public static void main(String[] args)
   {
       // welcome the user to the program 
       System.out.println("Welcome to the Download Time Estimator"); 
       System.out.println(); // print a blank line 

       // create Scanner object and start a while loop 
       Scanner sc = new Scanner(System.in); 
       String choice = "y"; 
       while (choice.equalsIgnoreCase("y")) 
       { 
           // get user input 
           System.out.print("Enter file size (MB): "); 
           double fileSize = sc.nextDouble(); 

           //get user input 
           System.out.print("Enter download speed (MB/sec): "); 
           double downloadSpeed = sc.nextDouble(); 

           // calculate the hour, minutes and seconds 
           double totalSeconds; 
           double totalMinutes; 
           double totalHours; 
           int hour; 
           int minutes = 60; 
           int seconds = 60; 


           totalSeconds = fileSize / downloadSpeed; 
           totalMinutes = ((totalSeconds)/60)%60; 
           totalHours = (totalSeconds)/3600; 
           totalSeconds - totalSeconds % 60;
           NumberFormat number = NumberFormat.getNumberInstance(); 
           number.setMaximumFractionDigits(0); 

           String message = 
           "This download will take approximately\n" + 
           number.format(totalHours) 
           + " hours\n" + number.format(totalMinutes) + " minutes\n" 
           + number.format(totalSeconds) + " seconds\n" ; 
           System.out.println(message); 

           // see if the user wants to continue 
           System.out.print("Continue? (y/n): "); 
           choice = sc.next(); 
           System.out.println();
       }
   }
}

As mentioned in previous answer, in java there is a modulus operator %, which is perfect to this kind of jobs like extracting the seconds remainding to full minutes, or minutes remainding to full hour. basically the % operator calculates the result like this:

1%60=1;
2%60=2;
3%60=3;
...
61%60=1;
62%60=2;
63%60=3;
...
121%60=1;
122%60=2;
123%60=3;
...
and so on...and so on...

Upvotes: 1

JoshKopen
JoshKopen

Reputation: 960

In Java, there is the modulus function which uses the % operator. You can use this like when you would division and you will get the remainder instead of the division result. For example, your minutes could be minutesDisplayed = minutes % 60in order to get what you are looking for.

Upvotes: 1

Related Questions