0

This program is a work in progress. In it, I created an array of ten objects with five pieces of different data types for each object. I need to find the highest score for q1 which I hoped to accomplish by creating a loop that would compare the variable highScore with each q1 data(8, 3, 10, 8, 9, 7.5, 8.5, 6, 7.5, 7) as the loop went through its cycles, however, I am getting an error message that says "The operator < is undefined for the argument type(s) double, ClassGrade" at the line that is second from the bottom. I don't understand why I am getting this error message, but i suspect that the reason i am getting it is that I am not correctly specifying the specific element that i am trying to access from each object. Any help on the matter would be greatly appreciated.

public class ClassGrade {
public String studentID;
public double q1;
public double q2;
public int mid;
public int finalExam;


public ClassGrade(String studentID, double q1, double q2, int mid, int finalExam) 
{
    // TODO Auto-generated constructor stub with a few modifications
}

public static void main(String[] args) {
    System.out.println("this program works");
    double highScore;
    highScore = 0;
    ClassGrade [] student = new ClassGrade[10];
    student[0] = new ClassGrade ("A1", 8, 8.5, 84, 82);
    student[1] = new ClassGrade ("B2", 3, 7, 0, 99);
    student[2] = new ClassGrade ("C3", 10, 9.5, 92, 84);
    student[3] = new ClassGrade ("D4", 8, 8, 89, 86);
    student[4] = new ClassGrade ("E5", 9, 10, 83, 91);
    student[5] = new ClassGrade ("F6", 7.5, 8.5, 88, 69);
    student[6] = new ClassGrade ("G7", 8.5, 0, 81, 52);
    student[7] = new ClassGrade ("H8", 6, 8.5, 79, 68);
    student[8] = new ClassGrade ("I9", 7.5, 6, 72, 91);
    student[9] = new ClassGrade ("J10", 7, 6.5, 58, 77);
    for(int i=0; i<10; i++){
        if (highScore < student[i])
            highScore = student[i];

   }




}

}

2
  • 3
    Hint 1: the TODO is there for a good reason :) Commented Jan 23, 2016 at 23:26
  • Another Hint: student[i] accesses the ClassGrade object at that index. You still need to specify the field such as student[i].q1. Commented Jan 23, 2016 at 23:27

4 Answers 4

1

First, you need to assign your instance variables in you constructor.

You are comparing a double (highscore) with a ClassGrade (student[i]).

You need to create public methods in ClassGrade to return your desired properties.

Accessing an object's properties from an array is just the same way as from a single object. You fetch the object from the array and use '.' to access its public properties or methods. E.g:

array[i].method()

Sign up to request clarification or add additional context in comments.

Comments

0

You are comparing the highscore with the actual object in the array, you are comparing a student with grade, so just make some small change - declare a method in your ClassGrade class like getQ1() and then access the q1 from the loop

Comments

0

This should work:

ClassGrade classGrade = (ClassGrade) student[i];
classGrade.method();

1 Comment

Can you please explain how it helps?
0

Each member of the array is still a ClassGrade, so all you need to do is check its q1 member like you would any other ClassGrade's q1.

for(int i=0; i<10; i++){
    if (highScore < student[i].q1)
        highScore = student[i].q1;
}

Just think about it as if the array index is part of the name, and it'll make more sense.

// Consider this:
studentZero = new ClassGrade("A1", 8, 8.5, 84, 82);

if (highScore < studentZero)
    highScore = studentZero;

// studentZero is not a double.  Therefore...
if (highScore < studentZero.q1)
    highScore = studentZero.q1;

Alternatively, you can add a getter for q1, and use it.

int score = student[i].getQ1()
if (highScore < score)
    highScore = score;

See here for an example of how to access a member of an object in an array:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.