Problem 1: The Point class

The goal of this in-lecture exercise is to get familiar with:

  • implicit constructor invokation, and
  • method override from Object class, in particular, toString, equals, hashCode and finalize methods.

1. Provide a class named Point with two integer fields x and y with:

  • A no-arg constructutor, where both x and y should be initialized with 0.
  • A constructutor with one parameter, where x is initialized with the value passed by the argument and y is initialized with 0.
  • A construtor with two parameters, where x and y are initialized with the values passed by the arguments.

Use implicit constructor invokations when possible.

2. In the main method create three objects: (0,0), (0,0) and (2,3). Print their textual description, and test for identity and equality.

3. Override toString to provide a textual description of a Point as "(x,y)". Run the program and check for their new textual descriptions.

4. Override equals to provide equality when the x's and the y's of the points being compared are the same. When equals is overriden which other method should also be overriden? Override it.

5. In the main method create a HashSet<Point> and insert the three points in it. What happens? What happens if one or both of the previous methods were not overriden?

6. Override finalize. In the main, assign one of the references to null, call garbage collector (just for illustration purposes, don't do this in your code as garbage collector runs periodically) and check what happens.

Attachments