实验四类与对象(封装、继承、多态等机制的使用)

实验内容:

1、 编写一个名为TwoDimensionalShape的抽象类,拥有属性area和circumference表示面积和周长,以及抽象方法getArea()和getCircumference(),用于获取面积和周长。

2、 编写Printable接口,包括一个抽象方法printShapeInfo,用于输出图形信息。

3、 分别编写Rectangle、Triangle、Circular三个类,用于描述矩形、三角形和圆形,要求继承于TwoDimensionalShap类,并实现Printable接口,每个图形有各自的构造方法,矩形有length和width字段表示长和宽,三角形有base、hypotenus1和hypotenus2字段表示底和两条斜边,圆形有radius字段表示半径,按几何常识实现父类中所定义的抽象方法,printShapeInfo方法要求能够输出:

(1) 图形的类型(使用getClass方法)

(2) 图形的基本数据(使用自定义toString方法,输出图形的字段,输出示例:宽3.0, 长4.0)

4、 编写一个名为ShapeDetector的类,拥有三个重载的detectShape方法,分别为detectShape(Rectangle r)、detectShape(Triangle t)、detectShape(Circular c),分别输出所接收到的图形的类型。

5、 编写Test类,创建一个名为Shapes的TwoDimensionalShape型数组,并创建Rectangle、Triangle、Circular的对象存储在该数组中,用foreach循环遍历该数组,使用detectShape方法输出该对象的图形类型,用printShapeInfo方法输出该对象的图形信息,然后分别用getArea和getCircumference方法输出面积及周长。

6、(可选)给三个图形的构造方法加上验证功能,保证所输入三角形三边能够组成三角形,圆形半径、矩形的长和宽为正实数。

 

要求:程序文件放置在合适的包中,保证程序有良好的组织性,在编写过程中尽量保证程序的良好封装性。

 

附:三角形面积公式 p=(a+b+c)/2 s=√p(p-a)(p-b)(p-c)

 

 

public class test {
	public static void main(String[] args) {
		Rectangle r1 = new Rectangle(10.0, 20.0);
		Triangle t1 = new Triangle(3, 4, 5);
		Circular c1 = new Circular(10);
		ShapeDetector sd = new ShapeDetector();
		TwoDimensionalShape[] Shape = { r1, t1, c1 };
		for (TwoDimensionalShape tds : Shape) {
			if (tds instanceof Rectangle) {
				sd.detectShape((Rectangle) tds);
				((Rectangle) tds).printShapeInfo();
				;
			}
			if (tds instanceof Circular) {
				sd.detectShape((Circular) tds);
				((Circular) tds).printShapeInfo();
			}
			if (tds instanceof Triangle) {
				sd.detectShape((Triangle) tds);
				((Triangle) tds).printShapeInfo();
			}
			System.out.println("Area=" + tds.getArea());
			System.out.println("Circumference=" + tds.getCircumference()+"\n");
		}
	} 
}
 
abstract class TwoDimensionalShape {
 
	double area, circumference;
 
	public abstract double getArea();
 
	public abstract double getCircumference();
 
}
 
interface Printable {
	void printShapeInfo();
} 
class Rectangle extends TwoDimensionalShape implements Printable {
 
	double length, width;
 
	Rectangle(double l, double w) {
		this.length = l;
		this.width = w;
	} 
	public double getLength() {
		return length;
	} 
	public void setLength(double length) {
		this.length = length;
	}
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public double getArea() {
		return length * width;
	}
	public double getCircumference() {
		return 2 * (length + width);
	}
	public String toString() {
		return "length:" + length + " width:" + width;
	}
	public void printShapeInfo() {
		System.out.println("this is a " + getClass());
		System.out.println(toString());
	}
} 
class Triangle extends TwoDimensionalShape implements Printable {
 
	double base, hypotenus1, hypotenus2;
 
	Triangle(double b, double h1, double h2) {
		this.base = b;
		this.hypotenus1 = h1;
		this.hypotenus2 = h2;
	}
	public double getBase() {
		return base;
	}
	public void setBase(double base) {
		this.base = base;
	}
	public double getHypotenus1() {
		return hypotenus1;
	}
	public void setHypotenus1(double hypotenus1) {
		this.hypotenus1 = hypotenus1;
	}
	public double getHypotenus2() {
		return hypotenus2;
	}
	public void setHypotenus2(double hypotenus2) {
		this.hypotenus2 = hypotenus2;
	}
	public double getArea() {
		double p = (base + hypotenus1 + hypotenus2) / 2;
		double s = Math.sqrt(p * (p - base) * (p - hypotenus1) * (p - hypotenus2));
		return s;
	}
	public double getCircumference() {
		return base + hypotenus1 + hypotenus2;
	}
	public String toString() {
		return "base:" + base + " hypotenus1:" + hypotenus1 + " hypotenus2:" + hypotenus2;
	}
	public void printShapeInfo() {
		System.out.println("this is a " + getClass());
		System.out.println(toString());
	}
} 
class Circular extends TwoDimensionalShape implements Printable {
 
	double radius;
 
	Circular(double r) {
		this.radius = r;
	} 
	public double getRadius() {
		return radius;
	} 
	public void setRadius(double radius) {
		this.radius = radius;
	} 
	public double getArea() {
		return Math.PI * radius * radius;
	} 
	public double getCircumference() {
		return 2 * Math.PI * radius;
	} 
	public String toString() {
		return "radius:" + radius;
	} 
	public void printShapeInfo() {
		System.out.println("this is a " + getClass());
		System.out.println(toString());
	}
} 
class ShapeDetector {
	public void detectShape(Rectangle r) {
		System.out.println(r.getClass());
	} 
	public void detectShape(Triangle t) {
		System.out.println(t.getClass());
	} 
	public void detectShape(Circular c) {
		System.out.println(c.getClass());
	}
}

 

此代码中把所有的主类和接口均写在了一个文件中,方便贴出来,如有需要请在eclipse中分别创建文件分别写这些类