Reputation: 111
Prompt: http://puu.sh/lvdIG/404ebfba03.png
I am so close to being done. I need to fix three errors I have: on the treeReport+= line
, I get error: "cannot find symbol" for minSeeds
, totalSeeds
, and totalTrees
. I cannot declare them as a member of the class, and I can't use static before them. I also can only use one class.
import javax.swing.JOptionPane;
public class TreeCalc {
public static void main(String[] args) {
String[] treeTypes = new String[] {"Fir", "Pine", "Spruce"};
int[] desiredYield = new int[treeTypes.length];
double[] decayRate = new double[] {0.07, 0.12, 0.08};
desiredYield = getYield(decayRate, desiredYield, treeTypes);
getCalculate(decayRate, desiredYield, treeTypes);
printMessage(decayRate, desiredYield, treeTypes);
}
//Asks user to input # of trees for each tree type
public static int[] getYield(double[] decayRate, int[] desiredYield, String[] treeTypes) {
int index = 0;
for (int i = 0; i < treeTypes.length; i++) {
try {
desiredYield[index] = Integer.parseInt(JOptionPane.showInputDialog("Please enter your desired yield for: " + treeTypes[i]));
} catch (NumberFormatException e) {
desiredYield[index] = 0;
JOptionPane.showMessageDialog(null, "Error: Please enter your desired yield for " + treeTypes[i]);
}
if (desiredYield[index] <= 0) {
JOptionPane.showMessageDialog(null, "Error: Please enter your desired yield for " + treeTypes[i]);
} else {
index++;
}
}
return desiredYield;
}
//Calculates totals and minimums
public static int[] getCalculate(double[] decayRate, int[] desiredYield, String[] treeTypes) {
int totalSeeds = 0;
int totalTrees = 0;
int minSeeds = 0;
int index = 0;
for (int i = 0; i < treeTypes.length; i++) {
minSeeds += (desiredYield[index] * (decayRate[index] * 7)) + desiredYield[index];
totalSeeds += minSeeds;
totalTrees += desiredYield[index];
}
return desiredYield;
}
public static void printMessage(double[] decayRate, int[] desiredYield, String[] treeTypes) {
getCalculate(decayRate, desiredYield, treeTypes);
String treeReport = "Tree Type | Minimum Seeds | Total Seeds | Total Trees ";
for (int i = 0; i < treeTypes.length; i++) {
treeReport += "\n" + treeTypes[i] + " " + minSeeds + " " + totalSeeds + " " + totalTrees;
}
JOptionPane.showMessageDialog(null, treeReport);
}
}
Upvotes: 2
Views: 87
Reputation: 1
You declare totalSeeds, totalTrees and minSeeds in getCalculate so of course it can not find these variables. Does getCalculate change at all desiredYield? If not instead of returning this, you should create a list where you put totalSeeds, totalTrees and minSeeds and return this so you can use it later.
Upvotes: 0
Reputation: 14658
Instead of returning desiredYield
int array from getCalculate()
, which you are not at all using in your printMessage()
, you can prepare and return a more meaningful int array i.e. the results having your minSeeds
, totalSeeds
and totalTrees
. And then use it in your printMessage()
.
With this you need not to declare those variables as class level or static. And this is what is your requirement.
I have commented the updated code with //Updated code...
in my answer code snippet for your easy reference.
With int array you need to have index based approach to access the array, in case you want to have more readable approach then an alternative could be preparing and returning a HashMap
.
public static int[] getCalculate(double[]decayRate, int[]desiredYield, String[]treeTypes){
int totalSeeds =0;
int totalTrees=0;
int minSeeds=0;
int index=0;
int[] finalResult = new int[3]; //Updated code...
for(int i=0; i<treeTypes.length;i++){
minSeeds+=(desiredYield[index] * (decayRate[index]*7))+desiredYield[index];
totalSeeds+=minSeeds;
totalTrees+=desiredYield[index];
}
finalResult[0] = minSeeds; //Updated code...
finalResult[1] = totalSeeds; //Updated code...
finalResult[2] = totalTrees; //Updated code...
return finalResult; //Updated code...
}
public static void printMessage(double[]decayRate, int[]desiredYield, String[]treeTypes){
finalResult = getCalculate(decayRate, desiredYield, treeTypes); //Updated code...
String treeReport = "Tree Type | Minimum Seeds | Total Seeds | Total Trees ";
for(int i=0; i<treeTypes.length; i++){
treeReport += "\n"+treeTypes[i] + " " + finalResult[0] + " " + finalResult[1] + " " + finalResult[2]; //Updated code...
}
JOptionPane.showMessageDialog(null, treeReport);
}
Upvotes: 2