Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Newbie, treating two inputs in a method and returning them?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

    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


  2. #2

    Default 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.

  3. #3

    Default 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;
    }

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Newbie, treating two inputs in a method and returning them?

    Ok, thank you, I'll look into it

Similar Threads

  1. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  2. Returning multiple values from a method.
    By atar in forum Java Theory & Questions
    Replies: 14
    Last Post: July 31st, 2012, 04:59 PM
  3. [SOLVED] Help with method returning a double
    By Mike_Chase in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2011, 01:09 AM
  4. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM