继承与多态-3

(继承与多态上机与课堂同题)设计一个Shape类,该类无数据成员,函数成员:getArea()、getPerimeter()分别获得图形的面积(0)、周长(0),由此派生Circle类、Rectangle类并重写上述2个方法。然后设计实现GeoArrayList有序图形列表类(升序)数据成员自定(可以使用ArrayList<>),该类可以实现以ArryLsit<Shape>为参数的构造方法及无参构造方法,向GeoArrayList添加对象方法insertGeoShape(Shape)(要求加入后不影响原有升序关系)、reMove(Sahpe)方法删除图形对象、getIndex(Shape)得到对象在列表的位置、get(index)读指定位置的对象、Size()返回列表长度、Clear()清空列表、Contains(Shape)判断图形是否在列表中、displayGeoList()依次显示列表中的所有对象的面积及对象的类别名称,GeoArrayList列表的升序以面积大小判断。设计主类:定义2个数组一个圆数组、一个矩形数组,然后将这两个数组添加到一个GeoArrayList对象中,然后显示列表中的所有对象

package shape;

public class Circle extends Shape {
	private static double PI=3.14159;
	private double radius;
	public Circle(double radius) {
		this.radius=radius;
	}
	public double getArea() {
		return PI*Math.pow(radius, 2);
	}
	public double getPerimeter() {
		return 2*PI*radius;
	}
}
package shape;

public class Rectangle extends Shape {
	private double edge;
	public Rectangle(double edge) {
		this.edge=edge;
	}
	public double getArea() {
		return Math.pow(edge, 2);
	}
	public double getPerimeter() {
		return 4*edge;
	}
}
package shape;

public class Shape {
	public double getArea() {
		return 0;
	}
	public double getPerimeter() {
		return 0;
	}
}
package shape;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class GeoArrayList {
	 private ArrayList<Shape> arrayList=new ArrayList<>();
	 public GeoArrayList(ArrayList<Shape> arrayList) {
		 this.arrayList=arrayList;
		 Collections.sort(arrayList,new Comparator<Shape>() {
			 public int compare(Shape a,Shape b) {
				 return a.getArea()>b.getArea()?1:-1;
			 	}
			 });
	 }
	 public GeoArrayList() {
		 
	 }
	 public void insertGeoShape(Shape s) {
		 arrayList.add(s);
		 Collections.sort(arrayList,new Comparator<Shape>() {
			 public int compare(Shape a,Shape b) {
				 return a.getArea()>b.getArea()?1:-1;
			 }
		});
	 }
	 public void reMove(Shape s) {
		 arrayList.remove(s);
	 }
	 public int getIndex(Shape s) {
		return arrayList.indexOf(s);
	 }
	 public Shape get(int index) {
		 return arrayList.get(index);
	 }
	 public int Size() {
		 return arrayList.size();
	 }
	 public void Clear() {
		 arrayList.clear();
	 }
	 public Boolean Contains(Shape s) {
		 if(arrayList.contains(s))
			 return true;
		 else {
			return false;
		}
	 }
	 public void displayGeoList() {
		 for(Shape s:arrayList) {
			 System.out.println("Area:"+s.getArea()+", Name:"+s.getClass().getName());
		 }
	 }
}
package shape;

public class testGeoArrayList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Circle[] circle= {new Circle(1),new Circle(2),new Circle(3)};
		Rectangle[] rectangles= {new Rectangle(1),new Rectangle(2),new Rectangle(3)};
		GeoArrayList gList=new GeoArrayList();
		for(Circle c:circle) {
			gList.insertGeoShape(c);
		}
		for(Rectangle r:rectangles) {
			gList.insertGeoShape(r);
		}
		gList.displayGeoList();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值