Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. /*
  2.  * INFO0062 - Object-oriented programming
  3.  * Special session (16/05/2019)
  4.  *
  5.  * Example of solution for the 1st question of the written examination from May 2018.
  6.  *
  7.  * Keep in mind that it is not the only possible solution. There are ways to write some parts of
  8.  * the code in another manner that is still as valid as what is presented here.
  9.  *
  10.  * @author: J.-F. Grailet
  11.  */
  12.  
  13. import java.io.Serializable; // Remark: not asked at the examination.
  14.  
  15. public class Point3D implements Cloneable, Serializable
  16. {
  17.     // Remark: not asked at the examination.
  18.     private static final long serialVersionUID = 170192L;
  19.    
  20.     private int x, y, z;
  21.    
  22.     public Point3D(int x, int y, int z)
  23.     {
  24.         this.x = x;
  25.         this.y = y;
  26.         this.z = z;
  27.     }
  28.    
  29.     public int getX() { return x; }
  30.     public int getY() { return y; }
  31.     public int getZ() { return z; }
  32.    
  33.     public void translate(int dx, int dy, int dz)
  34.     {
  35.         x += dx;
  36.         y += dy;
  37.         z += dz;
  38.     }
  39.    
  40.     public boolean equals(Object o)
  41.     {
  42.         if(!(o instanceof Point3D))
  43.             return false;
  44.        
  45.         Point3D p2 = (Point3D) o;
  46.         if(x != p2.x || y != p2.y || z != p2.z)
  47.             return false;
  48.         return true;
  49.     }
  50.    
  51.     public int hashCode()
  52.     {
  53.         return x + y + z;
  54.     }
  55.    
  56.     public Object clone()
  57.     {
  58.         Point3D clone = null;
  59.         try
  60.         {
  61.             clone = (Point3D) super.clone();
  62.         }
  63.         catch(CloneNotSupportedException e)
  64.         {
  65.             throw new InternalError("Unable to clone Point3D");
  66.         }
  67.         return clone;
  68.     }
  69.    
  70.     // Not asked, just for display in the Main class
  71.     public String toString()
  72.     {
  73.         return "(" + x + "," + y + "," + z + ")";
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement