Write a class encapsulating the concept of student grades on a test, assuming st
ID: 3630037 • 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:
//1.A constructor with just one parameter, the number of students; all grades can be randomly generated.
//2.Accessor, mutator methods
3.A method returning an array of grades sorted in ascending order
4.A method returning the highest grade
5.A method returning the average grade
6.A method returning the median grade (Hint: The median grade will be located in the middle of the sorted array of grades
7.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 of the methods in your class.
Explanation / Answer
import java.util.Scanner;
import java.util.*;
public class TestGrades
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n;
System.out.print("Enter the number of students : ");
n=in.nextInt();
Grades grade = new Grades(n);
grade.sort();
grade.highest();
grade.calAverage();
grade.calMedian();
grade.calMode();
System.out.println("The sorted array is: "+grade.toString());
System.out.println("Highest grade: "+grade.getHigh());
System.out.println("Average grade: "+grade.getAverage());
System.out.println("Median grade: "+grade.getMedian());
System.out.println("Mode: "+grade.getMode());
}
}
class Grades
{
private int[] grade;
private int highest,mode;
private double average,median;
public Grades(int n)
{
Random r=new Random();
grade = new int[n];
for(int i = 0;i<n; i++)
grade[i] = Math.abs(r.nextInt())%101;
}
public double getTotal()
{
int total = 0;
int i;
for(i=0;i<grade.length;i++)
total+=grade[i];
return total;
}
public void calAverage()
{
average= getTotal() / (double)grade.length;
}
public int getHigh()
{
return highest;
}
public double getMedian()
{
return median;
}
public int getMode()
{
return mode;
}
public double getAverage()
{
return average;
}
public void highest()
{
highest = grade[grade.length-1];
}
public void sort()
{
int i,j,t;
for(i=0;i<grade.length-1;i++)
for(j=i+1;j<grade.length;j++)
if(grade[i]>grade[j])
{
t=grade[i];
grade[i]=grade[j];
grade[j]=t;
}
}
public void calMedian()
{
if(grade.length%2==0)
median=(grade[grade.length/2-1]+grade[(grade.length)/2])/2.;
else
median=grade[grade.length/2];
}
public void calMode()
{
int []a=new int[101];
int i,m;
for(i=0;i<=grade.length;i++)
a[i]=0;
for(i=0;i<grade.length;i++)
a[grade[i]]++;
m=0;
for(i=0;i<a.length;i++)
if(a[i]>a[m])
m=i;
mode=m;
}
public String toString()
{
int i;
String s="";
for(i=0;i<grade.length;i++)
{
s=s+grade[i]+" ";
if((i+1)%10==0)
s=s+' ';
}
return s;
}
}
Sample Output:
Enter the number of students : 10
The sorted array is:
12 21 39 50 55 56 57 63 70 84
Highest grade: 84
Average grade: 50.7
Median grade: 55.5
Mode: 12
Hope this would help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.