Write a class encapsulating the concept of student grades on a test, assuming st
ID: 3673656 • Letter: W
Question
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
GradeStats.java
import java.util.*;
public class GradeStats {
private int[] grades;
private int numberOfStudents;
public GradeStats(int numberOfStudents) {
this.numberOfStudents = numberOfStudents;
grades = new int[numberOfStudents];
for (int i = 0; i < numberOfStudents; ++i) {
grades[i] = (int) (Math.random() * 101);
}
}
public int[] getSortedGrades() {
int[] sorted = this.grades.clone();
Arrays.sort(sorted);
return sorted;
}
public int getHighestGrade() {
int max = -1;
for (int i = 0; i < numberOfStudents; ++i) {
max = Math.max(max, grades[i]);
}
return max;
}
public double getAverageGrade() {
double sum = 0.0;
for (int i = 0; i < numberOfStudents; ++i) {
sum += grades[i];
}
return sum / numberOfStudents;
}
public int getMedianGrade() {
int[] sorted = getSortedGrades();
if (numberOfStudents % 2 == 0) {
int half = numberOfStudents / 2;
return (sorted[half] + sorted[half - 1]) / 2;
} else {
return sorted[(numberOfStudents + 1) / 2];
}
}
public int getModeGrade() {
int[] sorted = getSortedGrades();
int i = 0;
int mode = -1;
int maxTillNow = -1;
while (i < numberOfStudents) {
int count = 0;
int current = sorted[i];
while (i < numberOfStudents && sorted[i] == current) {
++i;
++count;
}
if (count > maxTillNow) {
mode = current;
maxTillNow = count;
}
}
return mode;
}
public int[] getGrades() {
return this.grades;
}
public void setGrades(int[] grades) {
this.grades = grades;
}
@Override
public String toString() {
return "Grades: " + grades.toString();
}
public boolean equals(GradeStats b) {
if (this.numberOfStudents == b.numberOfStudents) {
for (int i = 0; i < numberOfStudents; ++i) {
if (this.grades[i] != b.getGrades()[i]) {
return false;
}
}
return true;
} else {
return false;
}
}
}
Main.java
public class Main {
public static void main(String[] args) {
GradeStats gs = new GradeStats(10);
System.out.print("Grades: ");
int[] grades = gs.getGrades();
for (int i = 0; i < grades.length; ++i) {
System.out.print(grades[i] + " ");
}
System.out.println();
System.out.print("Sorted Grades: ");
int[] sortedGrades = gs.getSortedGrades();
for (int i = 0; i < sortedGrades.length; ++i) {
System.out.print(sortedGrades[i] + " ");
}
System.out.println();
System.out.println("Highest Grade: " + gs.getHighestGrade());
System.out.println("Average Grade: " + gs.getAverageGrade());
System.out.println("Median Grade: " + gs.getMedianGrade());
System.out.println("Mode Grade: " + gs.getModeGrade());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.