简体   繁体   中英

public static boolean Equals Method

I have to objects. Student and Course (both of which have this static equals method) I also have the driver which my professor wrote and I have been told not to touch. My goal is to make two arraylists of courses taken by the student, and current courses the student is taking.

My professor gave me all of the methods, I am supposed to fill in the bodies.

I double checked, and the equals method is copied exactly as he wants it to be.

This is a homework problem.

I need to fill in the body of this equals method. I have tried things like if(this.id == other.getID()) but I keep on receiving this error: error: non-static variable id cannot be referenced from a static context

I have tried using this.getID() instead but to no avail. I think this probably has something to do with this being static. (I am not allowed to change that.)

What is the correct way to go about writing this? I am not going to include all of the getters and setters in the below code.

public class Student {

private String id;
private String firstName;
private String lastName;
private String major;
private String minor;
private ArrayList<Course> coursesTaken;
private ArrayList<Course> currentSemesterCourses;
private double gpa;

/**
    Course constructor
*/
public Student(String id, String firstName, String lastName, String major, String minor, ArrayList<Course> coursesTaken, ArrayList<Course> currentSemesterCourses) {
/*Your code goes here */
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.major = major;
    this.minor = minor;
    this.coursesTaken = coursesTaken;
    this.currentSemesterCourses = currentSemesterCourses;
}


public static boolean Equals(Object obj) {
//Your code goes here   
//base it on id firstName, lastName, major, minor and gpa
}

I think this probably has something to do with this being static. (I am not allowed to change that.)

Then your assignment makes no sense whatsoever and there is no way to solve it.

If you are writing a static equals(Object) method, then you have no Student . There is only one object that got passed in, and it could be any type. "Equals" is a question you ask about two things, and you don't even have two things to compare, you have only one thing that might not even be a Student .

The method you've been asked to write is like asking "Is this object equals?" It makes exactly as little sense as that sentence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM