Question: so i was practcing toString and equals but couldnt wrap my head around it. can someone help me Using the code at the following link

so i was practcing toString and equals but couldnt wrap my head around it. can someone help me
so i was practcing toString and equals but couldnt wrap my head
around it. can someone help me Using the code at the following
link replit practiced Do the following: 1. Write the toString method. It
must return a string with all instance variables labeled and formatted. (10
Points-3 points for each instance variable being included, 1 point foc formatting)

Using the code at the following link replit practiced Do the following: 1. Write the toString method. It must return a string with all instance variables labeled and formatted. (10 Points-3 points for each instance variable being included, 1 point foc formatting) 2. Write the equals method (10 Points - Correct use of Instanceof) Both the toString and the equals methods are in the Person class (the template is included at the bottom of the Person Class) 3. Then, create some instances and test them! (5 points for each instance) 4. You must use the built in.equals method to compare the instances (must use it at least 3 times) (5 Points - 5 tests in total that test both the toString (2 times) and the equals method (3 times)) Files m Console Shell OpenDK Runtime Environ .04.1) Main.java Person.java CH Main.java 1 class Main 2 public static void main(String[] args) { 3 //First, go write tostring and equals in the Person class 4 5 /Then, create some instances and test them! 6 Persons = new Person(); 7 Person d = new Person ("David", 17, "Red"); 8 Person d1 = new Person ("David", 17, "Red"); 9 Person s1 = new Person(); 19 Person d2 = new Person("David", 19, "Red"); 11 Person 1 = new Person ("James", 22, "Blue"); 12 Person j2 = new Person("James", 22, "Blue"); 13 14 //testing the tostring method 15 System.out.println(s.toString()); I 16 System.out.println(d.toString(); 17 System.out.println(di.toString()); 18 System.out.println(s1.toString()); 19 System.out.println(d2.toString(); 20 System.out.println().toString()); 21 System.out.println(j2.toString()); 22 23 System.out.println(); 24 25 //Use .equals at least 3 times 26 System.out.println(s.equals(d)): 27 System.out.println(s.equals(si)); 28 System.out.println(d.equals(d1)); 29 System.out.println().equals(2)): 30 System.out.println(12.equals(d2)); 31 > E Console Shell Open JDK Runtime Environment cu .04.1) Main.java Person.java Person d = new Person("David", 17, "Red"); Person d1 = new Person("David", 17, "Red"); Person si = new Person(); Person d2 = new Person("David", 19, "Red"); Person j = new Person("James", 22, "Blue"); Person 12 = new Person("James", 22, "Blue"); Main.java 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 //testing the toString method System.out.println(s.toString(); System.out.println(d.toString(); System.out.println(d1.toString(); System.out.println(s1.toString()); System.out.println(d2.toString(); System.out.println(j.toString()); System.out.println(j2.toString(): System.out.println(); //Use equals at least 3 times System.out.println(s.equals(d)): I System.out.println(s.equals(si)): System.out.println(d.equals(d1)); System.out.println(j.equals(2)); System.out.println(j2.equals(d2)): Book Pro Console Shell ava Open JDK Runtime Environment bull .84.1) n.java 18 Person.java 40 E public void setFavoriteColor(String fc){ 41 favoriteColor = fc;//why didn't I use this....? 42 } 43 44 77/toString method to print out Object variables easily 45 //You must print and label all of the instance variables 46 public String toString() { 47 String str = "name: + name + " Age: " + age + In Favorite color: " + favoriteColor + " "; 48 return str; 49 50 51 public boolean equals(Person p}{ 52 if(this == p){ 53 return true; 54 55 if(p == null) { 56 return false; 57 > 58 if(p instanceof Person) { 59 Person pl = (Person) P: 60 if(p1.getName() == this.getName() 61 return true; 62 63 if(p1.getAge() == this.getAge()) 64 return true; 65 ) 66 if (pl.getFavoritecolor() - this.getFavoriteColor()) return true; 67 68 69 M Console Shell java Open JDK Runtime Environment Cou .04.1) on.java 50 51B 52 53 54 455 56 57 58 59 60D 61 62 63 64 65 66 public boolean equals(Person p){ if(this == p) return true; } if(p == null){ return false; } if(p instanceof Person){ Person pl = (Person) p; if(p1.getName() == this.getName()){ return true; } if(p1.getAge() == this.getAge()) { return true; } if(p1.getFavoriteColor() ==this.getFavoriteColor() { return true; } return false; 69 70 71 72 73 > >

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!