Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.53 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.  
  14. public class LineSegment implements Cloneable, Serializable
  15. {
  16.     private Point3D p1, p2;
  17.    
  18.     // Remark: int[] p1, int[] p2 also accepted
  19.     public LineSegment(int x1, int y1, int z1, int x2, int y2, int z2)
  20.     {
  21.         p1 = new Point3D(x1, y1, z1);
  22.         p2 = new Point3D(x2, y2, z2);
  23.     }
  24.    
  25.     // Remark: providing the 3 values dx, dy and dz as an array is accepted too
  26.     public synchronized void translate(int dx, int dy, int dz)
  27.     {
  28.         p1.translate(dx, dy, dz);
  29.         p2.translate(dx, dy, dz);
  30.     }
  31.    
  32.     public synchronized LineSegment[] split() throws ZeroSegmentException
  33.     {
  34.         int m_x, m_y, m_z;
  35.         m_x = (p1.getX() + p2.getX()) / 2;
  36.         m_y = (p1.getY() + p2.getY()) / 2;
  37.         m_z = (p1.getZ() + p2.getZ()) / 2;
  38.        
  39.         Point3D middle = new Point3D(m_x, m_y, m_z);
  40.         if(middle.equals(p1) || middle.equals(p2))
  41.             throw new ZeroSegmentException();
  42.        
  43.         LineSegment[] res = new LineSegment[2];
  44.         res[0] = new LineSegment(p1.getX(), p1.getY(), p1.getZ(), m_x, m_y, m_z);
  45.         res[1] = new LineSegment(m_x, m_y, m_z, p2.getX(), p2.getY(), p2.getZ());
  46.         return res;
  47.     }
  48.    
  49.     public boolean equals(Object o)
  50.     {
  51.         if(!(o instanceof LineSegment))
  52.             return false;
  53.        
  54.         LineSegment l2 = (LineSegment) o;
  55.         if((p1.equals(l2.p1) && p2.equals(l2.p2)) || (p1.equals(l2.p2) && p2.equals(l2.p1)))
  56.             return true;
  57.         return false;
  58.     }
  59.    
  60.     public int hashCode()
  61.     {
  62.         return p1.hashCode() + p2.hashCode();
  63.     }
  64.    
  65.     public Object clone()
  66.     {
  67.         LineSegment clone = null;
  68.         try
  69.         {
  70.             clone = (LineSegment) super.clone();
  71.             clone.p1 = (Point3D) p1.clone();
  72.             clone.p2 = (Point3D) p2.clone();
  73.         }
  74.         catch(CloneNotSupportedException e)
  75.         {
  76.             throw new InternalError("Unable to clone LineSegment object.");
  77.         }
  78.         return clone;
  79.     }
  80.    
  81.     // Not asked, just for display in the Main class
  82.     public String toString()
  83.     {
  84.         return p1 + " -> " + p2;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement