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 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; } } 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; } }
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 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;
}
}
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;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps