Java Write a class encapsulating the concept of student grades on a test, assumi
ID: 3673137 • Letter: J
Question
Java
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 will 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 the 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 in the array of counter should correspond to the same index in the array of grades - use that index to find the grade value that is the mode)
Write a client class to test all your methods.
Explanation / Answer
Implemented following 5 sub-parts of the problem:
1)A constructor with just one parameter, the number of students; all grades will be randomly generated.
2)A method returning an array of the grades sorted in ascending order.
3)--A method returning the highest grade --a method returning the average grade
4)--a method returning the median grade[Hint: the median grade will be located in the middle of the sorted array of grades]
5)Write a client class to test all your methods
import java.util.Random;
public class StudentGradesRandomTest {
public static void main(String[] args)
{
StudentGradesRandom sg = new StudentGradesRandom(15);
System.out.println(" Random Grades generated:");
StudentGradesRandom.printGrades(sg.getGrades());
System.out.println(" Sorted Grades generated:");
StudentGradesRandom.printGrades(sg.sortGradesAsc());
System.out.println(" Highest Grade:"+sg.getHighestGrade());
System.out.println(" Median Grade:"+sg.getMedianGrade());
}
}
class StudentGradesRandom {
private int[] grades;
public StudentGradesRandom(int students) {
Random rand = new Random();
int max = 100;
int min = 0;
grades = new int[students];
for(int i=0; i < students; i++)
grades[i] = rand.nextInt((max - min) + 1) + min;
}
public int[] sortGradesAsc() {
int[] sortedArr = new int[grades.length];
for(int i=0; i < grades.length; i++) {
sortedArr[i] = grades[i];
}
int temp;
for(int i=0; i < sortedArr.length; i++) {
for(int j=0; j < sortedArr.length; j++) {
if(sortedArr[i] < sortedArr[j]) {
temp = sortedArr[i];
sortedArr[i] = sortedArr[j];
sortedArr[j] = temp;
}
}
}
return sortedArr;
}
public int getHighestGrade() {
int max = 0;
for(int i=0; i < grades.length; i++) {
if(max < grades[i])
max = grades[i];
}
return max;
}
public int getMedianGrade() {
int median = 0;
int sortedGrades[] = sortGradesAsc();
//middle of the sorted array
median = sortedGrades[sortedGrades.length/2-1];
return median;
}
public int[] getGrades() {
return grades;
}
public static void printGrades(int[] grades) {
for(int i=0; i < grades.length; i++)
System.out.print(grades[i]+" ");
System.out.println();
}
}
---output---
Random Grades generated:
58 92 3 81 62 20 65 42 5 96 39 76 46 51 97
Sorted Grades generated:
3 5 20 39 42 46 51 58 62 65 76 81 92 96 97
Highest Grade:97
Median Grade:51
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.