java语言程序设计 第十三章 (13.6、13.7)

来源:互联网 发布:c语言中 x是什么意思 编辑:程序博客网 时间:2024/06/07 01:36

程序小白,希望和大家多交流,共同学习
13.6
这里写图片描述
这里写图片描述

public class ComparableCircle     extends MyCircle implements Comparable<ComparableCircle>{    public ComparableCircle()    {    }    public ComparableCircle(double radius)    {        super(radius);    }    public ComparableCircle(double radius, String color, boolean filled)    {        super(radius, color, filled);    }    public ComparableCircle max(ComparableCircle o1, ComparableCircle o2)    {        if (o1.compareTo(o2) > 0)        {            return o1;        }        else            return o2;    }    @Override    public int compareTo(ComparableCircle o)    {        if (super.getArea() > o.getArea())            //用已知的对象的面积和 o 进行比较        {            return 1;        }        else if (super.getArea() < o.getArea())        {            return -1;        }        else            return 0;    }}
public class MyCircle extends GeometricObject{    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;    }}
public class TestComparableCircle{    public static void main(String [] args)    {        ComparableCircle circle1 = new ComparableCircle(3);        ComparableCircle circle2 = new ComparableCircle(4);        System.out.println(circle1.compareTo(circle2));    }}

13.7
这里写图片描述
这里写图片描述

interface Colorable{    public abstract void howToColor();}
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 filled)    {        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 java.util.Date getDateCreated()    {        return dateCreated;    }    public boolean isFilled()    {        return filled;    }    public void setFuilled(boolean filled)    {        this.filled = filled;    }    @Override    public String toString()    {        return "created on " + dateCreated + "\ncolor: " + color + "\nFilled " + filled;    }    public abstract double getArea();    public abstract double getPerimeter();}
public class Square     extends GeometricObject implements Colorable{    private double width;    private double height;    public Square ()    {    }    public Square (double width, double height)    {        this(width, height, "white", false);    }    public Square(double width, double height, String color, boolean filled)    {        super(color, filled);        this.width = width;        this.height = height;    }    @Override    public double getArea()    {        return width * height;    }    @Override    public double getPerimeter()    {        return 2 * (width + height);    }    @Override    public void howToColor()    {        System.out.println("Color all four");    }    @Override    public String toString()    {        if (super.isFilled())        {            howToColor();        }        return super.toString();    }}
import java.util.Scanner;public class TestSqare{    public static void main(String [] args)    {        Scanner input = new Scanner(System.in);        GeometricObject[] squares = new Square[5];        for (int i = 0; i < 5; i++)        {            System.out.println(i + " : Square ");            System.out.print("\tEnter width: ");            double width = input.nextDouble();            System.out.print("\tEnter height: ");            double height = input.nextDouble();            System.out.print("\tEnter Color: ");            String color = input.next();            System.out.print("\tIs Filled: ");            boolean filled = input.nextBoolean();            squares[i] = new Square(width, height, color, filled);        }        for (int i = 0; i < 5; i ++)        {            System.out.println(squares[i].toString());            System.out.println();        }    }}
原创粉丝点击