Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Shape {
- public int compareTo (Shape s) {
- if (s == null) {
- throw new NullPointerException();
- }
- if (this.getArea() > s.getArea()) {
- return 1;
- }
- if (this.getArea() < s.getArea()) {
- return -1;
- }
- return 0;
- }
- double getArea() {
- return 0;
- }
- }
- class Circle extends Shape {
- public Circle(double r) {
- this.r = r;
- }
- double r;
- double getArea() {
- return Math.PI*r*r;
- }
- }
- class Square extends Shape {
- public Square(double a) {
- this.a = a;
- }
- double a;
- double getArea() {
- return a*a;
- }
- }
- class Rect extends Shape {
- double a;
- double b;
- public Rect(int a, int b) {
- this.a = a;
- this.b = b;
- }
- double getArea() {
- return a*b;
- }
- }
- public class Main {
- public static double area (double p, double a, double b, double c) {
- return Math.sqrt(p*(p-a)*(p-b)*(p-c));
- }
- Shape s1;
- Shape s2;
- public static void main(String[] args) {
- Shape s1 = new Rect(5,5);
- Shape s2 = new Square(2);
- System.out.println(s1.compareTo(s2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment