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

// ------------------------------------------------------------ // Name.java //

ID: 3879744 • 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

Lab #1-Designing Classes-Software Engineering NAME Problem: Using the ‘Namejava, and Studentjava, 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. All 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 NOTE 2:

Explanation / Answer

package student;

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

}

@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 first + " " + last;

}

@Override
protected Object clone() throws CloneNotSupportedException {
   // TODO Auto-generated method stub
   return super.clone();
}

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

//public void dispose ( ) { }    // dispose method

public int compareTo (Object name) {     // compareTo Method

Name n=(Name)name;

return n.first.compareTo(n.last);

} // end Name

}

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

@Override
protected Object clone() throws CloneNotSupportedException {
   // TODO Auto-generated method stub
   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 ( );

}

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

//public void finalize() { }

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

public void dispose() { }

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

public int compareTo (Object std ) {
   Student s=(Student)std;
  
   return fullName.compareTo(s.fullName);
  
}

} // end Student

package student;

public class TestStudentName {

   public static void main(String[] args) {
      
       Name n=new Name();
       n.setFirst("John");
       n.setLast("Miller");
       n.setName(n.getFirst(), n.getLast());
       Student s=new Student();
       s.setId("std001");
       s.setName(n);
       s.setStudent(s.getName(),s.getId());
      
       System.out.println(n);
       System.out.println(s);
   }
  
}

/*

John Miller
std001 John Miller

*/