作业三-图形类

## 作业1.实现抽象图形类
package com.xpm;
/**
 * 继承会报错,因为必须继承基类的抽象方法,或者自己定义成抽象类
 * @author 604347112
 *
 */## 圆形类## 
public class Circle extends Graph{
	private double raidus;
	
	public Circle(double d) {
		// TODO Auto-generated constructor stub
		this.raidus=d;
	}

	public double getRaidus() {
		return raidus;
	}

	public void setRaidus(double raidus) {
		this.raidus = raidus;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return Math.PI*raidus*raidus;
	}

	@Override
	public double girth() {
		// TODO Auto-generated method stub
		return Math.PI*raidus*2;
	}

}
## 长方形类
package com.xpm;

public class Rectangle extends Graph{
	private double length;
	private double width;
	public Rectangle(double d, double e) {
		this.length=d;
		this.width=e;
	}

	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;
	}

	
	
	@Override
	//计算面积
	public double area() {
		// TODO Auto-generated method stub
		return length*width;
	}

	@Override
	public double girth() {
		// TODO Auto-generated method stub
		return 2*(length+width);
	}

}
## 正方形类
package com.xpm;

public class Square extends Graph{
	private double length;
	
	public Square(double d) {
		this.length=d;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return length*length;
	}

	@Override
	public double girth() {
		// TODO Auto-generated method stub
		return length*4;
	}

}






## 作业2设计Comparable接口比较对象
package com.xpm;
/**
 * 打印语句一般在main方法里面
 * @author 604347112
 *
 */
public interface Comparable {
	public int compareTo(Graph graph);
}


package com.xpm;

public class TestGraphs {
	public static void main(String[] args) {
		Graph[] gra=new Graph[10];
		for (int i = 0; i < 3; i++){
			Circle circle=new Circle(2.0+i);
			gra[i]=circle;
		}
		for (int i = 3; i < 7; i++) {
			Square square=new Square(3.0+i);
			gra[i]=square;
		}
		for (int i = 7; i < 10; i++) {
			Rectangle rectangle=new Rectangle(3.0+i, 4.0+i);
			gra[i]=rectangle;
		}
		Graph max=gra[0];
		for (int i=0;i<gra.length;i++){
			if (gra[i].area()>max.area()) {
				max=gra[i];
			}
		}
		System.out.println(max.area());
	}
}




public abstract class Graph implements Comparable{
	public abstract double area();
	public abstract double girth();
	@Override
	public int compareTo(Graph graph) {
		if(this.area()>graph.area()){
			return 1;
		}else if (this.area()<graph.area()) {
			return -1;
		}else {
			return 0;	
		}
	}
	
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值