0

I've been studying code on my own, and I got a problem that I do not know how to answer. I am given a student and classroom class, and from those two I need to be able to create a method for getTopStudent, as well as thegetAverageScore. **Edit: All of the code was given except for the two methods, I needed to create those 2. The thing is that I'm not sure if what I'm doing is correct.

  public class Student
{
private static final int NUM_EXAMS = 4;

private String firstName;
private String lastName;
private int gradeLevel;
private double gpa;

private int[] exams;
private int numExamsTaken;

public Student(String fName, String lName, int grade)
{
    firstName = fName;
    lastName = lName;
    gradeLevel = grade;
    exams = new int[NUM_EXAMS];
    numExamsTaken = 0;
}

 public double getAverageScore() //this is the method that I need to, but I'm not sure if it is even correct. 
{
    int z=0;
    for(int i =0; i<exams.length; i++)
    {
        z+=exams[i];
    }
    return z/(double) numExamsTaken;
}

public String getName()
{
    return firstName + " " + lastName;
}

public void addExamScore(int score)
{
    exams[numExamsTaken] = score;
    numExamsTaken++;
}

public void setGPA(double theGPA)
{
    gpa = theGPA;
}

public String toString()
{
    return firstName + " " + lastName + " is in grade: " + gradeLevel;
}

}

public class Classroom
{
    Student[] students;
    int numStudentsAdded;

public Classroom(int numStudents)
{
    students = new Student[numStudents];
    numStudentsAdded = 0;
}

public Student getTopStudent() //this is the other method I need to create
{
    int x=0;
    int y=0;
    for(int i =0; i<numStudentsAdded; i++)
    {
        if(x<students.getAverageScore())
        {
            x=students.getAverage();
            y++;
        }
    }
    return students[y];
}

public void addStudent(Student s)
{
    students[numStudentsAdded] = s;
    numStudentsAdded++;
}

public void printStudents()
{
    for(int i = 0; i < numStudentsAdded; i++)
    {
        System.out.println(students[i]);
    }
}

}

I have something down for each of them but it isn't running. I don't fully understand arrays yet, but this is apparently a beginner code using arrays. If anyone could help with what I need to do and tell me how arrays work, much would be appreciated.

  • You probably want to look at ArrayList instead of creating an Array of Students. One problem you're having is that your addStudent() method is not adding a new Student. – Glen Pierce Apr 30 '17 at 02:27
  • @Edgar Flores you haven't specified the problem you're having? – Ousmane D. Apr 30 '17 at 02:30
  • Because it just makes the last Student in the Array = the Student they're "adding". As soon as they try to add more students than the Array got initialized to, they're going to have problems. – Glen Pierce Apr 30 '17 at 02:30
  • http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes – Glen Pierce Apr 30 '17 at 02:38

2 Answers2

0

getAverageScore() is a method of Student. But students is not a Student object, it's an array of Student objects. (And getAverage(), which you call inside the for loop, isn't a method at all.) An array is a separate object that contains other objects or primitives (like ints) in it. So students.getAverageScore() is not going to compile, because students doesn't have that method, each of its members (student[0], student[1], etc.) has it.

Try replacing the getTopStudent method with something like this:

public Student getTopStudent() //this is the other method I need to create
{
    int x=0; //this will contain the highest average
    int y=0; //this will be the index in the array of the highest scoring student

    for(int i =0; i<numStudentsAdded; i++)
    {
        int currentAverage = students[i].getAverageScore(); //run the getAverageScore() on the current student
        if(x<currentAverage) // compare it to the previous high average
        {
            x=currentAverage; // replace x with new high average
            y=i; //replace the index of the highest scoring student with current index i
        }
    }
    return students[y]; // so if the fifth student had the highest score, y would be 4
}
jimboweb
  • 4,362
  • 3
  • 22
  • 45
0

So you are having trouble int the method public Student getTopStudent()

public Student getTopStudent() //this is the other method I need to create
{
double x= students[0].getAverageScore();
int y = 0;
for(int i=1;i<students.length;i++){
    if(x<students[i].getAverageScore()) {
        x = students[i].getAverageScore();
        y =i;
    }   
}
return students[y];
}

See if this helps

Kangkan
  • 127
  • 1
  • 2
  • 10