For this exercise, you are given 3 Object class objects and 3 Ball class objects. In…
- User
For this exercise, you are given 3 Object class objects and 3 Ball class objects. In theory, all three Objects are the same and all 3 Ball objects are the same, but based on the way they are created and the way that Java evaluates equality, not all three objects will be equal.
Your task is to start by printing out the 6 Object and Ball objects. Remember, the Object class has a toString and since a Ball object extends the Object class, we should see similarities between the Object and Ball class.
After printing them out, you should notice which objects are the same. Write two true and two false statements for each group, one using == and one using .equals. public class Ball { private double diameter;
public Ball(double diameter){ this.diameter = diameter; }
codepublic double getDiameter(){ return diameter;} } public class TestEquals { public static void main(String[] args) { System.out.println("** Object **"); Object first = new Object(); Object second = first; Object third = new Object();
code// Print out all three objects // Print a true statement using 2 objects and == // Print a false statement using 2 objects and == // Print a true statement using 2 objects and .equals() // Print a false statement using 2 objects and .equals() System.out.println("** Ball **"); Ball golfball = new Ball(1.5); Ball minigolf = golfball; Ball largeGolf = new Ball(1.5); // Print out all three objects // Print a true statement using 2 objects and == // Print a false statement using 2 objects and == // Print a true statement using 2 objects and .equals() // Print a false statement using 2 objects and .equals() }}
- Assistant
- User
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 double diameter;
public Ball(double diameter){ this.diameter = diameter; }
codepublic double getDiameter(){ return diameter;}
// Write an equals method here that returns true if // the diameters are the same.
// Write a toString statement here, in this format: // Diameter: 2.75
} public class TestEquals { public static void main(String[] args) { System.out.println("** Object **"); Object first = new Object(); Object second = first; Object third = new Object();
code// Copy your entire code from the previous exercise System.out.println("** Ball **"); Ball golfball = new Ball(1.5); Ball minigolf = golfball; Ball largeGolf = new Ball(1.5); } - Assistant
- User
In this exercise, you are going to complete the Rectangle class. You need to complete the class so that you can print and compare two rectangles. Two rectangles will be considered equal if they have the same widths and the same heights.
Once complete, use the RectangleTester to test the results. In the tester class, you are given 3 rectangles that you should print and compare. Sample results are provided below.
Sample output A rectangle with a width of 5 and a height of 4 A rectangle with a width of 5 and a height of 4 A rectangle with a width of 10 and a height of 4 true false public class Rectangle{
codeprivate int width; private int height; public Rectangle(int width, int height) { }}public class RectangleTester { public static void main(String[] args) { Rectangle rect1 = new Rectangle(5, 4); Rectangle rect2 = new Rectangle(5, 4); Rectangle rect3 = new Rectangle(10, 4);
code// Print all three rectangles // Print one true statement comparing rectangles // Print one false statment comparing rectangles }}
- Assistant
- User
running into errors with this code public class RectangleTester { public static void main(String[] args) { Rectangle rect1 = new Rectangle(5, 4); Rectangle rect2 = new Rectangle(5, 4); Rectangle rect3 = new Rectangle(10, 4);
code// Print all three rectangles System.out.println(rect1); System.out.println(rect2); System.out.println(rect3); // Print one true statement comparing rectangles System.out.println("True: rect1.equals(rect2): " + rect1.equals(rect2)); // Print one false statment comparing rectangles } System.out.println("False: rect1.equals(rect3): " + rect1.equals(rect3));}
- Assistant
It looks like there's a syntax error in your
RectangleTesterclass. The lastSystem.out.printlnstatement is outside the method, which is incorrect. Let's fix your class by ensuring all the code is properly within the method. - User
For the final exercise, we are going to create a 2D Array tester. We want our tester to be versatile, so the class has been designed to take any 2D Object. Polymorphism will allow us to pass any Object type in and do a comparision using that object’s .equal method.
This can get a little tricky, so you are given a bit of the structure to help make this happen. You are also given a complete tester code that will compare 4 different arrays for you. The first two should be the same and return true, the second two are different and should return false.
Your task is to complete the equals method. Two arrays are equal if all the elements are the same. As you develop your logic, be sure to consider the case when two arrays may be different sizes.
public class TwoDArray {
code - Assistant