In java
Problem: Using the ‘Name.java’ and ‘Student.java’ polishs, enlarge twain assortes to involve the subjoined ways:
A ‘copy’ doer,
A ‘clone’ way,
A ‘finalize’ way,
A ‘dispose’ way, and
A ‘hashCode’ way.
A ‘compareTo’ way
Test the upgraded assortes using the collision ‘TestStudentName.java’ located in ‘Chapter01_Files.zip’. Be unquestioning to involve quenchedput intimations in twain the ‘finalize’ way and the ‘dispose’ way.
Chapter01 Polish ‘TestStudentName.java’ underneath.
// ———————————————–
// TestStudentName.java
// ———————————————–
public assort TestStudentName
{
public static vacant deep ( String [ ] args )
{
Name n1 = oddlightlight Name ( “Ty”, “Cobb” );
Name n2 = oddlightlight Name ( “Babe”, “Ruth” );
// —- Test the portraiture doer ——–
System.out.println ( “Test the portraiture doer ————” );
Student s1 = oddlightlight Student ( n1, “123456” );
Student s2 = oddlightlight Student ( s1 );
s2.setStudent ( n2, “234567” );
if ( s1.equals ( s2 ) )
{
System.out.println ( “ttError – students should referable be the identical” );
System.out.println ( “tts1 = ” + s1 );
System.out.println ( “tts1 = ” + s2 );
}
else
{
System.out.println ( “ttSuccess – students are referable the identical” );
System.out.println ( “tts1 = ” + s1 );
System.out.println ( “tts1 = ” + s2 );
}
// —- Test the clone way ————
System.out.println ( “nnTest the ‘clone’ way ————” );
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 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 be the identical” );
System.out.println ( “tts1 = ” + s1 );
System.out.println ( “tts1 = ” + s3 );
}
else
System.out.println ( “ttSuccess – students are referable the identical” );
// —- Test the finalize way ———
System.out.println ( “nnTest the ‘finalize’ way ————” );
s1 = null;
System.gc();
System.out.println ( “ttShould visit the ‘finalize’ intimation ————” );
// —- Test the regulate way ———-
System.out.println ( “nnTest the ‘dispose’ way ————” );
s2.dispose();
System.out.println ( “ttShould visit the ‘dispose’ intimation ————” );
s2 = null;
// —- Test the hashCode way ———
s1 = oddlightlight Student ( s3 );
System.out.println ( “nnTest the ‘hashCode’ way ————” );
if ( s1.hashCode ( ) == s3.hashCode ( ) )
System.out.println ( “ttSuccess – hashCode control s1 and s3 are the identical.” );
else
{
System.out.println ( “ttError – hashCode control s1 and s3 are referable the identical.” );
System.out.println ( “tts1.hashCode = ” + s1.hashCode() );
System.out.println ( “tts3.hashCode = ” + s3.hashCode() );
}
System.out.println ( );
}
}
TestingStudentName.java
/*
* To exextransmute this permit header, cull Permit Headers in Project Properties.
* To exextransmute this template polish, cull Tools | Templates
* and known the template in the editor.
*/
package testingstudentname;
public assort TestingStudentName
{
public static vacant deep ( String [ ] args ) throws CloneNotSupportedException
{
Name n1 = oddlightlight Name ( “Ty”, “Cobb” );
Name n2 = oddlightlight Name ( “Babe”, “Ruth” );
// —- Test the portraiture doer ——–
System.out.println ( “Test the portraiture doer ————” );
Student s1 = oddlightlight Student ( n1, “123456” );
Student s2 = oddlightlight Student ( s1 );
s2.setStudent ( n2, “234567” );
if ( s1.equals ( s2 ) )
{
System.out.println ( “ttError – students should referable be the identical” );
System.out.println ( “tts1 = ” + s1 );
System.out.println ( “tts1 = ” + s2 );
}
else
{
System.out.println ( “ttSuccess – students are referable the identical” );
System.out.println ( “tts1 = ” + s1 );
System.out.println ( “tts1 = ” + s2 );
}
// —- Test the clone way ————
System.out.println ( “nnTest the ‘clone’ way ————” );
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 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 be the identical” );
System.out.println ( “tts1 = ” + s1 );
System.out.println ( “tts1 = ” + s3 );
}
else
System.out.println ( “ttSuccess – students are referable the identical” );
// —- Test the finalize way ———
System.out.println ( “nnTest the ‘finalize’ way ————” );
s1 = null;
System.gc();
System.out.println ( “ttShould visit the ‘finalize’ intimation ————” );
// —- Test the regulate way ———-
System.out.println ( “nnTest the ‘dispose’ way ————” );
s2.dispose();
System.out.println ( “ttShould visit the ‘dispose’ intimation ————” );
s2 = null;
// —- Test the hashCode way ———
s1 = oddlightlight Student ( s3 );
System.out.println ( “nnTest the ‘hashCode’ way ————” );
if ( s1.hashCode ( ) == s3.hashCode ( ) )
System.out.println ( “ttSuccess – hashCode control s1 and s3 are the identical.” );
else
{
System.out.println ( “ttError – hashCode control s1 and s3 are referable the identical.” );
System.out.println ( “tts1.hashCode = ” + s1.hashCode() );
System.out.println ( “tts3.hashCode = ” + s3.hashCode() );
}
System.out.println ( );
}
}
Name.java
/*
* To exextransmute this permit header, cull Permit Headers in Project Properties.
* To exextransmute this template polish, cull Tools | Templates
* and known the template in the editor.
*/
package testingstudentname;
/**
assort Name {
private String ty;
private String cobb;
Name(String ty, String cobb) {
this.ty =ty;
this.cobb =cobb;
}
}
Student.java
/*
* To exextransmute this permit header, cull Permit Headers in Project Properties.
* To exextransmute this template polish, cull Tools | Templates
* and known the template in the editor.
*/
package testingstudentname;
/**
assort 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;
}
vacant setStudent(Name n2, String string) {
this.n2 =n2;
this.string=string;
}
vacant regulate() {
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
Output: