I need to write a class that encapsulates this program. It should supplement the
ID: 3773939 • Letter: I
Question
I need to write a class that encapsulates this program. It should supplement the constructor, accessors, and mutators. As well as methods like toString and equals.
public class StudentClient
{
public static void main( String [] args )
{
Student s1 = new Student( "Jones", "222-88-1111", 3.7 );
Student s2 = new Student( "Smith", "333-99-4444", 3.2 );
System.out.println( "The name of student #1 is " + s1.getName( ) );
System.out.println( "The social security number of student #1 is " + s1.getSsn( ) );
System.out.println( "The gpa of student #1 is " + s1.getGpa( ) );
System.out.println( "Student #2 is " + s2.toString( ) );
if ( s1.equals( s2 ) )
System.out.println( "Original students #1 and #2 are identical" );
else
System.out.println( "Original students #1 and #2 are different" );
s2.setName( "Jones" );
s2.setSsn( "222-88-1111" );
s2.setGpa( 3.7 );
if ( s1.equals( s2 ) )
System.out.println( "Original student #1 and modified student #2 are identical" );
else
System.out.println( "Original student #1 and modified student #2 are different" );
}
}
Explanation / Answer
Student.java
public class Student {
private String name;
private String ssn;
private double gpa;
public Student(String n, String s, double g){
name = n;
gpa = g;
ssn = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public String toString(){
return "name is "+name+" ssn is "+ssn+" gpa is "+gpa;
}
public boolean equals(Student s){
if(this.name == s.name && this.ssn == s.ssn){
return true;
}
else{
return false;
}
}
}
StudentClient.java
public class StudentClient
{
public static void main( String [] args )
{
Student s1 = new Student( "Jones", "222-88-1111", 3.7 );
Student s2 = new Student( "Smith", "333-99-4444", 3.2 );
System.out.println( "The name of student #1 is " + s1.getName( ) );
System.out.println( "The social security number of student #1 is " + s1.getSsn( ) );
System.out.println( "The gpa of student #1 is " + s1.getGpa( ) );
System.out.println( "Student #2 is " + s2.toString( ) );
if ( s1.equals( s2 ) )
System.out.println( "Original students #1 and #2 are identical" );
else
System.out.println( "Original students #1 and #2 are different" );
s2.setName( "Jones" );
s2.setSsn( "222-88-1111" );
s2.setGpa( 3.7 );
if ( s1.equals( s2 ) )
System.out.println( "Original student #1 and modified student #2 are identical" );
else
System.out.println( "Original student #1 and modified student #2 are different" );
}
}
Output:
The name of student #1 is Jones
The social security number of student #1 is 222-88-1111
The gpa of student #1 is 3.7
Student #2 is name is Smith ssn is 333-99-4444 gpa is 3.2
Original students #1 and #2 are different
Original student #1 and modified student #2 are identical