java编_用java编写

展开全部

Java程序:public class Main {

public static void main(String[] args){

Shape p = null;

p = new Point(4, 4);

((Point)p).displayPosition();

p = new Circle(2, new Point());

System.out.println("圆的面积:" + p.area());

p = new Cylinder(2, 5, new Point());

System.out.println("圆柱e69da5e887aa3231313335323631343130323136353331333363373136体的面积:" + p.area());

System.out.println("圆柱体的体积:" + p.volume());

}

}

/**

* 形状类:抽象类

* @author developer

* @version 2017.05.18

*/

abstract class Shape {

/**

* 返回几何形状的面积

* @return 几何形状的面积

*/

abstract double area();

/**

* 返回几何形状的体积

* @return 几何形状的体积

*/

abstract double volume();

}

/**

* 点类

* @author developer

* @version 2017.05.18

*/

class Point extends Shape {

protected int x;

protected int y;

public Point() {

this.x = 0;

this.y = 0;

}

public Point(int x, int y) {

this.x = x;

this.y = y;

}

public void displayPosition() {

System.out.printf("点的当前位置:(%d, %d)\n", this.x, this. y);

}

@Override

double area() {

return 0;

}

@Override

double volume() {

return 0;

}

}

/**

* 圆类

* @author developer

* @version 2017.05.18

*/

class Circle extends Shape {

/**

* 半径

*/

protected double radius;

/**

* 圆心坐标

*/

protected Point center;

public Circle() {

this.radius = 0;

this.center = new Point();

}

public Circle(double radius, Point center) {

this.radius = radius;

this.center = new Point(center.x, center.y);

}

@Override

double area() {

return Math.PI * radius * radius;

}

@Override

double volume() {

return 0;

}

}

/**

* 圆柱体类

* @author developer

* @version 2017.05.18

*/

class Cylinder extends Shape {

/**

* 半径

*/

protected double radius;

/**

* 高

*/

protected double height;

/**

* 圆心坐标

*/

protected Point center;

public Cylinder() {

this.radius = 0;

this.height = 0;

this.center = new Point();

}

public Cylinder(double radius, double height, Point center) {

this.radius = radius;

this.height = height;

this.center = new Point(center.x, center.y);

}

@Override

double area() {

return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * height;

}

@Override

double volume() {

return Math.PI * radius * radius * height;

}

}

运行测试:点的当前位置:(4, 4)

圆的面积:12.566370614359172

圆柱体的面积:87.96459430051421

圆柱体的体积:62.83185307179586

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值