Hello. I am writing a program that takes in thirty grades for a class and outputs the mean, median, and mode of the entire class. I am confused about how to output the data using the toString method. Please help! Also if you find any other errors that aren't related to outputting the data then please tell me how to correct them. Thank you.
I'll post my parent class at top and subclass at the bottom.
This is my parent class:
This is my subclass:
I'll post my parent class at top and subclass at the bottom.
This is my parent class:
import java.util.Scanner;
public class ClassGrades
{
// instance variables
static int [] gradeSet;
static int mean;
//static int median;
static int grades;
// override the toString method
public String toString()
{
return ("Mean:"+ClassGrades.getMean(gradeSet) +"\n"+"Median:"
+ClassGrades.getMedian(gradeSet) +"\n"+"Mode:"
+ClassGrades.getMode(gradeSet));
}
// set the grade values
public static int [] setGrades()
{
Scanner keyboard = new Scanner(System.in);
int[] gradeSet = new int[3];
int b = 0;
System.out.print("Hello Teach! Please enter the grades of your thirty students below\n"
+ "Enter negative number to cancel.\n");
for(int i = 0; i < gradeSet.length; ++i) {
System.out.println("Please Enter Grade: \n");
gradeSet[i] = keyboard.nextInt();
if(gradeSet[i] < 0 && gradeSet.length > 2)
break;
++b;
}
return (gradeSet);
}
//calculate the mean
public static int getMean(int gradeSet [])
{
int sum = 0;
int average=0;
for(int j = 0; j < gradeSet.length; j++)
{
sum += gradeSet[j];
average = sum / gradeSet.length;
}
return average;
}
//calculate median
public static int getMedian(int gradeSet[])
{
int median;
if (grades % 2 != 0)
{
median = gradeSet[gradeSet.length / 2];
}
else
{
median = (gradeSet[(gradeSet.length / 2) - 1] +
gradeSet[gradeSet.length / 2]) / (int)2;
}
return (median);
}
//calculate mode
public static int getMode(int gradeSet[])
{
int max=0, maxCount=0;
int length=gradeSet.length;
for (int i = 0; i <length; ++i)
{
int count = 0;
for (int j = 0; j <length; ++j)
{
if (gradeSet[j] == gradeSet[i]) ++count;
}
if (count > maxCount)
{
maxCount = count;
max = gradeSet[i];
}
}
return max;
}
}
This is my subclass:
import classgrades.*;
import java.util.Scanner;
public class ClassGradesTest extends ClassGrades
{
public static int getMean(){
int m = ClassGrades.getMean(gradeSet);
return (m);
}
public static int getMedian(){
int med = ClassGrades.getMedian(gradeSet);
return (med);
}
public static int getMode(){
int mod=ClassGrades.getMode(gradeSet);
return (mod);
}
public static void main(String[] args)
{
ClassGrades.setGrades();
}
}