Write a class encapsulating the concept of student grades on a test, assuming st
ID: 3819678 • 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.
Sample output shoudl look like this: any help would be awesome!
I have the following already worked out, but I'm getting errors, please help!!
import java.io.*;
import java.util.Random;
public class Grades {
private int grades[];
private int size;
private int[] indexCounter;
//constructor with argument
public Grades(int n)
{
this.setSize(n);
//random number generator
Random rand = new Random();
int[] temp = new int[n];
for(int i=0; i<n; i++)
{
temp[i] = rand.nextInt(100);
}
//setter method
this.setGrades(temp);
}
//getter and setter methods
public void setSize(int s)
{
this.size = s;
}
public int getSize()
{
return size;
}
public void setGrades(int[] g)
{
this.grades = new int[this.getSize()];
this.grades = g;
}
public int[] getGrades()
{
return grades;
}
//sorting the array of grades
public void displayGradesAscending()
{
int[] temp = getGrades();
//bubble sort
for(int i=0; i<getSize()-1; i++)
{
for(int j=0; j<getSize()-1; j++)
{
if(temp[j+1] < temp[j])
{
int t = temp[j];
temp[j] = temp[j+1];
temp[j+1] = t;
}
}
}
setGrades(temp);
for(int n : getGrades())
{
System.out.print(n+" ");
}
}
public int highestGrade()
{
int temp[] = getGrades();
int high = temp[0];
for(int i=1; i<getSize(); i++)
{
if(temp[i] > high)
{
high = temp[i];
}
}
return high;
}
public double averageGrade()
{
int temp[] = getGrades();
double sum = 0;
for(int i=0; i<getSize(); i++)
{
sum += temp[i];
}
return sum/getSize();
}
//middle element in array of grades
public int medianGrade()
{
return getGrades()[getSize()/2];
}
//removing duplicate elements
//creating indexCounter
public int mode()
{
int size = getSize();
boolean present;
int temp[] = new int[size];
int temp_size = 0;
for(int i=0; i<getSize(); i++)
{
present = false;
for(int j=0; j<=temp_size; j++)
{
if(temp[j] == getGrades()[i])
{
present = true;
break;
}
}
if(present == false)
{
temp[temp_size++] = getGrades()[i];
}
}
int indexCounter[] = new int[temp_size];
for(int i=0; i<temp_size; i++)
{
int count = 0;
for(int j=0; j<getSize(); j++)
{
if(temp[i] == getGrades()[j])
count++;
}
}
int count;
int i;
indexCounter[i] = count;
}
int high = indexCounter[0];
int mode = 0;
{
int temp_size;
for(int i=1; i<temp_size; i++)
{
if(indexCounter[i] > high)
{
high = indexCounter[i];
mode = i;
}
}
return temp[mode];
}
}
------------------------------------------
import java.util.Scanner;
public class GradesClient {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("How many students are there: ");
int grades = new Scanner(System.in).nextInt();
Grades g = new Grades(20);
System.out.println("Sorted grades:");
g.displayGradesAscending();
System.out.println("Highest grade: " +g.highestGrade());
System.out.println("Average grade: " +g.averageGrade());
System.out.println("Median grade: " +g.medianGrade());
System.out.println("Mode: " +g.mode());
}
}
Explanation / Answer
#include "GradingClass.h"
GradingClass::GradingClass(unsigned long NumberOfStudents)
{
int tries = 0;
NumStudents = NumberOfStudents;
Grades = new char[NumStudents];
GradeNumbers = new int[NumStudents];
for(unsigned int i = 0; i < NumStudents; i++)
{
GiveEmAnotherShot:
int RandomGrade = rand() % 101;
if(RandomGrade < 60 && tries < 3)
{
tries++;
goto GiveEmAnotherShot;
}
tries = 0;
GradeNumbers[i] = RandomGrade;
}
}
GradingClass::~GradingClass()
{
delete[] Grades;
delete[] GradeNumbers;
}
void GradingClass::ReturnGrades()
{
for(unsigned int i = 0; i < NumStudents; i++)
{
if(GradeNumbers[i] < 60)
{
Grades[i] = 'F';
}
else if(GradeNumbers[i] >= 60 && GradeNumbers[i] < 70)
{
Grades[i] = 'D';
}
else if(GradeNumbers[i] >= 70 && GradeNumbers[i] < 80)
{
Grades[i] = 'C';
}
else if(GradeNumbers[i] >= 80 && GradeNumbers[i] < 90)
{
Grades[i] = 'B';
}
else if(GradeNumbers[i] >= 90)
{
Grades[i] = 'A';
}
printf("Student # %u grade: %02i%% with a %c ", (i+1), GradeNumbers[i], Grades[i]);
}
}
void GradingClass::CountGrades()
{
float a = 0.0f, b = 0.0f, c = 0.0f, d = 0.0f, f = 0.0f;
Gradez.As = 0;
Gradez.Bs = 0;
Gradez.Cs = 0;
Gradez.Ds = 0;
Gradez.Fs = 0;
for(unsigned int i = 0; i < NumStudents; i++)
{
if(Grades[i] == 'A')
{
Gradez.As++;
}
else if(Grades[i] == 'B')
{
Gradez.Bs++;
}
else if(Grades[i] == 'C')
{
Gradez.Cs++;
}
else if(Grades[i] == 'D')
{
Gradez.Ds++;
}
else if(Grades[i] == 'F')
{
Gradez.Fs++;
}
}
printf(" Number Of Students With ");
printf("A: %u B: %u C: %u D: %u F: %u ", Gradez.As, Gradez.Bs, Gradez.Cs, Gradez.Ds, Gradez.Fs);
a = (float)Gradez.As / NumStudents;
b = (float)Gradez.Bs / NumStudents;
c = (float)Gradez.Cs / NumStudents;
d = (float)Gradez.Ds / NumStudents;
f = (float)Gradez.Fs / NumStudents;
a *= 100.0f;
b *= 100.0f;
c *= 100.0f;
d *= 100.0f;
f *= 100.0f;
printf("%.2f%% of students are getting an A ", a);
printf("%.2f%% of students are getting an B ", b);
printf("%.2f%% of students are getting an C ", c);
printf("%.2f%% of students are getting an D ", d);
printf("%.2f%% of students are getting an F ", f);
}
GraderApp.cpp (main file)
#include "GraderApp.h"
char input[8];
unsigned long students;
GradingClass* GC = 0;
int main()
{
SetConsoleTitleA("Grading Application");
system("color 0a");
GoAgain:
printf("Enter the number of students to generate grades for: ");
gets(input);
students = atoi(input);
GC = new GradingClass(students);
GC->ReturnGrades();
GC->CountGrades();
printf("Quit or do it agian?: ");
gets(input);
if(input[0] != 'q' && input[0] != 'Q')
{
ZeroMemory(input, 8);
delete GC;
goto GoAgain;
}
delete GC;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.