michael_hartman_cz

PedF UK - OKB1319306 Zkouškový úkol 3 - Obdélník

Jan 27th, 2015
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. package obdelnik;
  2.  
  3. class NotARectangleException extends Exception {
  4.  
  5.     public NotARectangleException(String message) {
  6.         super(message);
  7.     }
  8. }
  9.  
  10. public class Obdelnik {
  11.  
  12.     int a, b, lhrx, lhry;
  13.  
  14.     Obdelnik(int sideALength,
  15.             int sideBLength,
  16.             int LeftUpperCornerXcoord,
  17.             int LeftUpperCornerYcoord) throws NotARectangleException {
  18.         nastavRozmery(sideALength,
  19.                 sideBLength,
  20.                 LeftUpperCornerXcoord,
  21.                 LeftUpperCornerYcoord);
  22.     }
  23.  
  24.     void nastavRozmery(int sideALength,
  25.             int sideBLength,
  26.             int LeftUpperCornerXcoord,
  27.             int LeftUpperCornerYcoord) throws NotARectangleException {
  28.         if (sideALength < 0
  29.                 || sideBLength < 0
  30.                 || LeftUpperCornerXcoord < 0
  31.                 || LeftUpperCornerYcoord < 0) {
  32.             throw new NotARectangleException("Either sides "
  33.                     + sideALength
  34.                     + " and "
  35.                     + sideBLength
  36.                     + " don't make a rectangle or coords "
  37.                     + LeftUpperCornerXcoord
  38.                     + " and "
  39.                     + LeftUpperCornerYcoord
  40.                     + " are negative!");
  41.         }
  42.         a = sideALength;
  43.         b = sideBLength;
  44.         lhrx = LeftUpperCornerXcoord;
  45.         lhry = LeftUpperCornerYcoord;
  46.     }
  47.  
  48.     boolean dotyk(Obdelnik obd) {
  49.         if ((lhrx + a == obd.lhrx && lhry + b > obd.lhry && obd.lhry + obd.b > lhry)
  50.                 || (lhry + b == obd.lhry && obd.lhrx + obd.a > lhrx && lhrx + a > obd.lhrx)
  51.                 || (lhrx == obd.lhrx + obd.a && obd.lhry + obd.b > lhry && lhry + b > obd.lhry)
  52.                 || (lhry == obd.lhry + obd.b && obd.lhrx + obd.a > lhrx && lhrx + a > obd.lhrx)) {
  53.             System.out.println("Trojúhleníky se dotýkají.");
  54.             return true;
  55.         }
  56.         System.out.println("Trojúhleníky se nedotýkají.");
  57.         return false;
  58.     }
  59.  
  60.     boolean prekryv(Obdelnik obd) {
  61.         if ((lhrx < obd.lhrx + obd.a)
  62.                 && (lhrx + a > obd.lhrx)
  63.                 && (lhry < obd.lhry + obd.b)
  64.                 && (lhry + b > obd.lhry)) {
  65.             System.out.println("Trojúhelníky se překrývají.");
  66.             return true;
  67.         }
  68.         System.out.println("Trojúhleníky se nepřekrývají.");
  69.         return false;
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.         try {
  74.         Obdelnik a = new Obdelnik(10, 20, 5, 5);
  75.         Obdelnik b = new Obdelnik(8, 2, 9, 25);
  76.         a.dotyk(b);
  77.         b.dotyk(a);
  78.         Obdelnik c = new Obdelnik(50, 50, 18, 30);
  79.         Obdelnik d = new Obdelnik(87,93, 35, 21);
  80.         c.prekryv (d);
  81.         d.prekryv(c);
  82.         } catch (NotARectangleException e) {
  83.             System.err.println("Error: " + e.getMessage());
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment