4

Using arrays in java with methods, return groups

 3 years ago
source link: https://www.codesd.com/item/using-arrays-in-java-with-methods-return-groups.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Using arrays in java with methods, return groups

advertisements

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);
    }
}


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);
  }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK