Write a class encapsulating the concept of student grades on a test, assuming st
ID: 3626953 • 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: 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 the methods in your class.
Explanation / Answer
please rate - thanks
message me if any problems
import java.util.Scanner;
public class TestGrades
{public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int n;
System.out.print("How many students are there: ");
n=in.nextInt();
Grades g=new Grades(n);
g.sort();
g.highest();
g.calAverage();
g.calMedian();
g.calMode();
System.out.println("The sorted array is: "+g.toString());
System.out.println("Highest grade: "+g.getHigh());
System.out.println("Average grade: "+g.getAverage());
System.out.println("Median grade: "+g.getMedian());
System.out.println("Mode: "+g.getMode()+" or it is multi modal, and this is the first value");
}
}
---------------------------------
import java.util.*;
public 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.