1. 设计图形(Shape)类及其子类(Circle、Rectangle、Triangle) 1) Shape属性:Point p 代表图形中心位置,color颜色 2) Circle

Point:

public class Point {
	private int x;
	private int y;
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public Point() {
		super();
	}

}

Shape:

public class Shape {
	private Point p;
	private String color;
	
	public double calcArea() {
		return 0;
	}
    
    public double calcPerimeter() {
		return 0;
	}
    
	public Shape() {
		super();
	}
	public Point getP() {
		return p;
	}
	public void setP(Point p) {
		this.p = p;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}

}

Circle :

public class Circle extends Shape {
	private int radius;


	public Circle() {
		super();
	}
	
	 public Circle(int radius) {
		super();
		this.radius = radius;
	}


	public double calcArea() {
	        return Math.PI * radius * radius;
	  }
	 
	 public double calcPerimeter() {
	        return 2 * Math.PI * radius;
	  }
	 
	public int getR() {
		return radius;
	}


	public void setR(int r) {
		this.radius = r;
	}

}

Rectangle:

public class Rectangle extends Shape {


	private int length;
	private int width;


	public Rectangle() {
		super();
	}
	
	public Rectangle(int length, int width) {
		super();
		this.length = length;
		this.width = width;
	}


	public double calcArea() {
        return length * width;
	}
 
	public double calcPerimeter() {
        return 2 *( length + width);
	}

	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}

}

Triangle:

public class Triangle extends Shape {


	private int length;
	private int height;
	
	public Triangle() {
		super();
	}
	
	public Triangle(int length, int height) {
		super();
		this.length = length;
		this.height = height;
	}
	public double calcArea() {
		return  (length * height)/2;
	}
    
    public double calcPerimeter() {
		return length +2 *Math.sqrt(((length/2)*(length/2))+(height* height));
	}
	
	public int getLength() {
		return length;
	}


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


	public int getHeight() {
		return height;
	}


	public void setHeight(int height) {
		this.height = height;
	}

}

测试:Test

public class Testlianxi1 {


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Shape s = new Shape();


		Circle c = new Circle(5);
		System.out.println("圆的面积为:" +c.calcArea());
		System.out.println("圆的周长为:" +c.calcPerimeter());
		
		Rectangle r = new Rectangle(5, 6);
		System.out.println("矩形的面积为:" +r.calcArea());
		System.out.println("矩形的周长为:" +r.calcPerimeter());
		
		Triangle t = new Triangle(6, 4);
		System.out.println("等腰三角形的面积为:" +t.calcArea());
		System.out.println("等腰三角形的周长为:" +t.calcPerimeter());
	}
}

  • 16
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值