Skip to content

Latest commit

 

History

History
28 lines (18 loc) · 1.41 KB

0f05.md

File metadata and controls

28 lines (18 loc) · 1.41 KB

Back to questions

0f05: Coloured points

Write a class, Point, representing a point in 3-dimensional Euclidean space. You should ensure that objects of type Point are immutable. That is, once a Point has been created, its fields cannot be modified further.

Your class should provide the following:

  • a method that takes a Point parameter and returns the Euclidean distance between this parameter and the Point on which the method is called

  • a method that returns a Point representing the origin

  • a method that returns the magnitude of a Point -- that is, its distance from the origin

In addition, you should override the toString() method provided by Object to provide an appropriate string representation of a Point.

Write some tests to make sure your class is working as expected.

Now write a new class, ColouredPoint, that extends Point to provide an additional colour field. It is up to you whether you represent colour using an enumeration of several permissible colours, using integer values for red, green and blue, or some other way. You should override toString() to represent a ColouredPoint as a string by showing its coordinates followed by a string representation of its colour value. You should not repeat the code used to represent a point's coordinates as a string: this string representation should be achieved via the toString() method in Point.