// Import anything you need to import
public class Download {
public static void main(String[] args){
// YOUR CODE GOES HERE
}
// To call this method, you simply type in "randomIntInRange"
// and follow it immediately with parentheses containing
// the lower bound, a comma, and the upper bound. For
// example, to simulate rolling a 6-sided die, you might use
// the following line of code:
//
// int dieRoll = randomIntInRange(1, 6);
//
// NOTE: Once you generate this value, you need to save it in
// a variable because the method will return a DIFFERENT
// value EACH time you call it.
private static int randomIntInRange(int low, int high) {
int multiplier = high - (low - 1);
return (int)(Math.random() * multiplier) + low;
}
// This method allows you to pause your program for
// some number of MILLISECONDS. 1 second = 1000 ms.
// For this particular program, if you wanted to pause
// 50 milliseconds after printing each '*', you would
// use the following line of code:
//
// pause(50);
private static void pause(int milliseconds){
try {
Thread.sleep(milliseconds);
} catch (Exception e) {
;
}
}
}
(NOTE: There is no genuine downloading of actual files taking place here, just the "idea of" such!)
Your task, then, is to work in the area of the code I specify. In the main method, you will need to do the following things:
- Declare and initialize whatever variables you need for the program.
- Your program will have two while loops, one nested inside the other.
- Your outer while loop is responsible the following sequence of events:
- Generating a random file size between 10 million and 100 million.
- Downloading the file.
- Updating the number of files downloaded so far.
- Asking if the user wants to download another file, and altering a value accordingly such that the outer while loop eventually stops, depending on the user's response.
- Your inner while loop is what downloads the file. This involves printing a * character for each 1 million (of the file's total size) that gets downloaded and keeping track of the progress on downloading the file (so that you know when the download is complete.). I also recommend pausing for 50 milliseconds after each * that you print, just because it makes for a better download simulation.
- At the end, your program reports how many files total the user downloaded.
If you would like to test out the program itself, to get a better idea of how it should run, here is a way:
- Log into the Linux system
- Enter this command: cd /home/ckelly/it114/test_code
- Enter this command: java Download
Note that because of user input and random values, the output will look different each time you run the program.
Program output example:
+ Expand Output
Downloading a file of 15149154 bytes.
****************COMPLETE!
Another file? true
Downloading a file of 32114196 bytes.
*********************************COMPLETE!
Another file? true
Downloading a file of 49946355 bytes.
...
- Collapse Output
Downloading a file of 15149154 bytes.
****************COMPLETE!
Another file? true
Downloading a file of 32114196 bytes.
*********************************COMPLETE!
Another file? true
Downloading a file of 49946355 bytes.
**************************************************COMPLETE!
Another file? true
Downloading a file of 70525001 bytes.
***********************************************************************COMPLETE!
Another file? true
Downloading a file of 74457562 bytes.
***************************************************************************COMPLETE!
Another file? true
Downloading a file of 88362658 bytes.
*****************************************************************************************COMPLETE!
Another file? true
Downloading a file of 63952457 bytes.
****************************************************************COMPLETE!
Another file? true
Downloading a file of 70083793 bytes.
***********************************************************************COMPLETE!
Another file? true
Downloading a file of 22587856 bytes.
***********************COMPLETE!
Another file? true
Downloading a file of 92608938 bytes.
*********************************************************************************************COMPLETE!
Another file? true
Downloading a file of 55043669 bytes.
********************************************************COMPLETE!
Another file? true
Downloading a file of 42656083 bytes.
*******************************************COMPLETE!
Another file? true
Downloading a file of 95922137 bytes.
************************************************************************************************COMPLETE!
Another file? true
Downloading a file of 27843717 bytes.
****************************COMPLETE!
Another file? true
Downloading a file of 66949868 bytes.
*******************************************************************COMPLETE!
Another file? false
You downloaded 15 files, total.
Questions: - How did the process of creating these programs go for you?
- What were your main challenges and how did you overcome them?
- What did you learn that may be of use as you move along in this class?