NLKappa

40 (сравнение двух площадей)

Nov 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. abstract class Shape {
  2.     public int compareTo (Shape s) {
  3.         if (s == null) {
  4.             throw new NullPointerException();
  5.         }
  6.         if (this.getArea() > s.getArea()) {
  7.             return 1;
  8.         }
  9.         if (this.getArea() < s.getArea()) {
  10.             return -1;
  11.         }
  12.         return 0;
  13.     }
  14.     double getArea() {
  15.         return 0;
  16.     }
  17. }
  18.  
  19. class Circle extends Shape {
  20.     public Circle(double r) {
  21.         this.r = r;
  22.     }
  23.  
  24.     double r;
  25.     double getArea() {
  26.         return Math.PI*r*r;
  27.         }
  28. }
  29.  
  30. class Square extends Shape {
  31.     public Square(double a) {
  32.         this.a = a;
  33.     }
  34.  
  35.     double a;
  36.  
  37.     double getArea() {
  38.         return a*a;
  39.     }
  40. }
  41.  
  42. class Rect extends Shape {
  43.     double a;
  44.     double b;
  45.         public Rect(int a, int b) {
  46.             this.a = a;
  47.             this.b = b;
  48.         }
  49.         double getArea() {
  50.             return a*b;
  51.         }
  52. }
  53. public class Main {
  54.     public static double area (double p, double a, double b, double c) {
  55.         return Math.sqrt(p*(p-a)*(p-b)*(p-c));
  56.     }
  57.     Shape s1;
  58.     Shape s2;
  59.     public static void main(String[] args) {
  60.  
  61.         Shape s1 = new Rect(5,5);
  62.         Shape s2 = new Square(2);
  63.         System.out.println(s1.compareTo(s2));
  64.  
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment