Newbie, treating two inputs in a method and returning them?
Hey guys,
New to programming, messing around with it, wanted to write a download calculator program
my problem is dealing with returning the user input for download speed and filesize - can it be done in a single method? (two return statements not working)
if not, what's a simple and clever way to take two inputs, treat them and return them?
Code :
import java.text.DecimalFormat;
import java.util.Scanner;
/*
* Program designed to allow a user to calculate how much time is left on his download
* takes input of downloadspeed and filesize, then calculates on that input and prints it
*/
public class DownloadCalc {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
double downloadspeed = 0;
double filesize = 0;
double time = 0;
DecimalFormat chronos = new DecimalFormat("00");
//method for taking in data
inputData(downloadspeed, filesize, myScanner);
//method for printing the result of the data treatment
printMe(theMath(downloadspeed, filesize, time), chronos);
} //end main
public static double theMath(double s, double f, double t){
//time in seconds = filesize in GB * 1024 / downloadspeed
t = (f * 1024)/s;
return t;
} //end theMath
public static void printMe(double t, DecimalFormat c){
double hours;
double remainder;
double seconds = t;
double minutes;
//conversion from only seconds into hours, minutes and seconds
hours = seconds / 3600;
remainder = seconds % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
System.out.println("Total time for download: " + c.format(hours) + ":" + c.format(minutes) + ":" + c.format(seconds));
} //end printMe
public static void inputData(double s, double f, Scanner mS){
int choice;
//switch for user to choose MB/GB, then input value
System.out.println("Download speed: press 1 for MB/s, 2 for GB/s ");
choice = mS.nextInt();
switch (choice){
case 1:
System.out.println("You have chosen MB/s. Enter your download speed: ");
s = mS.nextDouble();
break;
case 2:
System.out.println("You have chosen GB/s. Enter your download speed: ");
s = mS.nextDouble()*1024;
break;
} //end switch
System.out.println("File size: press 1 for MB, 2 for GB ");
choice = mS.nextInt();
switch (choice){
case 1:
System.out.println("You have chosen MB. Enter the file size: ");
f = mS.nextDouble()/1024;
break;
case 2:
System.out.println("You have chosen GB. Enter the file size: ");
f = mS.nextDouble();
break;
} //end switch
//MISSING RETURNS!
} //end inputData
} //endclass
kind regards,
vikingcoder
Re: Newbie, treating two inputs in a method and returning them?
Well I don't know how other people would do it, but personally, I'd put those two values in a separate object, and then, return the object.
Re: Newbie, treating two inputs in a method and returning them?
The answer is there:
Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike,
Environment env) {
Bicycle fastest;
// code to calculate which bike is
// faster, given each bike's gear
// and cadence and given the
// environment (terrain and wind)
return fastest;
}
Re: Newbie, treating two inputs in a method and returning them?
Ok, thank you, I'll look into it