// ------------------------------------------------------------ // Name.java //
ID: 3881057 • Letter: #
Question
// ------------------------------------------------------------
// 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 );
}
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.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 { }
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.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 ( " Error - students should not be the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s1 = " + s2 );
}
else
{
System.out.println ( " Success - students are not the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s1 = " + s2 );
}
// ---- Test the clone method ------------
System.out.println ( " Test the 'clone' method ------------" );
Student s3 = (Student) s1.clone ( );
if ( s1.equals ( s3 ) )
System.out.println ( " Success - Students s1 and s3 are the same." );
else
{
System.out.println ( " Error - Students s1 and s3 are not the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s3 = " + s3 );
}
s3.setStudent ( n2, "234567" );
if ( s1.equals ( s3 ) )
{
System.out.println ( " Error - students should not be the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s1 = " + s3 );
}
else
System.out.println ( " Success - students are not the same" );
// ---- Test the finalize method ---------
System.out.println ( " Test the 'finalize' method ------------" );
s1 = null;
System.gc();
System.out.println ( " Should see the 'finalize' message ------------" );
// ---- Test the dispose method ----------
System.out.println ( " Test the 'dispose' method ------------" );
s2.dispose();
System.out.println ( " Should 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 ( " Success - hashCode for s1 and s3 are the same." );
else
{
System.out.println ( " Error - hashCode for s1 and s3 are not the same." );
System.out.println ( " s1.hashCode = " + s1.hashCode() );
System.out.println ( " s3.hashCode = " + s3.hashCode() );
}
System.out.println ( );
}
}
Lab #1-Designing Classes-Software Engineering NAME Problem: Using the ‘Namejava, 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. Requirements: You will submit the following with this lab: A copy of the source code and related project files will be placed in a directory and Zip it and post. NOTE Each program should have a section of comments at the top Be sure the comment section includes your name, class and section, lab number, and the date the lab was completed. This section should also provide any relevant information on the program. NOTE 2: ll code must conform to generally accepted coding standards including the naming of variables, classes, and any constants. The code should also be appropriately indented to insure readability.Explanation / Answer
Hiii... Please check the code.
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 );
}
public boolean equals ( Object o ) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */
if (!(o instanceof Name)) {
return false;
}
return false;
} // Equals method
public String toString( )
{
return first + " " + last;
}
public void finalize ( ) throws Throwable {
// garbage collection
super.finalize();
}
public void dispose ( ) {
System.out.println("dispose method in java swings");
} // dispose method
public int hashCode ( ) {
System.out.println("In hashcode");
int hashcode = 0;
hashcode = first.hashCode();
hashcode += last.hashCode();
return hashcode;
} // hashCode method
public int compareTo ( ) {
return 0; } // compareTo Method
} // end Name
Student.java
public class Student implements Cloneable
{
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() 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; }
public String toString()
{
return id + " " + fullName.toString ( );
}
public boolean equals ( Object o ) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */
if (!(o instanceof Student)) {
return false;
}
return false;
}
public void finalize() throws Throwable {
// garbage collection
super.finalize();
}
public void dispose() {
System.out.println("dispose method in java swings");
}
public int hashCode() {
System.out.println("In hashcode");
int hashcode = 0;
hashcode = fullName.hashCode();
hashcode += id.hashCode();
return hashcode;
}
public int compareTo ( Student sd) {
return (Integer.parseInt(this.id) < Integer.parseInt(sd.id )) ? -1: (Integer.parseInt(this.id) < Integer.parseInt(sd.id)) ? 1:0 ;
}
} // end Student
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 ( " Error - students should not be the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s1 = " + s2 );
}
else
{
System.out.println ( " Success - students are not the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s1 = " + s2 );
}
// ---- Test the clone method ------------
System.out.println ( " Test the 'clone' method ------------" );
Student s3 = (Student) s1.clone ( );
if ( s1.equals ( s3 ) )
System.out.println ( " Success - Students s1 and s3 are the same." );
else
{
System.out.println ( " Error - Students s1 and s3 are not the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s3 = " + s3 );
}
s3.setStudent ( n2, "234567" );
if ( s1.equals ( s3 ) )
{
System.out.println ( " Error - students should not be the same" );
System.out.println ( " s1 = " + s1 );
System.out.println ( " s1 = " + s3 );
}
else
System.out.println ( " Success - students are not the same" );
// ---- Test the finalize method ---------
System.out.println ( " Test the 'finalize' method ------------" );
s1 = null;
System.gc();
System.out.println ( " Should see the 'finalize' message ------------" );
// ---- Test the dispose method ----------
System.out.println ( " Test the 'dispose' method ------------" );
s2.dispose();
System.out.println ( " Should 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 ( " Success - hashCode for s1 and s3 are the same." );
else
{
System.out.println ( " Error - hashCode for s1 and s3 are not the same." );
System.out.println ( " s1.hashCode = " + s1.hashCode() );
System.out.println ( " s3.hashCode = " + s3.hashCode() );
}
System.out.println ( );
}
}
Please check the code and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.