title: Using and manipulating arrays without Array.replace Array.copy
body: <p>Ok well I posted a question with a small portion of my code.  I realized that it would be easier for people to answer if they could see the entire code.  I get nullpointerException when I run the program.  I am not allowed to use .replace .copy .remove with regards to arrays. Im not sure why but thats what my teacher wants.</p>
  
  <p>Class Grades:</p>
  
  <pre><code>public class Grades {
  
      // Fields
  
      private float[] listOfGrades;
      private int numOfScores;
  
      //
      // Constructor
      public Grades() {
          float[] listOfGrades = new float[0];
      }
  
      public Grades(float[] g) {
  
          for (int i = 0; i &lt; g.length; i++) {
              if (g[i] &gt;= 0 &amp;&amp; g[i] &lt;= 100) {
                  listOfGrades[i] = g[i];
  
              }
          }
  
      }
  
      //
      // AccessorMethods
      public int getNumberOfScores() {
          try {
              int numOfScores = 0;
              for (int i = 0; i &lt; listOfGrades.length; i++) {
                  numOfScores++;
  
              }
          } catch (Exception e) {
  
          }
  
          return numOfScores;
      }
  
      public float[] getCopyOfListOfGrades() {
          float[] listOfGradesCopy = new float[listOfGrades.length];
  
          for (int i = 0; i &lt; listOfGrades.length; i++) {
  
              listOfGrades[i] = listOfGradesCopy[i];
          }
          return listOfGradesCopy = listOfGrades;
  
      }
  
      public float getFirstGrade() {
  
          return listOfGrades[0];
  
      }
  
      public float findMax() {
  
          float largest = listOfGrades[0];
  
          for (int i = 0; i &lt; listOfGrades.length; i++) {
              if (listOfGrades[i] &gt; largest) {
                  largest = listOfGrades[i];
              }
          }
          return largest;
      }
  
      public float findMin() {
  
          float largest = listOfGrades[0];
  
          for (int i = 0; i &lt; listOfGrades.length; i++) {
              if (listOfGrades[i] &lt; largest &amp;&amp; listOfGrades[i] != 0) {
                  largest = listOfGrades[i];
              }
          }
          return largest;
      }
  
      public float calculateAverage() {
          float avg = 0;
          for (int i = 0; i &lt; listOfGrades.length; i++) {
              if (listOfGrades[i] != 0) {
                  avg += listOfGrades[i];
              }
          }
          return avg / 100;
  
      }
  
      public void addGrade(float g) {
          if (g &gt;= 0 &amp;&amp; g &lt;= 100) {
              float[] newListOfGrades = new float[listOfGrades.length + 1];
  
              for (int i = 0; i &lt; newListOfGrades.length; i++) {
  
                  newListOfGrades[i] = listOfGrades[i];
                  if (listOfGrades.length == i) {
  
                      listOfGrades[i] = g;
                  }
              }
  
          } else {
              System.out.println("Sorry number not in range");
          }
      }
  
      public Grades deleteGrade(float g) {
  
          float[] result = new float[listOfGrades.length - 1];
          int index = 0;
          for (int i = 0; i &lt; listOfGrades.length; i++) {
              if (listOfGrades[i] == g) {
  
                  result[index] = listOfGrades[i];
                  index++;
              }
          }
          Grades newGrade = new Grades(result);
  
          return newGrade;
  
      }
  
  }
  </code></pre>
  
  <p>Class GradesTest:</p>
  
  <pre><code>/* Grades Client */
  public class GradesTest {
      public static void main(String[] args) {
  
          // Test the default constructor.
          Grades emptyScores = new Grades();
  
          if (emptyScores.getNumberOfScores() != 0)
              throw new Error("default constructor failed");
  
          // Now test the parameterized Constructor
          float[] a1 = { 70, 60, 80, 90, 100 };
          Grades allQuizScores = new Grades(a1);
  
          if (allQuizScores.getNumberOfScores() != 5)
              throw new Error("Parameterized constructor failed");
  
          float[] a = { 70, -34, 108, 60, 80, -8.9f, 90, 100 };
          allQuizScores = new Grades(a);
  
          // Now test getNumberOfScores
          if (allQuizScores.getNumberOfScores() != 5)
              throw new Error("Parameterized constructor failed with invalid grades");
  
          if (emptyScores.getFirstGrade() != -1)
              throw new Error("getFirstGrade failed to handle empty arrays");
  
          a[0] = -100;
  
          // Now check that allQuizScores didn't change
          if (allQuizScores.getFirstGrade() == -100)
              throw new Error("Constructor failed to make a copy of array");
  
          // Check getCopyOfListOfGradeds
          a = emptyScores.getCopyOfListOfGrades();
          if (a.length != 0)
              throw new Error("getCopyOfListOfGrades failed on empty array");
  
          a = allQuizScores.getCopyOfListOfGrades();
          a[0] = -100;
          if (allQuizScores.getFirstGrade() == -100)
              throw new Error("getCopyOfListOfGrades failed to make a copy of array");
  
          // Now check that min,max, and average are correct
          if (allQuizScores.findMin() != 60)
              throw new Error("failed to find minimum value");
  
          if (emptyScores.findMin() != -1)
              throw new Error("findMin failed to handle case of empty array");
  
          if (allQuizScores.findMax() != 100)
              throw new Error("failed to find maximum value");
  
          if (emptyScores.findMax() != -1)
              throw new Error("findMax failed to handle case of empty array");
  
          if (allQuizScores.calculateAverage() != 80)
              throw new Error("failed to find average value");
  
          if (emptyScores.calculateAverage() != -1)
              throw new Error("findAverage failed to handle case of empty array");
  
          // Testing addGrade
          emptyScores.addGrade(10);
          if (emptyScores.findMin() != 10 || emptyScores.getNumberOfScores() != 1)
              throw new Error("addGrade fails on empty array");
          allQuizScores.addGrade(10);
          if (allQuizScores.findMin() != 10 || allQuizScores.getNumberOfScores() != 6)
              throw new Error("addGrade fails on non-empty array");
          allQuizScores.addGrade(-10);
          if (allQuizScores.findMin() == -10)
              throw new Error("addGrade fails for invalid grades");
  
          allQuizScores.addGrade(10);
          allQuizScores.addGrade(10);
          Grades newQuizScores = allQuizScores.deleteGrade(allQuizScores.findMin());
          if (newQuizScores.getNumberOfScores() != 7)
              throw new Error("deleteGrade failed for non-empty array");
          Grades emptyScores2 = emptyScores.deleteGrade(10);
          if (emptyScores2.getNumberOfScores() != 0)
              throw new Error("deleteGrade failed for single-item array");
          emptyScores2 = emptyScores.deleteGrade(10);
          if (emptyScores2.getNumberOfScores() != 0)
              throw new Error("deleteGrade failed for empty array");
  
          // Test equals
          if (emptyScores.equals(allQuizScores))
              throw new Error("Equals fails");
          if (newQuizScores.equals(allQuizScores))
              throw new Error("Equals fails");
          float[] b = { 70, 60, 80, 90, 100, 10, 10 };
  
          Grades gr = new Grades(b);
          System.out.println(gr.toString());
          System.out.println(newQuizScores.toString());
          if (!newQuizScores.equals(gr))
              throw new Error("equals fails for non-empty array");
          System.out.println(emptyScores.toString());
          Grades gr2 = new Grades();
          if (!emptyScores2.equals(gr2))
              throw new Error("equals fails for empty array");
  
          // test toString
          System.out.println("The grades without the minimum grade are:\n" + allQuizScores.toString());
          System.out.println("If you are happy with the formatting, you're done!");
      }
  }
  </code></pre>