Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * INFO0062 - Object-oriented programming
- * Special session (16/05/2019)
- *
- * Example of solution for the 1st question of the written examination from May 2018.
- *
- * Keep in mind that it is not the only possible solution. There are ways to write some parts of
- * the code in another manner that is still as valid as what is presented here.
- *
- * @author: J.-F. Grailet
- */
- import java.io.Serializable; // Remark: not asked at the examination.
- public class Point3D implements Cloneable, Serializable
- {
- // Remark: not asked at the examination.
- private static final long serialVersionUID = 170192L;
- private int x, y, z;
- public Point3D(int x, int y, int z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public int getX() { return x; }
- public int getY() { return y; }
- public int getZ() { return z; }
- public void translate(int dx, int dy, int dz)
- {
- x += dx;
- y += dy;
- z += dz;
- }
- public boolean equals(Object o)
- {
- if(!(o instanceof Point3D))
- return false;
- Point3D p2 = (Point3D) o;
- if(x != p2.x || y != p2.y || z != p2.z)
- return false;
- return true;
- }
- public int hashCode()
- {
- return x + y + z;
- }
- public Object clone()
- {
- Point3D clone = null;
- try
- {
- clone = (Point3D) super.clone();
- }
- catch(CloneNotSupportedException e)
- {
- throw new InternalError("Unable to clone Point3D");
- }
- return clone;
- }
- // Not asked, just for display in the Main class
- public String toString()
- {
- return "(" + x + "," + y + "," + z + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement