1

So within this class, I need to create a Equals method that will check to determine if the two objects have the same name. I tried creating the two objects within the class and just initialize it with "" for the constructor, but it gave an error on the created objects

Person.Java

public class Person
{   

String firstName = "";
String lastName = "";
String age = "";    


public Person (String firstName, String lastName, String age){
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;


}

public String getFirstName(){
return firstName;

}

public String getLastName(){
return lastName;

}

public String getAge(){
return age;

}

public void setFirstName(String firstName){
this.firstName = firstName;

}

public void setLastName(String lastName){
this.lastName = lastName;

}

public void setAge(String age){
this.age = age; 

}

public String toString(){
return firstName + " " + lastName + ", " + age + " years old";

}



}

Here is my driver, so basically I need a method that sees both have the same name and prints out a message saying that they have the same name. My lab states it has to be in the class NOT the driver, which is why I'm lost considering I could easily make an if/else statement within the driver.

 public class PersonDriver
{

    public static void main(String[] args)
    {
        Person p1 = new Person("John","Doe", "42");
        Person p2 = new Person("John","Doe", "43");

        System.out.println(p1);
        System.out.println(p2);




    }

}
ripplex
  • 11
  • 1
  • 2

0 Answers0