Methods, Classes, and Variables

  • Thread starter Thread starter major_maths
  • Start date Start date
  • Tags Tags
    Classes Variables
Click For Summary
The discussion revolves around a programming issue related to a project involving a Student class derived from a Person class. The user encounters an error stating "cannot find symbol - variable ave" when trying to access the variable 'ave', which is not defined in the Student class. Instead, the correct variable to use is 'scoreAverage', which holds the average score for each student. The conversation highlights the importance of correctly referencing class members and understanding object-oriented principles in Java. The user is advised to replace 'ave' with 'scoreAverage' to resolve the error.
major_maths
Messages
30
Reaction score
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 ***.
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());}
    
}
 
Physics news on Phys.org
Your Student class does not have a member named ave.

Code:
***           Student.setGrade(studentArray[i].ave);

This class does have a member named scoreAverage, which is probably what you want to be using.

studentArray is the name of an array, each element of which is a Student object.
studentArray is the i-th element of the array above, and is of type Student.

The following are examples of valid expressions:
studentArray[0].scoreOne
studentArray[2].scoreTwo
studentArray[3].scoreAverage
studentArray[4].scoreGrade
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
13K