Results 1 to 8 of 8
  1. #1
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Cant we have two values returned from a method in a class

    I am trying to return two double values from the class. The code is ;

    public double[] calculations (double rain[], double evaptrans[], double cistern_water[]) //Does not need to receive vectors as arguments. Single daily values are most appropriate ~CJM
    { sws= RZ*AWHC; // I recommend renaming this variable as AWHC = Available water holding capacity ( =FC-PWP). Rename rooting depth as RZ. Specify the units of your calculations in comments.~CJM
    /* All units are in inches and gallons ~ JPP
    */
    mad= sws*allowablecoefficient; //What is the allowable coefficient? I do not know what this is. ~CJM // An availability coefficient is used
    /* to calculate the percentage of water that is readily available to the plant, ie the amt of stored water
    * a plant can extract from the soil without getting stressed (ref: British Columbia, Ministry of agriculture ~ JPP
    */
    initialsoilwaterbalance= 0;

    AD = mad/0.75;
    System.out.println ("mad= " +mad); //MAD should be an input not a calculated value ~CJM // MAD is calculated above as mad= sws *allowablecoefficient ~JPP

    double[] etc = new double [evaptrans.length]; //No need for vectors. This method should just handle "today's" numbers. This makes it more complicated than is needed. ~CJM // ?? ~JPP
    double[] netsoilwaterbalance = new double [rain.length];
    double[] netsoilwb_area = new double [rain.length]; //Is the net area different than another area? ~CJM // netsoilwaterbalance gives the value per unit area while netsoilwb_area calculates for given area ~ JPP
    double[] waterreqdforirrigation = new double [rain.length];
    double[] waterreqdforirrigation_area = new double [rain.length];
    double farm_area = 108000; // sq.inches ~ JPP
    double[] updated_cistern_water = new double [rain.length];
    /** for turf grass of area 750 sq.ft */

    for (int j=0; j<evaptrans.length; j++)
    { etc[j] = evaptrans[j] * kc;
    }


    for (int i=0;i<rain.length;i++)
    {
    a=i+1;

    netsoilwaterbalance[i] = initialsoilwaterbalance + rain[i] - etc[i];
    netsoilwb_area[i] = netsoilwaterbalance[i]*farm_area;
    System.out.println ("the amount of water in the soil on day " + a + " is " + netsoilwb_area[i]);
    initialsoilwaterbalance = netsoilwaterbalance[i];

    if (netsoilwaterbalance[i] <= mad)
    {


    System.out.println ("Irrigation is required on day " + a); //Not necessarily. It depends on the thresholds for irrigation, even if based on storage. ~CJM // ?? ~JPP
    waterreqdforirrigation[i] = AD - netsoilwaterbalance[i];
    waterreqdforirrigation_area[i] = waterreqdforirrigation[i] * farm_area;
    if (cistern_water[i] <= waterreqdforirrigation[i] )
    { System.out.println ("water equal to " + waterreqdforirrigation_area[i] + " to be supplied from secondary source on day " +a);
    }
    else if (cistern_water[i] >= waterreqdforirrigation[i] )
    { System.out.println ("water to be released from cistern on day " + a +" is " +waterreqdforirrigation_area[i]);
    cistern_water[i+1] = cistern_water[i+1] - cistern_water[i];
    updated_cistern_water [i] = cistern_water[i+1];
    }

    }
    else
    { System.out.println ("Irrigation is not required on day " +a);
    waterreqdforirrigation[i] = 0.0;
    }
    }
    return waterreqdforirrigation_area;
    return updated_cistern_water;

    }


    It shows error :-
    Unreachable statement for "updated_cistern_water"

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    that code looks horable. please use code tags.

    to return two values, you can use a wrapper class or an array.
    Java Code:
    public class MyWrapper{
      public double x;
      public double y;
      ...
    }
    ...
    return new MyWrapper(1.0, 4.5);
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    I don't want to return two values, but want to return two variables -- one is waterreqdforirrigation_area and the other one is updated_cistern_water.

    And, also how to put the entire code in code section ?
    Do i have to use //code ?

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    use code tags like this:
    [code ] <-- added space
    // code here
    [/code ] <-- added space
    * without the space
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What do you mean by "returning two variables"?

    Anyway in Java you can only return a single value. The value you return can be a primitive value like 42 or false. Or it can be a reference to an object, like a wrapper object as described above or an array. If your method returns anything at all your choice is: return a primitive value, return a reference value. And that's it.

    An array can act as the sort of wrapper object described earlier. If the caller wants two double values calculated by the method, then the method can create an array, place the two doubles of interest into the array, and then return an reference to that array.

    I mention this because your code declares calculations() as returning a double array. I can't read the code, so I can't really see if it does this or not at the moment.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jaiminparikh View Post
    I don't want to return two values, but want to return two variables -- one is waterreqdforirrigation_area and the other one is updated_cistern_water.
    Can you explain what you mean by return variable? Only thing you can do is returning/passing some values from one method to another. You cannot pass variables. In other words, you cannot return waterreqdforirrigation_area, and you can return the value hold by that variable.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    I would also recommend you use the standard naming convention for variables, for example:
    Java Code:
    [FONT="Arial"]allowable[B]C[/B]oefficient
    initial[B]S[/B]oil[B]W[/B]ater[B]B[/B]alance
    water[B]R[/B]eqd[B]F[/B]or[B]I[/B]rrigation[/FONT]
    Also in Java, variables with all capital letters means that it's a constant (it's value doesn't change). Don't know if this is the same in your program.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  2. Returned type changes if class is extended. How?
    By Gelembjuk in forum New To Java
    Replies: 10
    Last Post: 10-31-2008, 11:18 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 PM

Posting Permissions

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