0
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;
    }
}
  • object1.equals(object2) and to copy copyObject = object.clone() Every object in java has a clone method associated with it. https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone() – Waffles Jan 28 '17 at 07:49
  • i made the data equal to test the if statement and "s1.equals(s2))" but it didn't give me the right answer....to reiterate it was `if (s1.equals(s2)) System.out.println("The students are equal."); but when i set the arguments equal to each other it didn't give me that output – Coffee Man Jan 28 '17 at 07:57
  • What exactly are you trying to compare? The values within the objects? If you want to do that you'll have to override the equals function. http://stackoverflow.com/a/185942/3072960 – Waffles Jan 28 '17 at 08:06
  • I want to compare s1 and s2 & if they are = to print "They are ="...else have it print "They are not =" and then proceed to make s2 equal to s1 using the appropriate _set_ methods. Afterwards it will compare them again and it should say whether or not s1 and s2 are equal (they should be equal)...here is a sample block I'm working on that doesn't work for me...`if (s1.equals(s2)) System.out.println("The students are equal."); else System.out.println("The students are not equal."); s2.equals(s1); System.out.println(s2.toString());` – Coffee Man Jan 28 '17 at 08:19

0 Answers0