In Java, w rite a class encapsulating the concept of student grades on a test, a
ID: 3673710 • Letter: I
Question
In 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
import java.util.Arrays;
import java.util.Random;
public class Grade {
private int grade[];
public Grade(int number){
grade = new int[number];
// filling grade array with random numbers [0-100]
Random random = new Random();
for(int i=0; i<number; i++){
grade[i] = random.nextInt(101);
}
}
public int[] getGrade(){
return grade;
}
@Override
public String toString() {
String str = "";
for(int i=0; i<grade.length; i++){ // appending all grades
str = str+grade[i]+" ";
}
return str;
}
public boolean equals(int g) {
for(int i=0; i<grade.length; i++){
if(grade[i] == g)
return true;
}
return false;
}
public int[] sorted(){
// creating a copy of original array
int copy[] = Arrays.copyOf(grade, grade.length);
Arrays.sort(copy);
return copy;
}
public int max(){
int max = grade[0];
for(int i=1; i<grade.length; i++){
if(grade[i] > max)
max = grade[i];
}
return max;
}
public int min(){
int min = grade[0];
for(int i=1; i<grade.length; i++){
if(grade[i] < min)
min = grade[i];
}
return min;
}
public int median(){
// getting sorted array
int copy[] = sorted();
int length = copy.length;
// if length is even , them median will be
if(length%2 == 0)
return copy[length/2 - 1];
else // odd length
return copy[length/2];
}
public int mode(){
//creating counting array
int temp[] = new int[101];
for(int i=0; i<grade.length; i++){
temp[grade[i]]++;
}
//finding maximum in counting array
int max = temp[0];
int maxIndex = 0;
for(int i=1; i<temp.length; i++){
if(temp[i] > max){
max = temp[i];
maxIndex = i;
}
}
return maxIndex;
}
}
class Test{
public static void main(String[] args) {
Grade g = new Grade(15);
System.out.println(g);
System.out.println("Max: "+g.max());
System.out.println("Min: "+g.min());
System.out.println("Median: "+g.median());
System.out.println("Mod: "+g.mode());
}
}
/*
Output:
52 92 69 69 50 86 45 99 40 35 49 17 87 90 84
Max: 99
Min: 17
Median: 69
Mod: 69
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.