Write a public Java class called Student that represents one student. The class
ID: 638869 • Letter: W
Question
Write a public Java class called Student that represents one student. The class should include:
? Instance variables ID (int), firstName (String), and GPA (double).
? A constructor with no parameters that sets the instance variables, respectively, to these values: -111 (not set) -2.22
? A constructor with three parameters that sets the instance variables to the parameter values.
? Getter methods for each instance variable.
? Setter methods for each instance variable.
? equals and toString methods.
Explanation / Answer
class Student {
private int ID;
private String firstName;
private double GPA;
public Student() {
this.ID=-111;
this.GPA=-2.22;
this.firstName="not set";
}
public Student(int ID,String firstName,double GPA) {
this.GPA=GPA;
this.ID=ID;
this.firstName=firstName;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
@Override
public String toString() {
return this.firstName+" having ID "+this.ID+" has scored "+this.GPA+" GPA";
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student student=(Student)obj;
return this.ID==student.getID()&&this.firstName.equals(student.getFirstName())&&Double.compare(this.GPA, student.getGPA())==0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.