Results 1 to 17 of 17
  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default How do I make function to return its value to multiple variable?

    Java Code:
    public class TestTax 
    {
    	public static void main (String[] args)
    	{
    	
    		double salary, rate;
    		salary = 50000.0; //salary
    		rate = 0.25; //fedRate
    		
    		calcTax (salary, rate);
    		
    		salary = 50000.0; //salary
    		rate = 0.1; //stateRate
    		
    		calcTax (salary, rate);
    	} // end main
    		public static void calcTax(double salary, double rate) //header
      		{
    			double tax = (salary*rate); //calculate tax
    			System.out.println("Tax is "+ tax); //print out the result
    	 	}// end calcTax
    } // end application
    ok. this is first code I wrote and as you can see here calcTax will print both tax rate to console. here's my question.

    Java Code:
    public class TestTax2 
    {
    	public static void main (String[] args)
    	{
    	
    		double salary, rate;
    		
    		salary = 50000.0;
    		rate = 0.25; //fedRate
    		
    		double tax = calcTax (salary, rate);
    		
    		
    		salary = 50000.0;
    		rate = 0.1; //stateRate
    		
    		tax = calcTax (salary, rate);
    		
    		System.out.println("Tax is "+ tax);
    		
    	} // end main
    		public static double calcTax(double salary, double rate) //header
      		{
    			return (salary*rate);
    			
    	 	}// end calcTax
    	
    } // end application
    when I use return value as above, it only returns stateRate value. How do I fix this so calcTax returns to both value? I've been on this for an hour, can't figure it out. :(
    here's instruction for second code suppose to be.

    PART 1: MODIFY the method called calcTax that takes 2 parameters: a salary and a rate. It multplies them together and returns the product

    PART 2:
    Write an application called TestTax2

    It will declare the following variables
    salary (precise decimal) and assign it the value 50000.0
    fedRate (precise decimal) and assign it the value 0.25
    stateRate (precise decimal) and assign it the value 0.1

    It will now call the calcTax method 2 times:
    1. the first time, pass in the salary and the fedRate
    2. the second time, pass in the salary and the stateRate
    After each call to calcTaxes. The main will output to the console the amount of the tax returned by the calcTax method with an appropriate message
    Last edited by shin777; 07-04-2013 at 12:27 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: How do I make function to return its value to multiple variable?

    Java Code:
     public static void main (String[] args)
        {
         
            double salary, rate;
             
            salary = 50000.0;
            rate = 0.25; //fedRate
             
            double tax = calcTax (salary, rate);
            System.out.println("Fed rate Tax is "+ tax);
             
            salary = 50000.0;
            rate = 0.1; //stateRate
             
            tax = calcTax (salary, rate);
             
            System.out.println("State rate Tax is "+ tax);
             
        }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: How do I make function to return its value to multiple variable?

    Calling the same method twice should be sufficient; you might store the return value in two separate variables to be able to use them individually.

    But you are probably here to learn so lets go the extra mile: I'm having difficulty understanding what you are asking for exactly. Are you perhaps asking how to make the calcTax() calculate and return both rates at once?

  4. #4
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    PART 1: MODIFY the method called calcTax that takes 2 parameters: a salary and a rate. It multplies them together and returns the product

    PART 2:
    Write an application called TestTax2

    It will declare the following variables
    salary (precise decimal) and assign it the value 50000.0
    fedRate (precise decimal) and assign it the value 0.25
    stateRate (precise decimal) and assign it the value 0.1

    It will now call the calcTax method 2 times:
    1. the first time, pass in the salary and the fedRate
    2. the second time, pass in the salary and the stateRate
    After each call to calcTaxes. The main will output to the console the amount of the tax returned by the calcTax method with an appropriate message


    umm.. this was the instruction.

  5. #5
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by shin777 View Post
    when I use return value as above, it only returns stateRate value
    when a function is invoked it can only return a single value... for you to have those values you'll have to invoke it twice and at least store the returned values in variables

    e.g. just to clarify what a method does and what a variable does
    Java Code:
        int variable; // sure this varable holds an int
        int method() // sure thing it returns an int
    
        variable = method(); // let variable hold whatever value that is returned by method()
    Just follow PhHeins example

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by shin777 View Post
    PART 1: MODIFY the method called calcTax that takes 2 parameters: a salary and a rate. It multplies them together and returns the product

    PART 2:
    Write an application called TestTax2

    It will declare the following variables
    salary (precise decimal) and assign it the value 50000.0
    fedRate (precise decimal) and assign it the value 0.25
    stateRate (precise decimal) and assign it the value 0.1

    It will now call the calcTax method 2 times:
    1. the first time, pass in the salary and the fedRate
    2. the second time, pass in the salary and the stateRate
    After each call to calcTaxes. The main will output to the console the amount of the tax returned by the calcTax method with an appropriate message


    umm.. this was the instruction.
    Ur 1st mistake is at part 1 calcTax() has to return something,should be declared public static double calcTax(double s, double r)
    Second mistake on part 2... since calcTax() in part 1 is a static method you don't really have create a new one... you just call it directly e.g.
    Java Code:
    // remember this is done in TestTax2
    public static void main(String[] args) {
           System.out.println("fedRate is " + TestTax1.calcTax(salary, fedRate));
           System.out.println("StateRate is "+ TestTax1.calcTax(salary, stateRate));
    }
    This is what is expected of you
    Last edited by Kagiso; 07-04-2013 at 12:39 PM.

  7. #7
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    is it suppose to look like this, then?

    Java Code:
    public class TestTax2 
    {
    	public static void main (String[] args)
    	{
    	
    		double salary, fedRate, stateRate;
    		
    		salary = 50000.0;
    		fedRate = 0.25;
    		
    		double tax = calcTax (salary, fedRate);
    		
    		
    		salary = 50000.0;
    		stateRate = 0.1;
    		
    		tax = calcTax (salary, stateRate);
    		
    		System.out.println("fedRate is " + calcTax(salary, fedRate));
          System.out.println("StateRate is "+ calcTax(salary, stateRate));
    		
    	} // end main
    		public static double calcTax(double salary, double rate) //header
      		{
    			return (salary*rate);
    			
    	 	}// end calcTax
    	
    } // end application

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by shin777 View Post
    is it suppose to look like this, then?
    I guess it's supposed to work like that, but it could certainly look a lot better:
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    yep. it could look a lot better but my professor always take points out if I put anything that he hasn't shown in class. I've seen bunch of guys getting either 0 or 50% for using stuff we haven't learned in class. kinda funny. :(

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How do I make function to return its value to multiple variable?

    I was referring to the code formatting. It's atrocious.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by shin777 View Post
    my professor always take points out if I put anything that he hasn't shown in class. I've seen bunch of guys getting either 0 or 50% for using stuff we haven't learned in class.
    What an awful teacher...
    Get in the habit of using standard Java naming conventions!

  12. #12
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    that applies to formatting too. it has to be same order as that. only thing i didn't put there here are comments on every single line. i would problem get minus 50%+ if I don't put comment explain what each line do too. I learned it hard way.
    he just doesn't want us to do anything that's not done his way or pay the consequences. there's just no reason behind it. asking question itself is big minus too. i am just debating whether i should drop the class or not but I already paid tuition. I've got
    no choice but follow his way. :(

  13. #13
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by shin777 View Post
    is it suppose to look like this, then?
    No... this is not what I meant...
    have a look at the following

    Java Code:
    class Alpha {
        public static double calculate(double s, double r) {
            return s*r;
        }
    the second class will access class Alpha. And class Beta will look something like this
    Java Code:
    class Beta {
         public static void main(String []arg) {
              double salary = 5000;
              double rate1 = 0.2;
              double rate2 = 0.5;
              System.out.println("First Rate is " + Alpha.calculate(salary, rate1)); // remember calculate() is static that's why I call it directly from its class
              System.out.println("Second Rate is " + Alpha.calculate(salary, rate2));  
        }
    }
    nice and easy
    Last edited by Kagiso; 07-04-2013 at 03:37 PM.

  14. #14
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    ^ yep, kagiso. that's how my c++ teacher would taught me but I am trying to mimic exact same order and way my java teacher is writing. he just want us to seperate method not class. I don't wanna get marked wrong for it. Anyway, I fixed it so it looks a little cleaner. just seperated method, not class.

    Java Code:
    public class TestTax2 
    {
    	public static void main (String[] args)
    	{
    	
    		double salary, fedRate, stateRate;
    		
    		salary = 50000.0;
    		fedRate = 0.25;
    		stateRate = 0.1;
    		
    		System.out.println("fedRate is " + calcTax(salary, fedRate));
            System.out.println("StateRate is "+ calcTax(salary, stateRate));
    		
    	} // end main
    	public static double calcTax(double salary, double rate) //header
      	{
    			return (salary*rate);
    			
    	}// end calcTax
    	
    } // end application

  15. #15
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by shin777 View Post
    ^ yep, kagiso. that's how my c++ teacher would taught me but I am trying to mimic exact same order and way my java teacher is writing.
    Oh well! I guess you know what you are doing then....

    he just want us to seperate method not class
    .
    Say What?

    I don't wanna get marked wrong for it
    Trust me nobody wants to be wrong....

    Anyway, I fixed it so it looks a little cleaner
    A little thank you wouldn't hurt you my friend

  16. #16
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: How do I make function to return its value to multiple variable?

    I thank you all for your help. I appreciate it greatly of course. I am a little shy on expressing myself. :)

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: How do I make function to return its value to multiple variable?

    Quote Originally Posted by Kagiso View Post
    A little thank you wouldn't hurt you my friend
    I'm passing that one back at you: you can also assume the thanks is implied so this can remain a forum and not a support group :/

Similar Threads

  1. How to return a vector from a function
    By 8Observer8 in forum New To Java
    Replies: 6
    Last Post: 05-17-2013, 03:38 PM
  2. function return 1 > 0 ??
    By garnachito in forum Android
    Replies: 4
    Last Post: 02-19-2013, 11:26 AM
  3. Replies: 2
    Last Post: 01-09-2013, 09:10 PM
  4. Return ArrayList function - Java
    By skp123 in forum New To Java
    Replies: 23
    Last Post: 05-01-2011, 05:11 AM
  5. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •