Write a class encapsulating the concept of student grades on
Write a class encapsulating the concept of student grades on a test, assuming student grades are composed of a list of integers between 0 and 100. Write the following methods: - A constructor with just one parameter, the number of students; all grades can be randomly generated. - Accessor, mutator, toString, and equals methods. - A method returning an array of the grades sorted in ascending order. - A method returning the highest grade. - A method returning the average grade. - A method returning the median grade (Hint: The median grade will be located in the middle of the sorted array of grades.) - A method returning the mode (the grade that occurs most often). (Hint: Create an array of counters; count how many times each grade occurs; then pick the maximum in the array of counters; the array index is the mode.) Write a client class to test all the methods in your class.
Thank you for your help!
Solution
import java.util.Arrays;
import java.util.Random;
public class tut
{
public static void main (String [] args)
{
//generating random number
Random rand = new Random();
//number of student in class
int number_of_student = 70;
int sum=0;
int median;
int [] myArray = new int[number_of_student];
// alloting random number in the array
for (int i=0 ; i < number_of_student ; i++){
int a =0;
//System.out.println(i);
a = rand.nextInt(100) + 1;
myArray[i]=a;
sum = sum +a;
}
// sort the array
Arrays.sort( myArray );
//finding the median of the array
int middle = ((myArray.length) / 2);
if(myArray.length % 2 == 0){
int medianA = myArray[middle];
int medianB = myArray[middle-1];
median = (medianA + medianB) / 2;
}
else{
median = myArray[middle + 1];
}
System.out.println(\"list of array : \");
System.out.println(Arrays.toString(myArray));
System.out.println(\"Highest Grade : \" + myArray[number_of_student-1]);
System.out.println(\"Average of the class : \" + sum/number_of_student);
System.out.println(\"Median : \" + median);
System.out.println(\"Mode : \" + mode(myArray));
}
public static int mode(int[] input) {
int[] count = new int[101];
//count the occurrences
for (int i=0; i < input.length; i++) {
count[input[i]]++;
}
//go backwards and find the count with the most occurrences
int index = count.length-1;
for (int i=count.length-2; i >=0; i--) {
if (count[i] >= count[index])
index = i;
}
return index;
}
public static void rand_array (int[] input){
}
}

