0

I need to return multiple variables, but I got confused how to do that. I only know to return 1 variable.

Tyear, aveannualcost, aveinv, aveorderperyear, avelostdemandperyear, avelostdemandperyear

public class inventory {

    DistDiskrit acak = new DistDiskrit();

    float inisialisasi (float Tfinal, float s, float Q, float dist_x) {
        float TT = 1, demand1;
        float cumlost = 0, Norder = 0, TOrderArrive = 0 ;
        boolean Orderplaced = false;
        float inv = 20, cuminv = inv, Cinv = (float) 0.4, Corder = 5, Cpenalty = 1;
        while(TT < Tfinal) {
            if(TT == TOrderArrive){     
                inv = inv + Q;
                Orderplaced = false;
            }
            cuminv = cuminv + inv;
            demand1 = demand(TT);
            if(demand1 > inv) {
                cumlost = (float) (cumlost + demand1 - inv);
                inv = 0;
            }                
            else {inv = inv - demand1;}
            if((inv <= s) & (!Orderplaced))  
                Orderplaced = true;
            TOrderArrive = TT + 4 + (dist_x * 3);
            Norder = Norder + 1;
            TT = TT + 1;
        }
        float Tyear = Tfinal/365;
        float aveannualcost=Cinv*(cuminv/TT)+(Corder/Tyear)*Norder+Cpenalty*(cumlost/Tyear);
        float aveinv = cuminv/TT;
        float aveorderperyear = Norder/Tyear;
        float avelostdemandperyear = cumlost/Tyear;
        return avelostdemandperyear;
    }
    float mean (int n) {
        float x[] = new float[n];
        float jumlah=0;
        for(int i=0;i<n;i++){
            jumlah = jumlah+x[i];
        }
        return jumlah/n;
    }
}

[updated-part]

so then, my friend told me to add something like this

   public float [] inisialisasi (float Tfinal, float s, float Q, float dist_x){
.
.
.    
    float rtrn []= {Tyear,aveannualcost, aveorderperyear, avelostdemandperyear};
            return rtrn;
}

and i got this -- [F@d93b30

3
  • create a class that contains the data you want and return an instance of that class Commented Sep 21, 2017 at 7:25
  • You can't. Wrap them in a Object Commented Sep 21, 2017 at 7:26
  • 1
    or you can just put them in a map and return them :) Commented Sep 21, 2017 at 8:09

1 Answer 1

-1

Java methods can only return a single thing. However that thing can be a scalar or an object. So you could create a class to hold your calculated variables and return that.

For example:

class Example {
    float Tyear;
    float aveannualcost;
    float aveinv;
    float aveorderperyear;
    float avelostdemandperyear;
}

You'll need to flesh this out with a constructor and/or accessor methods.

Sign up to request clarification or add additional context in comments.

4 Comments

this should be a comment
@ScaryWombat Why should it be a comment? What's the protocol there?
OK, no longer just a comment now ;-)
if all the datatypes are the same it's more efficient to just return an array than a wrapper object. In some circumstances, it's also viable to pass differently typed data as a String instead of using wrapper object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.