Question: 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

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 ( "\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 );

}

// ---- 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 ( );

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!