Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

//Name

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

}

public boolean equals ( Object obj ) { } // Equals method

public String toString( )

{

return first + " " + last;

}

public void finalize ( ) { } // finalize method

public void dispose ( ) { } // dispose method

public int hashCode ( ) { } // hashCode method

public int compareTo ( ) { } // compareTo Method

} // end Name

//Student

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 { }

public Object 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; }

public String toString()

{

return id + " " + fullName.toString ( );

}

public boolean equals ( Object obj ) { }

public void finalize() { }

public void dispose() { }

public int hashCode() { }

public int compareTo ( ) { }

} // end Student

// TestStudentName.

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

}

}

So i need to upgrade those two classes Name.java and Student.java and test it on TestStudentname.java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

0764532545, 978-0764532542

More Books

Students also viewed these Databases questions