vendredi 27 janvier 2017

How to use mutator method to make a copy of object and compare it before and after

package studentclient;

public class StudentClient {
    public static void main(String[] args) {

        Student s1 = new Student("Samson", "101875643", 2.7);
        System.out.println(s1.toString());
        System.out.println();

        Student s2 = new Student("Delilah", "511978900", 3.9);
        System.out.println(s2.toString());
        System.out.println();


        /*Using the equals method and a selection control structure
        (if statement), compare objects s1 and s2 and output an appropriate
        message indicating if the objects are equal
        */

        /*Using the appropriate mutator methods on student object s2,
        change the name, social security number and GPA to the same 
        values as in object s1. Use the set methods.
        */

        /*Again, using the equals method and a selection control structure
        (if statement), compare objects s1 and s2 and output an 
        appropriate message indicating if the objects are equal
        */
    }

}

I need to 1st compare two objects: in this case, Samson and Delilah, and I need to use an if statement structure to achieve that under StudentClient. Then, I need to use the appropriate mutator methods to change Delilah into a direct copy of Samson (that means, name, SSN and GPA). Finally after that change is done, I need to compare them again. How do I do that? This is very confusing for me, and my experience in C, and Python are of little help as my object-oriented programming muscles are extremely new.

Here is the other part of the code...

package studentclient;

public class Student {

    private String name;
    private String SSN;
    private double gpa;

    public Student(String name, String SSN, double gpa) {
        this.name = name;
        this.SSN = SSN;
        this.gpa = gpa; 
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSSN() {
        return this.SSN;
    }

    public void setSSN(String SSN) { 
        this.SSN = SSN;
    }

    public double getGpa() {
        return this.gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    @Override
    public String toString() {
        String information = "Name: " + name;
        information += "\n" + "Social Security Number: " + SSN;
        information += "\n" + "GPA: " + gpa;

        return information;
    }

    public boolean equals (Student student)
    {
        this.name = student.getName();
        this.SSN = student.getSSN();
        this.gpa = student.getGpa();

        /*equals method returns boolean
        Compares two Student objects for the same field values
        returns a boolean, true if this object
        has the same field value as the parameter object*/
        return false;
    }
}

Aucun commentaire:

Enregistrer un commentaire