title: Finding the median and max value of an array in java
body: <p>I used this code to calculate the max value and the median element in an array of integers, but when I call the methods in my client class, both of these two methods produce an output of zero. The name of the array is "grades" and it is made of randomly generated integers</p>
  
  <pre><code>import java.util.*;
  public class StudentGrades {
  
      private int [] grades;
  
      //Constructor
      public StudentGrades ( int students)
          {
          Random number = new Random();
          grades = new int[students];
          for (int i = 0 ; i &lt; students ; i++)
              {
              grades[i] = number.nextInt(99) + 1;
              }
          }
      double median;
      public void median()
      {
          Arrays.sort(grades) ;
          double median ;
  
          if (grades.length % 2 == 0)
          {
              int indexA = (grades.length - 1 ) /2;
              int indexB = (grades.length)/2;
  
              median = ((double) (grades[indexA] + grades[indexB]))/2;
          }
          else
          {
              int medIndex = (grades.length-1) / 2;
              median = grades[ medIndex ];
          }
      }
  
      public double getMedian()
      {
          return median;
      }
  
  
      int max;
      public int getHighest()
      {
          for(int i = 0 ; i &lt; grades.length - 1 ; i++)
          {   
              int max = 0;
              if(grades[i] &gt; max)
              {
                  max = grades[i];
              }
          }
          return max; 
      }
  </code></pre>
  
  <p>In my driver, I simply had to prove that the method worked correctly, so it's:</p>
  
  <pre><code>System.out.println(" The highest grade is" + grades.getHighest());
  System.out.println("The median grade is" + grades.getMedian());
  </code></pre>