我正在尝试运行一个类,该类根据从随机变量生成器类获得的数据执行计算。我想要的是在任何给定时间生成3个随机变量,然后在不生成另一组随机变量的情况下执行该类中的所有计算。一旦完成所有计算,就可以生成一组新的随机变量。 Computation类如下所示:
public class Computation {
public int meltTemp;
//public void mTemp;
public int mouldTemp;
public int setTemp;
public int Q;
public int volume;
public double Cp;
public int actualMouldTemp;
public double dT;
public int AMT ;//= Math.abs(dT);
public double mTemp;
public Hashtable catalogue1;
protected void setup() {
//create Hashtable
catalogue1 = new Hashtable();
}
public double getAnswer() {
int rand1[] = new int[3];
int input [] = new int [2];
ControlGUI par = new ControlGUI ();
//input = ControlGUI.getArray();
//catalogue am = new catalogue();
RandomVariableGenerator var = new RandomVariableGenerator();
rand1 = RandomVariableGenerator.getRand();
//int rand[] = Arrays.copyOf(rand1, rand1.length);
meltTemp = 245;
System.out.println("Melt Temp is : "+ meltTemp);
actualMouldTemp = (int) ((0 - 51.4)+(0.302 * meltTemp) +(1.64 * rand1[0])+(0.201 * rand1[1]));
System.out.println("The actual mould temperature is :" +actualMouldTemp + " Degrees celcius" );
return actualMouldTemp;
}
public int getDiff(){
Computation amg = new Computation();
double result[] = new double [4]; {
setTemp = 55;
dT = (actualMouldTemp - setTemp);
AMT = (int) Math.abs(dT);
System.out.println("The temperature difference is : "+ AMT);
return AMT;
}
}
尝试使用生成的变量的下一个方法是getHeatingTime1(),它需要坦克卷的rand1 [2]:
public double getHeatingTime1(){
Computation jose = new Computation();
int [] Results = new int [4];
Results [0] = AMT;
//Results [] = Computation.class;
Q = 3; //heating in kW
Cp = 4.2; //Specific heat capacity of water
volume = 6;
//AMT =
System.out.println("AMT IS "+ Results [0]);
long HT1 = (long) ((volume*Cp*Results [0])/Q);
return HT1;
}
public double getHeatingTime2(){
int Results [] = new int [4] ;
Computation cr7 = new Computation();
//double dT = cr7.getDiff();
Q = 9; //heating in kW
Cp = 4.2; //Specific heat capacity of water
volume = 6;
//AMT = 7;
System.out.println("AMT IS "+ Results [1]);
long HT2 = (long) ((volume*Cp*Results [1])/Q);
return HT2;
}
public double getHeatingTime3(){
int Results [] = new int [4];
//double AMT = getDiff();
Computation jt = new Computation();
//double dT = jt.getDiff();
// AMT = 7;
Q = 18; //heating in kW
Cp = 4.2; //Specific heat capacity of water
volume = 6;
System.out.println("AMT IS "+ Results [1]);
long HT3 = (long) ((volume*Cp*Results [1])/Q);
return HT3;
}
public double getCoolingTime(){
// double CT = 0;
Computation nvh = new Computation();
int rand1[] = new int[3];
int rand[] = Arrays.copyOf(rand1, rand1.length);
//RandomVariableGenerator var = new RandomVariableGenerator();
//rand1 = Computation;
mouldTemp = 55;
System.out.println("Rand 0 is "+ rand1[0]);
System.out.println("Rand 1 is "+ rand1[1]);
System.out.println("Rand 2 is "+ rand1[2]);
double CT = ((0.5/rand[2])*((mouldTemp - rand1[0])/(rand1[1] - mouldTemp)));
double CTA = Math.abs(CT);
return CTA;
}
}
随机变量生成器类如下所示:
public class RandomVariableGenerator {
public static int[] getRand (){
int rand[] = new int [3];
Random r = new Random ();
int myRandomNumber = 0;
//for (int i=0; i < 1 ; i++)
myRandomNumber = r.nextInt(15) + 5;
System.out.println("Chilled water temperature:" + myRandomNumber);
rand[0] = myRandomNumber;
Random rn = new Random ();
int myNumber = 0;
// (for int i=0; i < 1; i++)
myNumber = rn.nextInt(25) + 50;
System.out.println("Heated water temperature:" + myNumber);
rand[1]= myNumber;
Random rm = new Random ();
int myRandNumber = 0;
// (for int i=0; i < 1; i++)
myRandNumber = rm.nextInt(2) + 6;
System.out.println("Tank Volume:" + myRandNumber);
rand[2]= myRandNumber;
return rand;
}
}
答案 0 :(得分:0)
你可以将所有3个随机变量设为私有和类,而不是函数。然后,当你需要一个新的集合时,调用一个重置它们的函数。
例如:
public class MyClass{
//replace these with the actual types/variables you need
int var1;
double var2;
double var3;
/**
*Call this function when you need a new set of variables
**/
public void resetVariables(){
//Change this to the random class you would prefer to use
Random random = new Random(System.currentTimeMillis());
var1 = random.nextInt();
var2 = random.nextDouble();
var3 = random.nextDouble();
}
//put your other functions here except remove the declarations for var1, var2, and var3
/*
ex.
public int myFunction(){
Random random = new Random(System.currentTimeMillis());
int var1 = random.nextInt();
return var1 * 2;
}
becomes
public int myFunction(){
// use the global variable instead of the function's variable
return var1 * 2;
}
*/
}
答案 1 :(得分:0)
声明一个实例int vector来保存对象(实例)级别的三个值(rndVars [3])
public class Computation {
public int meltTemp;
//public void mTemp;
public int mouldTemp;
public int setTemp;
public int Q;
public int volume;
public double Cp;
public int actualMouldTemp;
public double dT;
public int AMT ;//= Math.abs(dT);
public double mTemp;
private int [] rndVars = new int[3];
...
如果需要重置3个值,可以调用公共方法setRandomVars()。
public void setRandomVars(){
rndVars[0] = RandomVariableGenerator.getRand();
rndVars[1] = RandomVariableGenerator.getRand();
rndVars[2] = RandomVariableGenerator.getRand();
}