package wr3; public class Person { private String name; private String id; private String bday; private String address; public String getName(){ return name; } public String getID(){ return id; } public String getBday(){ return bday; } public String getAdd(){ return address; } public void equals(){ super.equals(id); } @Override public String toString(){ return(name + bday + id + address); } }
package wr3; public class Test { public static void main(String[] args){ String name = "Claude Rhay Torre"; String name2 = "Bea Señerpida"; String id = "10302993"; String id2 = "11102825"; String bday = "06/201993"; String bday2 = "11/17/1994"; String address = "BF Better Living Basak LLC"; String address2 = "F Martyr St Poblacion LLC"; boolean eq; System.out.println(name.toString()); System.out.println(id.toString()); System.out.println(bday.toString()); System.out.println(address.toString()); System.out.println(); System.out.println(name2.toString()); System.out.println(id2.toString()); System.out.println(bday2.toString()); System.out.println(address2.toString()); eq = id.equals(id2); System.out.println("\nDo they have the same ID number? " + eq ); } }
So I have this code. And I also have this problem.
- A. Object class
- Study the Object class in the java.lang package.
- Understand all its methods.
- Create a Person class with the requirements:
a. Implement encapsulation
b. The fields are: name, ID (identification number), birthday, and address.
c. A method that will override the equals( ) method of Object class. Two persons are equal if they have the same id.
d. A method that will override the toString( ) method of Object class. It displays the id, name, birthday, and address of a Person object. - Create a test class to create Person objects and call the equals( ) and toString( ) methods appropriately.
My question is, are these two classes even related? What I mean, is my "toString" and "equal" method called on my Test class the one that is in my Person's class? Or is it the "toString" and "equal" methods on the Object class?
How can I override the equals and toString class in the Object class?