2

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);
    }
}
2
  • What is your issue, please mention it clearly. Commented Nov 23, 2015 at 0:33
  • You would have less problems if you would move the printMessage call into the getCalculate method, but I guess this is too obvious. Commented Nov 23, 2015 at 0:36

2 Answers 2

2

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Your answer maybe correct but can use improvement. please read How to improve your answer meta.stackoverflow.com/q/253804/3664960

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.