Homework Solution: Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following methods: A ‘copy’ constructor,…

    In java Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following methods: A ‘copy’ constructor, A ‘clone’ method, A ‘finalize’ method, A ‘dispose’ method, and A ‘hashCode’ method. A ‘compareTo’ method Test the upgraded classes using the application ‘TestStudentName.java’ located in ‘Chapter01_Files.zip’. Be sure to include output messages in both the ‘finalize’ method and the ‘dispose’ method. Chapter01 File 'TestStudentName.java' below. // ----------------------------------------------- // TestStudentName.java // ----------------------------------------------- public class TestStudentName { public static void main ( String [ ] args ) { 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 ( "ttError - students should not be the same" ); System.out.println ( "tts1 = " + s1 ); System.out.println ( "tts1 = " + s2 ); } else { System.out.println ( "ttSuccess - students are not the same" ); System.out.println ( "tts1 = " + s1 ); System.out.println ( "tts1 = " + s2 ); } // ---- Test the clone method ------------ System.out.println ( "nnTest the 'clone' method ------------" ); Student s3 = (Student) s1.clone ( ); if ( s1.equals ( s3 ) ) System.out.println ( "ttSuccess - Students s1 and s3 are the same." ); else { System.out.println ( "ttError - Students s1 and s3 are not the same" ); System.out.println ( "tts1 = " + s1 ); System.out.println ( "tts3 = " + s3 ); } s3.setStudent ( n2, "234567" ); if ( s1.equals ( s3 ) ) { System.out.println ( "ttError - students should not be the same" ); System.out.println ( "tts1 = " + s1 ); System.out.println ( "tts1 = " + s3 ); } else System.out.println ( "ttSuccess - students are not the same" ); // ---- Test the finalize method --------- System.out.println ( "nnTest the 'finalize' method ------------" ); s1 = null; System.gc(); System.out.println ( "ttShould see the 'finalize' message ------------" ); // ---- Test the dispose method ---------- System.out.println ( "nnTest the 'dispose' method ------------" ); s2.dispose(); System.out.println ( "ttShould see the 'dispose' message ------------" ); s2 = null; // ---- Test the hashCode method --------- s1 = new Student ( s3 ); System.out.println ( "nnTest the 'hashCode' method ------------" ); if ( s1.hashCode ( ) == s3.hashCode ( ) ) System.out.println ( "ttSuccess - hashCode for s1 and s3 are the same." ); else { System.out.println ( "ttError - hashCode for s1 and s3 are not the same." ); System.out.println ( "tts1.hashCode = " + s1.hashCode() ); System.out.println ( "tts3.hashCode = " + s3.hashCode() ); } System.out.println ( ); } }

    Expert Answer

     
    TestingStudentName.java

    In java

    Problem: Using the ‘Name.java’ and ‘Student.java’ refines, referableorious twain arrangees to enclose the subjoined systems:

    A ‘copy’ producer,

    A ‘clone’ system,

    A ‘finalize’ system,

    A ‘dispose’ system, and

    A ‘hashCode’ system.

    A ‘compareTo’ system

    Test the upgraded arrangees using the collision ‘TestStudentName.java’ located in ‘Chapter01_Files.zip’. Be believing to enclose extinguishedput communications in twain the ‘finalize’ system and the ‘dispose’ system.

    Chapter01 Refine ‘TestStudentName.java’ adown.

    // ———————————————–

    // TestStudentName.java

    // ———————————————–

    public arrange TestStudentName

    {

    public static destitute ocean ( String [ ] args )

    {

    Name n1 = strange Name ( “Ty”, “Cobb” );

    Name n2 = strange Name ( “Babe”, “Ruth” );

    // —- Test the representation producer ——–

    System.out.println ( “Test the representation producer ————” );

    Student s1 = strange Student ( n1, “123456” );

    Student s2 = strange Student ( s1 );

    s2.setStudent ( n2, “234567” );

    if ( s1.equals ( s2 ) )

    {

    System.out.println ( “ttError – students should referable attributable attributable attributable attributable attributable attributable be the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts1 = ” + s2 );

    }

    else

    {

    System.out.println ( “ttSuccess – students are referable attributable attributable attributable attributable attributable attributable the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts1 = ” + s2 );

    }

    // —- Test the clone system ————

    System.out.println ( “nnTest the ‘clone’ system ————” );

    Student s3 = (Student) s1.clone ( );

    if ( s1.equals ( s3 ) )

    System.out.println ( “ttSuccess – Students s1 and s3 are the identical.” );

    else

    {

    System.out.println ( “ttError – Students s1 and s3 are referable attributable attributable attributable attributable attributable attributable the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts3 = ” + s3 );

    }

    s3.setStudent ( n2, “234567” );

    if ( s1.equals ( s3 ) )

    {

    System.out.println ( “ttError – students should referable attributable attributable attributable attributable attributable attributable be the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts1 = ” + s3 );

    }

    else

    System.out.println ( “ttSuccess – students are referable attributable attributable attributable attributable attributable attributable the identical” );

    // —- Test the finalize system ———

    System.out.println ( “nnTest the ‘finalize’ system ————” );

    s1 = null;

    System.gc();

    System.out.println ( “ttShould comprehend the ‘finalize’ communication ————” );

    // —- Test the manage system ———-

    System.out.println ( “nnTest the ‘dispose’ system ————” );

    s2.dispose();

    System.out.println ( “ttShould comprehend the ‘dispose’ communication ————” );

    s2 = null;

    // —- Test the hashCode system ———

    s1 = strange Student ( s3 );

    System.out.println ( “nnTest the ‘hashCode’ system ————” );

    if ( s1.hashCode ( ) == s3.hashCode ( ) )

    System.out.println ( “ttSuccess – hashCode coercion s1 and s3 are the identical.” );

    else

    {

    System.out.println ( “ttError – hashCode coercion s1 and s3 are referable attributable attributable attributable attributable attributable attributable the identical.” );

    System.out.println ( “tts1.hashCode = ” + s1.hashCode() );

    System.out.println ( “tts3.hashCode = ” + s3.hashCode() );

    }

    System.out.println ( );

    }

    }

    Expert Vindication

     

    TestingStudentName.java

    /*

    * To vary this permit header, pick-extinguished Permit Headers in Project Properties.

    * To vary this template refine, pick-extinguished Tools | Templates

    * and referableorious the template in the editor.

    */

    package testingstudentname;

    public arrange TestingStudentName

    {

    public static destitute ocean ( String [ ] args ) throws CloneNotSupportedException

    {

    Name n1 = strange Name ( “Ty”, “Cobb” );

    Name n2 = strange Name ( “Babe”, “Ruth” );

    // —- Test the representation producer ——–

    System.out.println ( “Test the representation producer ————” );

    Student s1 = strange Student ( n1, “123456” );

    Student s2 = strange Student ( s1 );

    s2.setStudent ( n2, “234567” );

    if ( s1.equals ( s2 ) )

    {

    System.out.println ( “ttError – students should referable attributable attributable attributable attributable attributable attributable be the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts1 = ” + s2 );

    }

    else

    {

    System.out.println ( “ttSuccess – students are referable attributable attributable attributable attributable attributable attributable the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts1 = ” + s2 );

    }

    // —- Test the clone system ————

    System.out.println ( “nnTest the ‘clone’ system ————” );

    Student s3 = (Student) s1.clone();

    if ( s1.equals ( s3 ) )

    System.out.println ( “ttSuccess – Students s1 and s3 are the identical.” );

    else

    {

    System.out.println ( “ttError – Students s1 and s3 are referable attributable attributable attributable attributable attributable attributable the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts3 = ” + s3 );

    }

    s3.setStudent ( n2, “234567” );

    if ( s1.equals ( s3 ) )

    {

    System.out.println ( “ttError – students should referable attributable attributable attributable attributable attributable attributable be the identical” );

    System.out.println ( “tts1 = ” + s1 );

    System.out.println ( “tts1 = ” + s3 );

    }

    else

    System.out.println ( “ttSuccess – students are referable attributable attributable attributable attributable attributable attributable the identical” );

    // —- Test the finalize system ———

    System.out.println ( “nnTest the ‘finalize’ system ————” );

    s1 = null;

    System.gc();

    System.out.println ( “ttShould comprehend the ‘finalize’ communication ————” );

    // —- Test the manage system ———-

    System.out.println ( “nnTest the ‘dispose’ system ————” );

    s2.dispose();

    System.out.println ( “ttShould comprehend the ‘dispose’ communication ————” );

    s2 = null;

    // —- Test the hashCode system ———

    s1 = strange Student ( s3 );

    System.out.println ( “nnTest the ‘hashCode’ system ————” );

    if ( s1.hashCode ( ) == s3.hashCode ( ) )

    System.out.println ( “ttSuccess – hashCode coercion s1 and s3 are the identical.” );

    else

    {

    System.out.println ( “ttError – hashCode coercion s1 and s3 are referable attributable attributable attributable attributable attributable attributable the identical.” );

    System.out.println ( “tts1.hashCode = ” + s1.hashCode() );

    System.out.println ( “tts3.hashCode = ” + s3.hashCode() );

    }

    System.out.println ( );

    }

    }

    Name.java

    /*

    * To vary this permit header, pick-extinguished Permit Headers in Project Properties.

    * To vary this template refine, pick-extinguished Tools | Templates

    * and referableorious the template in the editor.

    */

    package testingstudentname;

    /**

    arrange Name {

    private String ty;

    private String cobb;

    Name(String ty, String cobb) {

    this.ty =ty;

    this.cobb =cobb;

    }

     

    }

    Student.java

    /*

    * To vary this permit header, pick-extinguished Permit Headers in Project Properties.

    * To vary this template refine, pick-extinguished Tools | Templates

    * and referableorious the template in the editor.

    */

    package testingstudentname;

    /**

    arrange Student implements Cloneable {

    private Name n1;

    private String string;

    private Name n2;

    Student(Student s3) {

    }

    Student(Name n1, String string) {

    this.n1 = n1;

    this.string = string;

    }

    destitute setStudent(Name n2, String string) {

    this.n2 =n2;

    this.string=string;

    }

    destitute manage() {

    }

    public Object clone() throws CloneNotSupportedException

    {

    return super.clone();

    }

    }

    Output: