抽象类与接口
任务:(接口与抽象类)设计一个抽象类GeoGraph,定义所有2维图形的共有数据特性:颜色(color,用整型表示)、填充标志(filled,使用boolean),共有行为特性:面积、周长(抽象方法)及其他必要行为特性(实例方法);该类继承Comparable接口和自定义Colorable接口(抽象方法:procColor,用于对图像作色),然后由GeoGraph类派生:Circle、SemiCircle(半圆)、Rectangle(矩形)、Triangle三角形)、Oval(椭圆形)、Trapezoid(梯形),重写这些类型的toString方法:类型:面积(Oval:456.456),作色方式定义为:图形面积的高2位整数(如果只有1位整数部分,则只取一位或小数点后不为0的2位数(1 <=color<=99)为color赋值,这些类型的其他要求由大家自己定义,但要符合GeoGraph派生类的要求。最后在主类中定义一个GeoGraph数组GeoArray并为该数组创建上面定义类型的图形对象,然后使用Arrays.Sort对数组排序并输出数组,输出方式:对象类型:面积(如:Oval:23.54)。排序依据:根据图形面积大小排序。(第9周课堂作业与上机作业共用)
实现代码:
1,GeoGraph类
package InterGeo;
public abstract class GeoGraph implements Comparable<GeoGraph>, Colorable {
public int color;
public boolean filled;
public abstract double getArea();
public abstract double getPerimeter();
public abstract int compareTo(GeoGraph o);
public abstract void procColor(GeoGraph o);
// 其他实例方法
protected GeoGraph() {
}
protected GeoGraph(int color, boolean filled) {
this.color = color;
this.filled = filled;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public String toString() {
return "this is a geograph";
}
}
2,Circle类
package InterGeo;
import java.lang.String;
public class Circle extends GeoGraph {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public int compareTo(GeoGraph o) {
if (getArea() - o.getArea() > 0) {
return 1;
} else if (getArea() - o.getArea() < 0) {
return -1;
} else
return 0;
}
public void procColor(GeoGraph o) {
int color = (int) getArea();
while (color >= 100) {
color /= 10;
}
this.color = color;
}
@Override
public String toString() {
return "the area of Circle is:" + getArea();
}
}
3,Rectangle类
package InterGeo;
public class Rectangle extends GeoGraph {
private double height;
private double width;
public Rectangle() {
}
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
public double getArea() {
return height * width;
}
public double getPerimeter() {
return 2 * (height + width);
}
@Override
public String toString() {
return "the area of rectangle is:" + getArea();
}
public int compareTo(GeoGraph o) {
if (getArea() - o.getArea() > 0) {
return 1;
} else if (getArea() - o.getArea() < 0) {
return -1;
} else
return 0;
}
public void procColor(GeoGraph o) {
int color = (int) getArea();
while (color >= 100) {
color /= 10;
}
this.color = color;
}
}
4,SemiCircle类
package InterGeo;
public class SemiCircle extends GeoGraph {
public double radius;
public SemiCircle() {
}
public SemiCircle(double radius) {
this.radius = radius;
}
public double getArea() {
return 0.5 * radius * radius * Math.PI;
}
public double getPerimeter() {
return radius * Math.PI;
}
@Override
public String toString() {
return "the area of SemiCircle is:" + getArea();
}
public int compareTo(GeoGraph o) {
if (getArea() - o.getArea() > 0) {
return 1;
} else if (getArea() - o.getArea() < 0) {
return -1;
} else
return 0;
}
public void procColor(GeoGraph o) {
int color = (int) getArea();
while (color >= 100) {
color /= 10;
}
this.color = color;
}
}
5,Triangle类
package InterGeo;
public class Triangle extends GeoGraph {
private double a, b, c;
public Triangle() {
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return a + b + c;
}
@Override
public String toString() {
return "the area of Triangle is:" + getArea();
}
public int compareTo(GeoGraph o) {
if (getArea() - o.getArea() > 0) {
return 1;
} else if (getArea() - o.getArea() < 0) {
return -1;
} else
return 0;
}
public void procColor(GeoGraph o) {
int color = (int) getArea();
while (color >= 100) {
color /= 10;
}
this.color = color;
}
}
6,Oval类
package InterGeo;
public class Oval extends GeoGraph {
private double a, b;
public Oval() {
}
public Oval(double a, double b) {
this.a = a;
this.b = b;
}
public double getArea() {
return Math.PI * a * b;
}
public double getPerimeter() {
return 2 * Math.PI * b + 4 * (a - b);
}
@Override
public String toString() {
return "the area of Oval is:" + getArea();
}
public int compareTo(GeoGraph o) {
if (getArea() - o.getArea() > 0) {
return 1;
} else if (getArea() - o.getArea() < 0) {
return -1;
} else
return 0;
}
public void procColor(GeoGraph o) {
int color = (int) getArea();
while (color >= 100) {
color /= 10;
}
this.color = color;
}
}
7,Trapezoid类
package InterGeo;
public class Trapezoid extends GeoGraph {
private double a,b,c,d,height;//上:a,下:b,左:c,右:d
public Trapezoid()
{
}
public Trapezoid(double a,double b,double c,double d,double height)
{
this.a=a;this.b=b;this.c=c;this.d=d;this.height=height;
}
public double getArea()
{
return (a+b)*height/2;
}
public double getPerimeter()
{
return a+b+c+d;
}
@Override
public String toString()
{
return "the area of Trapezoid is:"+getArea();
}
public int compareTo(GeoGraph o) {
if(getArea()-o.getArea()>0) {
return 1;
}
else if(getArea()-o.getArea()<0) {
return -1;
}
else
return 0;
}
public void procColor(GeoGraph o) {
int color = (int)getArea();
while(color>=100)
{
color/=10;
}
this.color=color;
}
}
8,主类:
package InterGeo;
public class GeoArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
GeoGraph[] geo = new GeoGraph[] { new Circle(3), new SemiCircle(3), new Rectangle(2, 3), new Triangle(3, 3, 3),
new Oval(2, 3), new Trapezoid(2, 2, 3, 3, 3) };
java.util.Arrays.sort(geo);
for (GeoGraph obj : geo) {
System.out.println(obj);
}
}
}
9,运行截图: