- #1
major_maths
- 30
- 0
My professor assigned a project that has the user enter information for an Object called Student which is derived from Person. The information is a name, an id number, two scores, an average, and the letter grade equivalent of the average. I'm having trouble sorting the student's according to their averages.
This is my testing program where I'm getting an error: "cannot find symbol - variable ave". I don't understand why the error is occurring as I've declared the variable ave to a type double just three lines before that. I marked the line where the error is occurring with ***.
And this is the Student class where the methods are:
This is my testing program where I'm getting an error: "cannot find symbol - variable ave". I don't understand why the error is occurring as I've declared the variable ave to a type double just three lines before that. I marked the line where the error is occurring with ***.
Code:
import java.util.*;
public class TestStudent
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Student[] studentArray = new Student[5];
System.out.println("Please enter data for five students: ");
for(int i=0; i<5; i++)
{
String n = keyboard.next();
int idNumber = keyboard.nextInt();
int s1 = keyboard.nextInt();
int s2 = keyboard.nextInt();
double ave = (s1+s2)/2.0;
studentArray[i] = new Student(n,idNumber,s1,s2,ave);
*** Student.setGrade(studentArray[i].ave);
}
System.out.println();
System.out.println("The students are: ");
for(int i=0;i<5;i++)
{
System.out.println(studentArray[i]);
System.out.println();
}
System.out.println("Would you like to sort these students' scores according to average?");
String response3 = keyboard.next();
char letter3 = response3.charAt(0);
String response4 = "n";
double[] numbers = new double[5];
do{
switch (letter3)
{
case 'y':
System.out.println();
System.out.println("Great! Here you go: ");
Student.selectionSort(studentArray);
System.out.println();
for(int l=0; l<studentArray.length; l++)
{
System.out.println(studentArray[l]);
for(int z=0; z<studentArray.length; z++)
{
System.out.println(studentArray[z].getGrade());
System.out.println();
}
}
break;
case 'Y':
System.out.println();
System.out.println("Cool! Here you go: ");
Student.selectionSort(studentArray);
System.out.println();
for(int l=0; l<studentArray.length; l++)
{
System.out.println(studentArray[l]);
for(int z=0; z<studentArray.length; z++)
{
System.out.println(studentArray[z].getGrade());
System.out.println();
}
}
break;
case 'n':
break;
case 'N':
break;
default:
System.out.println("Could not recognize answer.");
}
}while(response3.equalsIgnoreCase("y")&&response4.equalsIgnoreCase("y"));
}
}
And this is the Student class where the methods are:
Code:
import java.util.Scanner;
public class Student extends Person
{
private int scoreOne;
private int scoreTwo;
double scoreAverage;
private char scoreGrade;
//default constructor
public Student()
{
super();
scoreOne=0;
scoreTwo=0;
scoreAverage=0;
}
//parameterized constructor
public Student(String theName, int theId, int scr1, int scr2, double ave)
{
super(theName, theId);
scoreOne=scr1;
scoreTwo=scr2;
scoreAverage=ave;
}
//sorts given averages
public static void selectionSort(Student[] studentArray)
{
int min_index;
Student temp;
for(int i=0; i<studentArray.length-1; i++)
{
min_index = i;
for(int j=min_index+1; j<studentArray.length; j++)
{
if (studentArray[j].scoreAverage < studentArray[min_index].scoreAverage)
{min_index=j;}
}
temp = studentArray[min_index];
studentArray[min_index] = studentArray[i];
studentArray[i] = temp;
}
}
//calculates an average
public double getAverage(Person p)
{
scoreAverage=(scoreOne+scoreTwo)/2;
return scoreAverage;
}
//converts an average into a grade letter
public void setGrade(double scoreAverage)
{
if(scoreAverage>=90)
scoreGrade='A';
else if(scoreAverage>=80)
scoreGrade='B';
else if(scoreAverage>=70)
scoreGrade='C';
else if(scoreAverage>=60)
scoreGrade='D';
else
scoreGrade='E';
}
//changes a letter grade
public char getGrade()
{return scoreGrade;}
//changes an average
public void setAve(double altAverage)
{scoreAverage=altAverage;}
public String toString()
{return(super.toString()+"\nScore One: "+scoreOne+"\nScore Two: "+scoreTwo+"\nAverage: "
+scoreAverage+"\nLetter Grade: "+getGrade());}
}