【发布时间】:2016-06-18 12:16:46
【问题描述】:
我最近写了一个小模拟,找不到方差,我的课程如下:
class aQuarter {
public int ordered;
public int leftOver;
public int profit;
public int profit2;
public double variance;
public int demand;
public int demandTotal;
public int counter;
public int sold;
public int averageProfit;
public double standardDev;
public int soldTotal;
public int orderedTotal;
public int getCounter() {
return counter;
}
public void incrementCounter() { //increments the counter for every week
counter++;
}
public int getDemand() {
return demand;
}
public void setDemand(int demand) { //sets the demand and adds that demand to the total demand for stat keeping
this.demand = demand;
demandTotal+=demand;
}
public int getDemandAverage() {
return demandTotal/counter; //returns the average demand
}
public int getProfitAverage() {
return profit/counter; //returns the average profit
}
public int getOrderedAverage() { //returns how many ordered on average for that quarter
return orderedTotal/counter;
}
public void setOrdered(int ordered) { //sets how much was ordered
this.ordered = ordered;
orderedTotal+=ordered;
}
public int getleftOver() { //testing purposes
return leftOver;
}
public void setleftOver() { //sets how many gallons we have left over to deduct from the profit made
int x=ordered-demand;
if (x<0) { //if the demand was more than we ordered, none leftover
leftOver=0;
}
else
leftOver=x; //if we ordered more than was demanded, leftover = ordered-demand
}
public int getSoldAverage() {
return soldTotal/counter; //returns how many was sold average
}
public void setSold() {
int x = ordered-demand;
if (x<=0) { //sets how many we sold
sold=ordered; //if the demand was more than we ordered, we sold how much was demanded
soldTotal+=sold; //add that total to the soldTotal accumulator
}
else {
sold=demand; //else, we ordered more than was demanded, making us sell how much was demanded
soldTotal+=sold; //add that total to the soldTotal accumulator
}
}
public int getProfit() {
return profit;
}
public void setProfit() { //sets the profit made for the week
int baseProfit=sold*10 - leftOver*7;
profit+= sold*10 - leftOver*7; //For every gallon we sold, we made 10 dollars,but for every gallon that
profit2+=(baseProfit*baseProfit); //was leftover, we lost 7 dollars
} //add the profit squared to profit2
public double getVariance() { //gets the variance of the simulation for the quarter
averageProfit=profit/counter;
variance=profit2/counter -averageProfit*averageProfit;
return variance;
}
public double getStandardDev() { //gets the standard deviation for the quarter
standardDev=Math.sqrt(variance);
return standardDev;
}
}
以及在“for”时间循环中对类的调用。
firstQuarter.incrementCounter(); //add 1 to the counter
firstQuarter.setOrdered(50+gallonsDelivered()); //set how many gallons ordered for quarter+ randomness
firstQuarter.setDemand(gallonsleftOver1()); //sets demand using the demands for the first quarter
firstQuarter.setSold(); //sets sold
firstQuarter.setleftOver(); //sets how many leftover to deduct from the profit made
firstQuarter.setProfit(); //sets the profit
一切都按预期打印出来,除了这个世界数字给我的方差。
For Quarter 1:
Average Profit Per Week: 444
Average Demand Per Week: 54
Average Ordered Per Week: 51
Average Sold Per Week:47
Average Variance of Profit: 8915.0
Standard Deviation: 94.41927769264072
我按照程序在过去的作业中完成了差异,所以我不确定我在哪里出错了,希望另一双眼睛可以得出一些答案。感谢您的宝贵时间!
【问题讨论】:
-
为什么你认为方差是错误的?它有什么问题?
标签: java class object simulation variance