1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Methods, Classes, and Variables

  1. Oct 3, 2011 #1
    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 (Text):

    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 (Text):

    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());}
       
    }
     
     
  2. jcsd
  3. Oct 4, 2011 #2

    Mark44

    Staff: Mentor

    Your Student class does not have a member named ave.

    Code (Text):
    ***           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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook