An object's methods are meant to operate on one single instance of that object. Unless I've misunderstood the problem, you want to sort the array of objects based on one of the values inside the object, and you're not actually changing the individual objects at all just the array...so it doesn't belong inside the object itself. The quiz sorting method doesn't belong inside the object for the same reason that the name sorting one doesn't.
Now, if you needed a method which sorted an individual student's quizzes by the score he received on them, it would go inside the Student object. You see what I'm getting at?
I'm not sure what you mean by q[x] isn't a legal identifier though. If you're referring to the part where I said it's printing 10 times because of this instead of x[q], here's what I'm talking about (in the sorting() method of Student):
Code:
for(int q=0; q<10; q++)
System.out.println(this.n+" "+this.temp2);
I'm not sure what the purpose of that loop is though, looks sort of like debugging code that should probably be removed.
I see what you tried to do with the temp2 property in the Student object, but it's not going to work. Reason being, Java lacks an eval() statement--it would work in something like JavaScript on a website but even though Java uses a VM to do its work, allowing for code that changes at runtime in a compiled environment would be a nightmare.
Unless I'm totally missing part of what the project is supposed to do, this is all I would say belongs in the Student class:
Code:
import javax.swing.*;
import java.util.Arrays;
class Student
{
String n="";
int q[6];
Student()
{
for(int i=0;i<6;i++)
q[i] = (int)(Math.Random()*101);
n = JOptionPane.showInputDialog("Enter the student's name:");
}
public void printout()
{
System.out.println("------------------------------------------------"+
"\nInformation for Student: "+this.n);
for(int i=0;i<6;i++)
System.out.println("\nTest " + i+1 + " Score: " + this.q[i]);
System.out.println("\n------------------------------------------------");
}
}
When you want to access a particular test score for comparison, using the choice variable that they enter, it'd be something like
if (srecords[k].q[choice-1] > srecords[k+1].q[choice-1]) ... etc.
I agree with you that this does indeed make for a rather simplistic use of an object, but I can only assume that's because it's an introductory class.
PS-Using variables like n and q rather than, say, strName and intQuiz makes baby jesus cry.
Edit: minor typo in printout()