Answered Essay: IN JAVA (No copy and paste thank you)

IN JAVA (No copy and paste thank you)

***************************** Problem: Using the ‘Name.java’ and ‘Student.java’ filesexpand both classes to include the following methods: *****************************

‘copy’ constructor,

‘clone’ method,

‘finalize’ method,

‘dispose’ method, and

‘hashCode’ method.

‘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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Name.java File Code below~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// ————————————————————

// 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 File below~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// ———————————————————-

// 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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TestingStudent.java File 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

 

Name.java

// ———————————————————-

// Name.java

// ———————————————————-

public class Name

{

private String first; // first name

private String last; // last name

//default constructor

public Name()

{

this ( “”, “” );

}

//contructor with parameters

public Name(String firstName, String lastName)

{

first = firstName;

last = lastName;

}

//method to set the name

public void setName(String firstName, String lastName)

{

first = firstName;

last = lastName;

}

// Copy Constructor

public Name(Name obj) throws NullPointerException

{

this.first = obj.first;

this.last = obj.last;

}

//setter and getter methods

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

//method that set last name of one object to another

public void giveLastNameTo ( Name aName )

{

aName.setLast ( last );

}

//override equal method

public boolean equals (Object obj )

{

//if obj is equal to current object then return true, else return false

if(this.first.equals(((Name)(obj)).first) && this.last.equals(((Name)(obj)).last))

return true;

else

return false;

} // Equals method

//override toString method

public String toString( )

{

return first + ” ” + last;

}

// finalize method

public void finalize ( ) throws Throwable

{

super.finalize();

System.out.println(“nFinalise of Name class”);

}

// dispose method

public void dispose ( )

{

System.out.println(“nDispose of Name class”);

}

// hashCode method

public int hashCode ( )

{

return first.hashCode() + last.hashCode();

}

// compareTo Method

public int compareTo (Name nameObj)

{

return this.compareTo(nameObj);

}

} // end Name

Student.java

// ———————————————————-

// Student.java

// ———————————————————-

public class Student implements Cloneable

{

//instance variables

private Name fullName;

private String id;

//default constructor

public Student()

{

this ( new Name ( ), “” );

}

//contructor with parameters

public Student(Name studentName, String studentId)

{

fullName = studentName;

id = studentId;

}

//copy constructor

public Student(Student obj) throws NullPointerException

{

this.id = obj.id;

this.fullName = obj.fullName;

}

//cloning

public Object clone() throws CloneNotSupportedException

{

return super.clone();

}

public void setStudent(Name studentName, String studentId)

{

fullName = studentName;

id = studentId;

}

//getter and setter methods

public void setName(Name studentName) { fullName = studentName; }

public Name getName() { return fullName; }

public void setId(String studentId) { id = studentId; }

public String getId() { return id; }

//override toString method

public String toString()

{

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

}

//override equals method

public boolean equals(Object obj)

{

//check if obj is equal to current object, if yes return true else return false

if(this.id == ((Student)obj).id && this.fullName.equals(((Student)obj).fullName))

return true;

else

return false;

}

//finalize method()

public void finalize() throws Throwable

{

System.out.println(“nFinalise of Student class”);

super.finalize();

}

//dispose method

public void dispose()

{

System.out.print(“nDispose of student class”);

}

// hash code method

public int hashCode()

{

return fullName.hashCode() + id.hashCode();

}

//override compareTo

public int compareTo(Student stuObj)

{

return this.compareTo(stuObj);

}

} // end Student

TestStudentName.java

// ———————————————————-

// TestStudentName.java

// ———————————————————-

public class TestStudentName

{

//in order to implement cloning the main method must handle CloneNotSupportedException

public static void main ( String [ ] args ) throws CloneNotSupportedException

{

//create two objects for Name class

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

//test the equal method

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

}

//test the equals method

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

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

}

}

Output:

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.