Question: In this second exercise, we are going to look at the impact of overriding the equals and toString methods. Start by copying over your TestEquals
In this second exercise, we are going to look at the impact of overriding the equals and toString methods.
Start by copying over your TestEquals class from the previous exercise. You will not need to make any changes to this code.
Instead, you are going to add the following methods to your Ball class:
public String toString() public boolean equals(Ball other)
public class Ball { private String color; public Ball(String color){ this.color = color; } public String getColor(){ return color; } // Write an equals method here that returns true if // the colors are the same. // Write a toString statement here, for example: // Color: Red }
public class TestEquals { public static void main(String[] args) { System.out.println("** Object **"); Object obj1 = new Object(); Object obj2 = new Object(); Object obj3 = obj1; // Copy your entire code from the previous exercise
System.out.println("** Ball **"); Ball ball1 = new Ball("Red"); Ball ball2 = new Ball("Red"); Ball ball3 = ball1;
} }


1 public class Ball 2-{ 3 private String color; 5 points Status: Not Submitted FILES 4 In this second exercise, we are going to look at the impact of overriding the equals and toString methods. public Ball(String color){ this.color = color; 7 L Balljava Start by copying over your TestEquals class from the previous exercise. You will not need to make any changes to this code. Instead, you are going to add the following methods to your Ball class: TestEquals.java public String getColor { return color; 7 public String toString public boolean equals(Ball other) 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 203 21 // Write an equals method here that returns true if // the colors are the same. // Write a toString statement here, for example: 1/ Color: Red Inheritance 92 9 Status: Not Submitted 10 RUN CODE TEST CASES ASSIGNMENT DOCS | GRADE | MORE 5 points Status: Not Submitted FILES 9.7.7: Equals? - Part 2 Save Submit + Continue 1 public class TestEquals ( 3 public static void main(String[] args) { 5 System.out.println("** Object **"); 6 Object obji - new ObjectO; 7 Object obj2 = new ObjectO; 8 Object obj3 = obj1; 9 10 // Copy your entire code from the previous exercise In this second exercise, we are going to look at the impact of overriding the equals and toString methods. Start by copying over your TestEquals class from the previous exercise. You will not need to make any changes to this code. Instead, you are going to add the following methods to your Ball class: Ball.java TestEquals.java public String toStringo public boolean equals(Ball other) System.out.println("** Ball **"); 12 13 14 15 16 17 18 19 20 21 ZZ Ball balli - new Ball("Red"); Ball ball2 - new Ball("Red"); Ball ball3 = ball1; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
