Finding the median and max value of an array in java

1.1k views Asked by At

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

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 < 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 < grades.length - 1 ; i++)
        {   
            int max = 0;
            if(grades[i] > max)
            {
                max = grades[i];
            }
        }
        return max; 
    }

In my driver, I simply had to prove that the method worked correctly, so it's:

System.out.println(" The highest grade is" + grades.getHighest());
System.out.println("The median grade is" + grades.getMedian());
1

There are 1 answers

0
Ankur Singhal On BEST ANSWER

Few mistakes.

1.) Might be calling getMedian(), whereas the logic is inside median() method.

2.) Inside method, getHighest(),

a.) No need to loop the array, since array is already sorted. So i have commented the code. Just get the value at last index of array.

public class Test {
    static int    max;
    static double median;

    static int[]  grades = { 2, 3, 4, 5, 62, 34 };

    public static void main(String args[]) {
        Arrays.sort(grades);

        median();
        getHighest();

        System.out.println(median);
        System.out.println(max);
    }

    public static void 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;
    }

    public static int getHighest() {
        /* for (int i = 0 ; i < grades.length ; i++) {
         if (grades[i] > max) {
             max = grades[i];
         }
     }*/
     max = grades[grades.length - 1];
     return max;
    }

Output

4.5
62