Studentjava public class Student private Name fullName

// ----------------------------------------------------------
//                    Student.java
// ----------------------------------------------------------

public class Student
{
   private Name fullName;
   private String id;

   public Student()
   {
       this ( new Name ( ), \"\" );
   }

   public Student(Name studentName, String studentId)
   {
       fullName = studentName;
       id = studentId;
   }

   public Student(Student obj) throws NullPointerException   { }


    @Override
            public Object clone() throws CloneNotSupportedException
            {
            
             //   return super.clone();
            }

   public void setStudent(Name studentName, String studentId)
   {
       fullName = studentName;
       id       = studentId;
   }

   public void   setName(Name studentName) {   fullName = studentName;   }
   public Name   getName()                 {   return fullName;        }
   public void   setId(String studentId)   {   id = studentId;         }
   public String getId()                   {   return id;              }


        @Override
   public String toString()
   {
       return id + \" \" + fullName.toString ( );
   }

  
    @Override
   public boolean equals ( Object obj )
        {
        return false;
        }
        @Override
   public void finalize()
        {
            System.out.println(\"Finalizing the student object\");
       System.out.println(this);
        }

   public void dispose()
        {
            System.out.println(\"Disposing the student object\");
       System.out.println(this);
        }

   public int hashCode()
        {
            return super.hashCode();
        }

   public int compareTo (Student studentObj )
        {
            return studentObj.getName().compareTo(this.getName());
        }

} // end Student

// ------------------------------------------------------------
//                Name.java
// ------------------------------------------------------------

public class Name
{
   private String first; // first name
   private String last;   // last name

   public Name()
   {
       this ( \"\", \"\" );
   }

   public Name(String firstName, String lastName)
   {
       first = firstName;
       last = lastName;
   }

   public void setName(String firstName, String lastName)
   {
       first = firstName;
       last = lastName;
   }

   public Name(Name obj) throws NullPointerException { } // Copy Constructor

   public void   setFirst(String firstName) { first = firstName; }
   public void   setLast (String lastName ) { last = lastName;    }
   public String getFirst()                 { return first;       }
   public String getLast ()                 { return last;         }
   public String getName ()                 { return toString(); }
   public void   giveLastNameTo ( Name aName )
   {
       aName.setLast ( last );
   }

        @Override
   public boolean equals ( Object obj )
        {
            //return obj.equals(first);
          
        }               // Equals method

   public String toString( )
   {
       return first + \" \" + last;
   }

        @Override
   public void finalize ( )
        {
       System.out.println(\"Finalizing the name object\");
       System.out.println(this);
        }                           // finalize method

   public void dispose ( )
        {
          System.out.println(\"Disposing the name object\");
       System.out.println(this);
        }                           // dispose method

   public int hashCode ( )
        {
             return super.hashCode();
        }                           // hashCode method

   public int compareTo (Name nameObj)
        {
            return nameObj.getName().compareTo(this.getName());
        }
        @Override
        public Object clone() throws CloneNotSupportedException
        {
              Name obj = new Name(this.first, this.last);
        return obj;
        }

} // end Name

// -----------------------------------------------
//                    TestStudentName.java
// -----------------------------------------------

public class TestStudentName
{
   public static void main ( String [ ] args ) throws CloneNotSupportedException
   {
       Name n1 = new Name ( \"Ty\",   \"Cobb\" );
       Name n2 = new Name ( \"Babe\", \"Ruth\" );

   // ---- Test the copy constructor --------
       System.out.println ( \"Test the copy constructor ------------\" );
       Student s1 = new Student ( n1, \"123456\" );
       Student s2 = new Student ( s1 );

       s2.setStudent ( n2, \"234567\" );
       if ( s1.equals ( s2 ) )
       {
           System.out.println ( \"\\t\\tError - students should not be the same\" );
           System.out.println ( \"\\t\\ts1 = \" + s1 );
           System.out.println ( \"\\t\\ts1 = \" + s2 );
       }
       else
       {
           System.out.println ( \"\\t\\tSuccess - students are not the same\" );
           System.out.println ( \"\\t\\ts1 = \" + s1 );
           System.out.println ( \"\\t\\ts1 = \" + s2 );
       }
                //
                System.out.println(\"student1\");
       System.out.println(s1);
       System.out.println(\"student2\");
       System.out.println(s2);
              
                //

   // ---- Test the clone method ------------
       System.out.println ( \"\ \ Test the \'clone\' method ------------\" );
                           Student s3 = (Student) s1.clone ();
              

       if ( s1.equals ( s3 ) )
           System.out.println ( \"\\t\\tSuccess - Students s1 and s3 are the same.\" );
       else
       {
           System.out.println ( \"\\t\\tError - Students s1 and s3 are not the same\" );
           System.out.println ( \"\\t\\ts1 = \" + s1 );
           System.out.println ( \"\\t\\ts3 = \" + s3 );
       }

       s3.setStudent ( n2, \"234567\" );
       if ( s1.equals ( s3 ) )
       {
           System.out.println ( \"\\t\\tError - students should not be the same\" );
           System.out.println ( \"\\t\\ts1 = \" + s1 );
           System.out.println ( \"\\t\\ts1 = \" + s3 );
       }
       else
           System.out.println ( \"\\t\\tSuccess - students are not the same\" );

   // ---- Test the finalize method ---------
       System.out.println ( \"\ \ Test the \'finalize\' method ------------\" );
       s1 = null;
       System.gc();
       System.out.println ( \"\\t\\tShould see the \'finalize\' message ------------\" );

   // ---- Test the dispose method ----------
       System.out.println ( \"\ \ Test the \'dispose\' method ------------\" );
       s2.dispose();
       System.out.println ( \"\\t\\tShould see the \'dispose\' message ------------\" );
       s2 = null;


   // ---- Test the hashCode method ---------
       s1 = new Student ( s3 );
       System.out.println ( \"\ \ Test the \'hashCode\' method ------------\" );
       if ( s1.hashCode ( ) == s3.hashCode ( ) )
           System.out.println ( \"\\t\\tSuccess - hashCode for s1 and s3 are the same.\" );
       else
       {
           System.out.println ( \"\\t\\tError - hashCode for s1 and s3 are not the same.\" );
           System.out.println ( \"\\t\\ts1.hashCode = \" + s1.hashCode() );
           System.out.println ( \"\\t\\ts3.hashCode = \" + s3.hashCode() );
       }

       System.out.println ( );
   }
}


// Someone code me equals and clone method that work with tester file would be great. Post only those method code.

Solution

Hi,

Please see below the updated classes. Please comment for any queries/feedbacks.

Thanks,

Anita Joseph

Student.java

// ----------------------------------------------------------
// Student.java
// ----------------------------------------------------------
public class Student
{
private Name fullName;
private String id;
public Student()
{
this ( new Name ( ), \"\" );
}
public Student(Name studentName, String studentId)
{
fullName = studentName;
id = studentId;
}
public Student(Student obj) throws NullPointerException {
   this.fullName = obj.fullName;
   this.id = obj.id;
}

@Override
public Object clone() throws CloneNotSupportedException
{
           Student obj = new Student((Name)this.getName().clone(), this.id);
       return obj;
      
}
public void setStudent(Name studentName, String studentId)
{
fullName = studentName;
id = studentId;
}
public void setName(Name studentName) { fullName = studentName; }
public Name getName() { return fullName; }
public void setId(String studentId) { id = studentId; }
public String getId() { return id; }

@Override
public String toString()
{
return id + \" \" + fullName.toString ( );
}
  
@Override
public boolean equals ( Object obj )
{
   Student st = (Student) obj;
   if(this.getName().equals(st.getName()) ){
       return true;
   }
return false;
}
@Override
protected void finalize()
{
System.out.println(\"Finalizing the student object\");
System.out.println(this);
}
public void dispose()
{
System.out.println(\"Disposing the student object\");
System.out.println(this);
}
public int hashCode()
{
       String studentId = this.id;
return Integer.valueOf(studentId);
}
public int compareTo (Student studentObj )
{
return studentObj.getName().compareTo(this.getName());
}
} // end Student

Name.java

// ------------------------------------------------------------
// Name.java
// ------------------------------------------------------------
public class Name
{
private String first; // first name
private String last; // last name
public Name()
{
this ( \"\", \"\" );
}
public Name(String firstName, String lastName)
{
first = firstName;
last = lastName;
}
public void setName(String firstName, String lastName)
{
first = firstName;
last = lastName;
}
public Name(Name obj) throws NullPointerException { } // Copy Constructor
public void setFirst(String firstName) { first = firstName; }
public void setLast (String lastName ) { last = lastName; }
public String getFirst() { return first; }
public String getLast () { return last; }
public String getName () { return toString(); }
public void giveLastNameTo ( Name aName )
{
aName.setLast ( last );
}
@Override
public boolean equals ( Object obj )
{  
   Name name = (Name) obj;
   if(this.getFirst().equalsIgnoreCase(name.getFirst())){
       return true;
   }
   return false;
  
  
} // Equals method
public String toString( )
{
return first + \" \" + last;
}
@Override
public void finalize ( )
{
System.out.println(\"Finalizing the name object\");
System.out.println(this);
} // finalize method
public void dispose ( )
{
System.out.println(\"Disposing the name object\");
System.out.println(this);
} // dispose method
public int hashCode ( )
{
return super.hashCode();
} // hashCode method
public int compareTo (Name nameObj)
{
return nameObj.getName().compareTo(this.getName());
}
@Override
public Object clone() throws CloneNotSupportedException
{
Name obj = new Name(this.first, this.last);
return obj;
}
} // end Name

TestStudentName.java

// -----------------------------------------------
// TestStudentName.java
// -----------------------------------------------
public class TestStudentName
{
public static void main ( String [ ] args ) throws CloneNotSupportedException
{
Name n1 = new Name ( \"Ty\", \"Cobb\" );
Name n2 = new Name ( \"Babe\", \"Ruth\" );
// ---- Test the copy constructor --------
System.out.println ( \"Test the copy constructor ------------\" );
Student s1 = new Student ( n1, \"123456\" );
Student s2 = new Student ( s1 );
s2.setStudent ( n2, \"234567\" );
if ( s1.equals ( s2 ) )
{
System.out.println ( \"\\t\\tError - students should not be the same\" );
System.out.println ( \"\\t\\ts1 = \" + s1 );
System.out.println ( \"\\t\\ts1 = \" + s2 );
}
else
{
System.out.println ( \"\\t\\tSuccess - students are not the same\" );
System.out.println ( \"\\t\\ts1 = \" + s1 );
System.out.println ( \"\\t\\ts1 = \" + s2 );
}
//
System.out.println(\"student1\");
System.out.println(s1);
System.out.println(\"student2\");
System.out.println(s2);
  
//
// ---- Test the clone method ------------
System.out.println ( \"\ \ Test the \'clone\' method ------------\" );
Student s3 = (Student) s1.clone ();
  
if ( s1.equals ( s3 ) )
System.out.println ( \"\\t\\tSuccess - Students s1 and s3 are the same.\" );
else
{
System.out.println ( \"\\t\\tError - Students s1 and s3 are not the same\" );
System.out.println ( \"\\t\\ts1 = \" + s1 );
System.out.println ( \"\\t\\ts3 = \" + s3 );
}
s3.setStudent ( n2, \"234567\" );
if ( s1.equals ( s3 ) )
{
System.out.println ( \"\\t\\tError - students should not be the same\" );
System.out.println ( \"\\t\\ts1 = \" + s1 );
System.out.println ( \"\\t\\ts1 = \" + s3 );
}
else
System.out.println ( \"\\t\\tSuccess - students are not the same\" );
// ---- Test the finalize method ---------
System.out.println ( \"\ \ Test the \'finalize\' method ------------\" );
s1 = null;
System.gc();
System.out.println ( \"\\t\\tShould see the \'finalize\' message ------------\" );
// ---- Test the dispose method ----------
System.out.println ( \"\ \ Test the \'dispose\' method ------------\" );
s2.dispose();
System.out.println ( \"\\t\\tShould see the \'dispose\' message ------------\" );
s2 = null;

// ---- Test the hashCode method ---------
s1 = new Student ( s3 );
System.out.println ( \"\ \ Test the \'hashCode\' method ------------\" );
if ( s1.hashCode ( ) == s3.hashCode ( ) )
System.out.println ( \"\\t\\tSuccess - hashCode for s1 and s3 are the same.\" );
else
{
System.out.println ( \"\\t\\tError - hashCode for s1 and s3 are not the same.\" );
System.out.println ( \"\\t\\ts1.hashCode = \" + s1.hashCode() );
System.out.println ( \"\\t\\ts3.hashCode = \" + s3.hashCode() );
}
System.out.println ( );
}
}

SAmple output:

Test the copy constructor ------------
       Success - students are not the same
       s1 = 123456 Ty Cobb
       s1 = 234567 Babe Ruth
student1
123456 Ty Cobb
student2
234567 Babe Ruth


Test the \'clone\' method ------------
       Success - Students s1 and s3 are the same.
       Success - students are not the same


Test the \'finalize\' method ------------
       Should see the \'finalize\' message ------------


Test the \'dispose\' method ------------
Disposing the student object
234567 Babe Ruth
       Should see the \'dispose\' message ------------
Finalizing the name object
Ty Cobb
Finalizing the student object
123456 Ty Cobb
Finalizing the name object
Ty Cobb


Test the \'hashCode\' method ------------
       Success - hashCode for s1 and s3 are the same.

// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student
// ---------------------------------------------------------- // Student.java // ---------------------------------------------------------- public class Student

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site