In: Computer Science
Write a class encapsulating the concept of a student, assuming a student has the following attributes: a name, a Social Security number, and a GPA (for instance, 3.5). Include a constructor, the accessors and mutators, and methods toString and equals. Write a client class (test driver with main method) to test all the methods in your class (create 2 student objects, print the name, social security number and GPA of the 2 students, check if the two student’s GPA is the same or equal, change the name of the first student).
class Student
{
private String name;
private String SSN;
private double GPA;
//constructors
public Student(String name,String SSN,double
GPA)
{
this.name = name;
this.SSN = SSN;
this.GPA = GPA;
}
public Student()
{
name = " ";
GPA = 0.0;
SSN = " ";
}
//accessors and mutators
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setSSN(String SSN)
{
this.SSN = SSN;
}
public String getSSN()
{
return SSN;
}
public void setGPA(double GPA)
{
this.GPA = GPA;
}
public double getGPA()
{
return GPA;
}
public boolean equals(Student s)
{
if(this.name == s.name &&
this.SSN == s.SSN && this.GPA == s.GPA)
return true;
else
return false;
}
public String toString()
{
return "Student Name : "+name+" SSN
: "+SSN +" GPA : "+GPA;
}
}
class TestStudent
{
public static void main (String[] args)
{
Student s1 = new Student("James
Hawkings","668798809",3.77);
System.out.println(s1);
Student s2 = new Student();
s2.setName("Camy Lara");
s2.setSSN("768708099");
s2.setGPA(4.5);
System.out.println(s2);
System.out.println(s1.equals(s2));
}
}
Output:
Student Name : James Hawkings SSN : 668798809 GPA : 3.77 Student Name : Camy Lara SSN : 768708099 GPA : 4.5 false
Do ask if any doubt. Please upvote.