package designingclasses2; /** * This class stores data about a student. */ public class Student { // instance variable private String name; /** * This constructor initializes the field to the passed value. * @param studentName The student's name. */ public Student(String studentName) { name = studentName; } /** * This is a copy constructor. It initializes the fields of the object being created to the same * values as the fields in the object passed as an argument. * @param studentObject The object to copy. */ public Student(Student studentObject) { if( studentObject != null ) { name = studentObject.name; } } /** * The getName method returns a Student object's name. * @return The value in the name field. */ public String getName() { return name; } /** * The setName method stores a value in the name field. * @param studentName the value to store in name */ public void setName(String studentName) { name = studentName; } /** * The toString method returns a string representing the state of the object. * @return A string containing the student information. */ @Override public String toString() { // Create a string representing the object. String str = String.format("\n%-20s %s", "Student Name:", name); // Return the string. return str; } /** * The equals method compares two Student objects. The result is true if the argument * is not null and is a Student object with the same value for the name field as this object. * @param obj The object to compare this Student with. * @return true if the given object has the same value for the name field. */ @Override public boolean equals(Object obj) { if( !(obj instanceof Student)) return false; // we already know that obj is of type Student, so it's safe to cast Student student = (Student) obj; // return true or false depending on whether the name fields have the same value return name.equals(student.name); } /** * The copy method creates a new Student object and initializes it with the same data in the * calling object. * @return a reference to the new object. */ public Student copy() { return new Student(this); } }