3 Replies Latest reply on Dec 19, 2007 3:56 AM by 807601

    Constructor and Array problems

    807601
      Ok, I have been trying to fix this program so that it calculates the sum of the given doubles.

      I use the setGrades method and get the array variables assigned, but when I go to try to calculate the sum, it uses my default constructor's version of the array.

      How do I get it so that the default constructor will not overwrite the new variables in the array that I have assigned?

      Here is all the code:
      import java.lang.System;
      import java.lang.Math;
      import java.util.Scanner;
      
      public class Grades
      {
           private double[] grades;
           private double MAX_VALUE;
           
           public Grades()
           {
                grades =  new double[10];
                for (int count = 0; count < 10; count++)
                {
                     grades[count] = count;
                }          
           }
           
           public Grades(int size, String gradeStr)
           {
                Scanner chopper = new Scanner(gradeStr);
                double[] grades = new double[size];     
                int a = 0;
                
                while (chopper.hasNextDouble()) 
                {
                grades[a] = chopper.nextDouble();
                System.out.println(grades[a]);
                a++;
                 }                         
           }
           
           public void setGrades(int size, String gradeStr)
           {
                Scanner chopper = new Scanner(gradeStr);
                double[] grades = new double[size];     
                     
                for (int count = 0; count<size; count++)
                {
                     grades[count] = chopper.nextDouble();
                     System.out.println("Grade " + count + " ::   "  + grades[count]);
                }
                System.out.println("");
                
                MAX_VALUE = grades[0];
                
                for (int count2 = 0; count2<size; count2++)
                {
                     if (grades[count2] > MAX_VALUE)
                     {
                          MAX_VALUE = grades[count2];
                     }
                }
                
           }
           
           public double getSum()
           {
                double sum = 0.0;
                
                for (int count = 0; count<grades.length; count++)
                {
                     if (grades[count] <= MAX_VALUE)
                     {
                     sum += grades[count];
                     System.out.println(grades[count]);
                     System.out.println(sum);     
                     }
                     
                } 
      
                return sum;
           }
           
           public double getAverage()
           {
                double average = 0.0;
                
                
                
                return average;
           }
           
           public String toString()
           {
                
                return "";
           }
      }
      and
      public class Lab10d
      {
           public static void main(String [] args)
           {
                Grades test = new Grades();
                test.setGrades(5, "100.00 90.00 85.00 72.50  95.60");
                test.getSum();
                          
           }
      }
      The output should be:
      Grade 0 ::   100.00
      Grade 1 ::   90.00
      Grade 2 ::   85.00
      Grade 3 ::   72.50
      Grade 4 ::   95.60
      
      100.00
      100.00
      90.00
      190.00
      85.00
      275.00
      72.50
      347.50
      95.60
      443.10
      but its doing this right now
      0.0
      0.0
      1.0
      1.0
      2.0
      3.0
      3.0
      6.0
      4.0
      10.0
      5.0
      15.0
      6.0
      21.0
      7.0
      28.0
      8.0
      36.0
      9.0
      45.0
      I would greatly appreciate all the help I can get on this topic.