Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to

ID: 3876265 • Letter: P

Question

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

Explanation / Answer

// Name.java
public class Name {

   private String first;
   private String last;

   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
   {

   }

   public String getFirst() {
       return first;
   }

   public void setFirst(String first) {
       this.first = first;
   }

   public String getLast() {
       return last;
   }

   public void setLast(String last) {
       this.last = last;
   }

   public String getName(){
       return toString();
   }

   public void giveLastNameTo(Name aName){
       aName.setLast(last);
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((first == null) ? 0 : first.hashCode());
       result = prime * result + ((last == null) ? 0 : last.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Name other = (Name) obj;
       if (first == null) {
           if (other.first != null)
               return false;
       } else if (!first.equals(other.first))
           return false;
       if (last == null) {
           if (other.last != null)
               return false;
       } else if (!last.equals(other.last))
           return false;
       return true;
   }

   @Override
   public String toString() {
       return "Name [first=" + first + ", last=" + last + "]";
   }


  
   @Override
   protected void finalize() throws Throwable {
       // TODO Auto-generated method stub
       super.finalize();
   }

   public int compareTo(){
       return 0;
   }
  
  
   public void dispose(){   }
  
  
}

// Student.java

public class Student {

// to remove error import Name class here.  
private Name fullName;
private String id;
public Student() {
  
  
   this(new Name(),"");
}
public Student(Name fullName, String id) {
   super();
   this.fullName = fullName;
   this.id = id;
}

   public Student(Student obj)throws NullPointerException{
      
   }
   @Override
   protected Object clone() throws CloneNotSupportedException {
       // TODO Auto-generated method stub
       return super.clone();
   }
   @Override
   public boolean equals(Object obj) {
       // TODO Auto-generated method stub
       return super.equals(obj);
   }
   @Override
   protected void finalize() throws Throwable {
       // TODO Auto-generated method stub
       super.finalize();
   }
   @Override
   public int hashCode() {
       // TODO Auto-generated method stub
       return super.hashCode();
   }
   @Override
   public String toString() {
       // TODO Auto-generated method stub
       return super.toString();
   }
   public Name getFullName() {
       return fullName;
   }
   public void setFullName(Name fullName) {
       this.fullName = fullName;
   }
   public String getId() {
       return id;
   }
   public void setId(String id) {
       this.id = id;
   }
  
  
   public void setStudent(Name studentName,String studentId){
       fullName=studentName;
       id=studentId;
   }
  
  
   public int compareTo(){
       return 0;
   }
  
   public void dispose(){
      
   }
  
}

//TestStudentName

public class TestStudentName {

   public static void main(String[] args) {
      
       // to remove error import Name class here. like import packageName.Name;
       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,"12345");
       Student s2=new Student(s1);
      
       s2.setStudent(n2,"234567");
       if(s1.equals(s2)){
          
           System.out.println("WtWtError-students should not be same");
           System.out.println("WtWts1= "+s1);
           System.out.println("WtWts2= "+s2);          
          
       }
       else{
          
           System.out.println("WtWt Success-students are not the same");
           System.out.println("WtWts1= "+s1);
           System.out.println("WtWts2= "+s2);          
          
          
       }
      
      
      
       // test the clone method----------
               System.out.println("WtWt Test the clone method----------");
              
               Student s3=null;
               try {
                   s3 = (Student)s1.clone();
               } catch (CloneNotSupportedException e) {
                   System.out.println(e.getMessage());
               }
              
          
               if(s1.equals(s3)){
                   System.out.println("WtWt Success- Students s1 and s3 are the same");
                  
               }
               else{
                  
                   System.out.println("WtWtError- Students s1 and s3 are not the same");
                   System.out.println("WtWts1= "+s1);
                   System.out.println("WtWts2= "+s2);          
                  
               }
      
      
               try{
                   s3.setStudent(n2, "234567");
               }catch(NullPointerException e){
                   System.out.println(e.getMessage());
               }
      
if(s1.equals(s3)){
          
           System.out.println("WtWtError-students should not be same");
           System.out.println("WtWts1= "+s1);
           System.out.println("WtWts2= "+s3);          
          
       }
       else{
          
           System.out.println("WtWt Success-students are not the same");
              
       }
      
      
       // Test the finalize method
    System.out.println("WtWt Test the 'finalize' method------");
   s1=null;
   System.gc();
   System.out.println("WtWt should see the 'finalize' message------");

// test the dispose method

   System.out.println("WtWt Test the 'dispose' method------");  
      
       s2.dispose();
      
       System.out.println("WtWt should see the 'dispose' message------");
      
       s2=null;
      
      
       // test the hashcode method
      
       s1=new Student(s3);
      
          System.out.println("WtWt Test the 'hashcode' method------");  
       try{
       if(s1.hashCode()==s3.hashCode()){
           System.out.println("WtWt Hashcode of s1 and s3 are same---");
       }
       else{
           System.out.println("WtWt Hashcode of s1 and s3 are not same---");
      
           System.out.println("WtWts1.hashcode= "+s1.hashCode());
               System.out.println("WtWts2.hashcode= "+s3.hashCode());  
      
       }
       }catch(NullPointerException e){
           System.out.println(e.getMessage());
       }
      
       System.out.println();
      
      
   }
  
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote