Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package obdelnik;
- class NotARectangleException extends Exception {
- public NotARectangleException(String message) {
- super(message);
- }
- }
- public class Obdelnik {
- int a, b, lhrx, lhry;
- Obdelnik(int sideALength,
- int sideBLength,
- int LeftUpperCornerXcoord,
- int LeftUpperCornerYcoord) throws NotARectangleException {
- nastavRozmery(sideALength,
- sideBLength,
- LeftUpperCornerXcoord,
- LeftUpperCornerYcoord);
- }
- void nastavRozmery(int sideALength,
- int sideBLength,
- int LeftUpperCornerXcoord,
- int LeftUpperCornerYcoord) throws NotARectangleException {
- if (sideALength < 0
- || sideBLength < 0
- || LeftUpperCornerXcoord < 0
- || LeftUpperCornerYcoord < 0) {
- throw new NotARectangleException("Either sides "
- + sideALength
- + " and "
- + sideBLength
- + " don't make a rectangle or coords "
- + LeftUpperCornerXcoord
- + " and "
- + LeftUpperCornerYcoord
- + " are negative!");
- }
- a = sideALength;
- b = sideBLength;
- lhrx = LeftUpperCornerXcoord;
- lhry = LeftUpperCornerYcoord;
- }
- boolean dotyk(Obdelnik obd) {
- if ((lhrx + a == obd.lhrx && lhry + b > obd.lhry && obd.lhry + obd.b > lhry)
- || (lhry + b == obd.lhry && obd.lhrx + obd.a > lhrx && lhrx + a > obd.lhrx)
- || (lhrx == obd.lhrx + obd.a && obd.lhry + obd.b > lhry && lhry + b > obd.lhry)
- || (lhry == obd.lhry + obd.b && obd.lhrx + obd.a > lhrx && lhrx + a > obd.lhrx)) {
- System.out.println("Trojúhleníky se dotýkají.");
- return true;
- }
- System.out.println("Trojúhleníky se nedotýkají.");
- return false;
- }
- boolean prekryv(Obdelnik obd) {
- if ((lhrx < obd.lhrx + obd.a)
- && (lhrx + a > obd.lhrx)
- && (lhry < obd.lhry + obd.b)
- && (lhry + b > obd.lhry)) {
- System.out.println("Trojúhelníky se překrývají.");
- return true;
- }
- System.out.println("Trojúhleníky se nepřekrývají.");
- return false;
- }
- public static void main(String[] args) {
- try {
- Obdelnik a = new Obdelnik(10, 20, 5, 5);
- Obdelnik b = new Obdelnik(8, 2, 9, 25);
- a.dotyk(b);
- b.dotyk(a);
- Obdelnik c = new Obdelnik(50, 50, 18, 30);
- Obdelnik d = new Obdelnik(87,93, 35, 21);
- c.prekryv (d);
- d.prekryv(c);
- } catch (NotARectangleException e) {
- System.err.println("Error: " + e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment