Methods, Classes, and Variables

In summary, the conversation discusses a project assigned by a professor where the user enters information for a Student object that is derived from a Person. The information includes a name, id number, two scores, an average, and a letter grade equivalent. The user is having trouble sorting the students according to their averages and is getting an error in their testing program. The error is caused by trying to access a variable named ave instead of scoreAverage. The conversation also includes the code for the testing program and the Student class, which contains methods for sorting the averages and converting them into letter grades.
  • #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 ***.
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
  • #2
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
 

Related to Methods, Classes, and Variables

1. What is a method in programming?

A method is a block of code that performs a specific task. It is used to organize and group code, making it easier to read and maintain. Methods can also be reused multiple times throughout a program, reducing the need for repetitive code.

2. What is a class in programming?

A class is a blueprint or template for creating objects in programming. It defines the properties and behaviors that an object of that class will have. Classes are essential for creating organized and scalable programs.

3. How do you declare a variable in programming?

To declare a variable in programming, you need to specify the variable's name and data type. For example, in Java, you would write: int num = 5; This declares a variable named "num" of type integer and assigns it the value of 5.

4. What is the difference between a local and a global variable?

A local variable is declared inside a method or function and can only be accessed within that method or function. A global variable, on the other hand, is declared outside of any method or function and can be accessed and modified by any part of the program. Global variables should be used sparingly as they can make code more difficult to debug and maintain.

5. How are methods, classes, and variables related?

Methods and variables are both components of classes. A class can have multiple methods and variables, which work together to define the behavior and properties of objects created from that class. Variables are used to store data, while methods are used to manipulate that data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
827
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top