Write a class encapsulating the concept of student grades on a test, assuming st
ID: 3569582 • 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 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: Creat 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.
Explanation / Answer
Program Code:
import java.util.*;
//StudentGrades class encapsulation
public class StudentGrades
{
//declare the grade variable of array of integer type
private int[] grades;
//declare the other required varibles
private int highest,mode;
private double average,median;
//constructor with size of the array as parameter
public StudentGrades(int n)
{
//Random class object
Random r=new Random();
//initialize the grade array
grades = new int[n];
//randomly generate the values inside the
//array
for(int i = 0;i<n; i++)
grades[i] = Math.abs(r.nextInt())%101;
}
//getTotal method will return the Total value of grades
public double getTotal()
{
int total = 0;
int i;
for(i=0;i<grades.length;i++)
total+=grades[i];
return total;
}
//calAverage will calculate the average of the StudentGrades
public void calAverage()
{
average= getTotal() / (double)grades.length;
}
//getHighest will return the highest grade
public int getHighest()
{
return highest;
}
//Returns the median of the Studentgrades
public double getMedian()
{
return median;
}
//return the mode of the Studentgrades
public int getMode()
{
return mode;
}
//returns the average grade of the Studentgrades
public double getAverage()
{
return average;
}
//highest will compute the highest of the grades.
public void highest()
{
highest = grades[grades.length-1];
}
//Sort the array of grades.
public void sort()
{
int i,j,t;
for(i=0;i<grades.length-1;i++)
{
for(j=i+1;j<grades.length;j++)
{
if(grades[i]>grades[j])
{
t=grades[i];
grades[i]=grades[j];
grades[j]=t;
}
}
}
}
//to calculate the Median of the StudentGrades
public void calMedian()
{
if(grades.length%2==0)
median=(grades[grades.length/2-1]+grades[(grades.length)/2])/2.;
else
median=grades[grades.length/2];
}
//to calculate the Mode of the grades
public void calMode()
{
//create an array a.
int []a=new int[101];
int i,m;
//initialize the array a with zero
for(i=0;i<=grades.length;i++)
a[i]=0;
//assign the grades value as index inside the array a
for(i=0;i<grades.length;i++)
a[grades[i]]++;
m=0;
//compute the mode of array
for(i=0;i<a.length;i++)
{
if(a[i]>a[m])
m=i;
}
//assign the value into mode
mode=m;
}
//toString method
public String toString()
{
int i;
String s="";
for(i=0;i<grades.length;i++)
{
s=s+grades[i]+" ";
if((i+1)%10==0)
s=s+" ";
}
return s;
}
}
--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
//test class StudentGradesTest
public class StudentGradesTest
{
//main method
public static void main(String[] args)
{
//Scanner object
Scanner in=new Scanner(System.in);
//int variable to store size
int n;
//prompt the user to enter the size of array value
System.out.print("How many students are there: ");
n=in.nextInt();
//create the object to the StudentGrades class
StudentGrades sg=new StudentGrades(n);
//sort the grades of the students
sg.sort();
//calculate the highest grade
sg.highest();
//call the Average method that computes the average of the grades
sg.calAverage();
//call the Median method that computes the average of the grades
sg.calMedian();
//call the Mode method that computes the average of the grades
sg.calMode();
//display the values.
System.out.println("The sorted array is: "+sg.toString());
System.out.println("Highest grade: "+sg.getHighest());
System.out.println("Average grade: "+sg.getAverage());
System.out.println("Median grade: "+sg.getMedian());
System.out.println("Mode: "+sg.getMode()+" or it is multi modal, and this is the first value");
}
}
----------------------------------------------------------------------------------------------------------
Sample Output:
How many students are there: 10
The sorted array is:
5 5 22 37 45 60 67 69 83 85
Highest grade: 85
Average grade: 47.8
Median grade: 52.5
Mode: 5 or it is multi modal, and this is the first value
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.