java语言程序设计 第十三章 (13.9、13.10、13.11、13.12)

程序小白,希望和大家多交流,共同学习

由于13.913.1013.1113.12继承自相同的父类,所以将其写在一起
UML类图也画在一起了。

java语言程序设计 第十三章 (13.9、13.10、13.11、13.12)_第1张图片
这里写图片描述
java语言程序设计 第十三章 (13.9、13.10、13.11、13.12)_第2张图片

public abstract class GeometricObject
{
    private String color;
    private boolean filled;
    private java.util.Date dateCreated;

    protected GeometricObject()
    {
        dateCreated = new java.util.Date();
    }

    protected GeometricObject(String color, boolean isFilled)
    {
        this.color = color;
        this.filled = filled;
        dateCreated = new java.util.Date();
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public boolean isFilled()
    {
        return filled;
    }

    public void setFilled(boolean filled)
    {
        this.filled = filled;
    }

    public java.util.Date getDateCreated()
    {
        return dateCreated;
    }

    @Override
    public String toString()
    {
        return "created on " + dateCreated + "\ncolor: " + color + "\nand filled: " + filled;
    }

    public abstract double getArea();

    public abstract double getPerimeter();
}
public class MyCircle 
    extends GeometricObject implements Comparable<MyCircle> 
{
    private double radius;

    public MyCircle()
    {
    }

    public MyCircle(double radius)
    {
        this(radius, "white", false);
    }

    public MyCircle(double radius, String color, boolean filled)
    {
        super(color, filled);
        this.radius = radius;
    }

    public double getRadius()
    {
        return radius;
    }

    public void setRadius(double radius)
    {
        this.radius = radius;
    }

    public double getDiameter()
    {
        return 2 * radius;
    }

    @Override
    public double getArea()
    {
        return Math.PI * radius * radius;
    }

    @Override
    public double getPerimeter()
    {
        return 2 * Math.PI * radius;
    }

    @Override
    public int compareTo(MyCircle circle)
    {
        if (getArea() < circle.getArea())
        {
            return -1;
        }
        else if (getArea() > circle.getArea())
        {
            return 1;
        }
        else
            return 1;
    }

    @Override
    public boolean equals(Object o)
    {
        if (((MyCircle)o).getRadius() == radius)
        {
            return true;
        }
        else
            return false;
    }
}
public class MyRectangle 
    extends GeometricObject implements Comparable
{
    private double width;
    private double height;

    public MyRectangle()
    {
    }

    public MyRectangle(double width, double height)
    {
        this(width, height, "white", false);
    }

    public MyRectangle(double width, double height, String color, boolean filled)
    {
        super(color, filled);
        this.width = width;
        this.height = height;
    }

    public double getWidth()
    {
        return width;
    }

    public void setWidth(double width)
    {
        this.width = width;
    }

    public double getHeight()
    {
        return height;
    }

    public void setHeight(double height)
    {
        this.height = height;
    }

    @Override
    public double getArea()
    {
        return width * height;
    }

    @Override
    public double getPerimeter()
    {
        return 2 * (width + height);
    }

    @Override
    public int compareTo(MyRectangle rectangle)
    {
        if (getArea() > rectangle.getArea())
        {
            return 1;
        }
        else if (getArea() < rectangle.getArea())
        {
            return -1;
        }
        else
            return 0;
    }

    @Override
    public boolean equals(Object o)
    {
        if (compareTo((MyRectangle)o) == 0)
        {
            return true;
        }
        else
            return false;
    }
}
public class MyOctagon 
    extends GeometricObject implements Comparable<MyOctagon>, Cloneable
{
    private double side;

    public MyOctagon()
    {
    }

    public MyOctagon(double side)
    {
        this(side, "White", false);
    }

    public MyOctagon(double side, String color, boolean filled)
    {
        super(color, filled);
        this.side = side;
    }

    public double getSide()
    {
        return side;
    }

    public void setSide(double side)
    {
        this.side = side;
    }

    @Override
    public double getArea()
    {
        return (2 + 4 * Math.sqrt(2)) * side * side;
    }

    @Override
    public double getPerimeter()
    {
        return 8 * side;
    }

    @Override
    public int compareTo(MyOctagon octagon)
    {
        if (getArea() > octagon.getArea())
        {
            return 1;
        }
        else if (getArea() < octagon.getArea())
        {
            return -1;
        }
        else
            return 0;
    }

    @Override
    public String toString()
    {
        return super.toString()  + "\nside : "  + side;
    }

    @Override
    public Object clone() 
    {
        try
        {
            MyOctagon octagon = (MyOctagon)super.clone();
            return octagon;
        }
        catch (CloneNotSupportedException ex)
        {
            return null;
        }
    }
}

下面是各个子类的测试类,还有面积求和类

import java.util.Scanner;
import java.util.Arrays;

public class TestMyCircle
{
    public static void main(String [] args)
    {
        MyCircle circle1 = new MyCircle(2.5);
        MyCircle circle2 = new MyCircle(4.5);
        MyCircle circle3 = new MyCircle(2.5);
        MyCircle circle4 = new MyCircle(4.5);
        MyCircle circle5 = new MyCircle(2.5);
        MyCircle circle6 = new MyCircle(4.5);

        System.out.println("circle1 equals circle2 ? " + circle1.equals(circle2));
        System.out.println("circle1 equals circle3 ? " + circle1.equals(circle3));

        MyCircle[] circles = {circle1, circle2, circle3, circle4, circle5, circle6};
        Arrays.sort(circles);

        for (MyCircle circle : circles )
        {
            System.out.println(circle.getArea());
        }

    }
}
import java.util.Arrays;

public class TestMyRectangle
{
    public static void main(String [] args)
    {
        MyRectangle rectangle1 = new MyRectangle(2, 4);
        MyRectangle rectangle2 = new MyRectangle(3, 5);
        MyRectangle rectangle3 = new MyRectangle(2, 4);
        MyRectangle rectangle4 = new MyRectangle(3, 5);
        MyRectangle rectangle5 = new MyRectangle(2, 4);
        MyRectangle rectangle6 = new MyRectangle(3, 5);

        System.out.println("rectangle1 equals rectangle2 ? " + rectangle1.equals(rectangle2));
        System.out.println("rectangle1 equals rectangle3 ? " + rectangle1.equals(rectangle3));

        MyRectangle[] rectangles = {rectangle1, rectangle2, rectangle3, rectangle4, rectangle5, rectangle6};
        for (MyRectangle rectangle : rectangles)
        {
            System.out.println(rectangle.getArea());
        }
    }
}
public class TestMyOctagon
{
    public static void main(String [] args)
    {
        MyOctagon octagon1 = new MyOctagon(5);
        System.out.println(octagon1.getArea());
        MyOctagon octagon2 = (MyOctagon)octagon1.clone();
        System.out.println(octagon1.compareTo(octagon2));
    }
}
import java.util.Arrays;

public class GetSumArea
{
    public static void main(String [] args)
    {
        MyCircle circle1 = new MyCircle(5.5);
        MyCircle circle2 = new MyCircle(3.5);

        MyRectangle rectangle1 = new MyRectangle(2, 4);
        MyRectangle rectangle2 = new MyRectangle(4, 5);

        MyOctagon octagon1 = new MyOctagon(3);
        MyOctagon octagon2 = new MyOctagon(6);

        GeometricObject[] figures = {circle1, rectangle1, rectangle1, circle2, octagon1, octagon2};
        double sum = sumArea(figures);

        System.out.println(sum);

    }

    public static double sumArea(GeometricObject[] a)
    {
        double sum = 0.0;
        for (GeometricObject o : a)
        {
            sum += o.getArea();
        }

        return sum;
    }
}

你可能感兴趣的:(抽象类和接口)