今天学了什么
用点类Point和圆类Circle设计圆锥类Cone
Point类
package cone;
public class Point {
protected double x,y;
Point(double x,double y){
this.x=x;
this.y=y;
}
public void setx(double x) {
this.x=x;
}
public void sety(double y) {
this.y=y;
}
public double getx() {
return x;
}
public double gety() {
return y;
}
public void showPoint() {
System.out.println("x="+x+",y="+y);
}
}
Circle类
package cone;
public class Circle extends Point {
protected final double PI=3.14159;
protected double radius;
public Circle(double x,double y,double radius) {
super(x,y);
this.radius=radius;
}
public void setr(double r) {
this.radius=r;
}
public double getr() {
return radius;
}
public double area() {
return PI*radius*radius;
}
public double length() {
return 2*PI*radius;
}
public void showCircle() {
System.out.println("radius="+radius+",length="+length()+",area="+area());
}
}
Cone类
package cone;
public class Cone extends Circle {
private double h;
public Cone(double x, double y, double radius, double h) {
super(x, y, radius);
this.h=h;
}
public void seth(double h) {
this.h=h;
}
public double geth() {
return h;
}
public double sarea() {
double l = Math.sqrt (radius*radius + h*h);
double s = PI*l*radius + PI*radius*radius;
return s;
}
public double volume() {
return (h*area())/3;
}
public void showCone() {
System.out.println("h="+h+",sarea="+sarea()+",volume="+volume());
}
public static void main(String[] args) {
Cone c = new Cone(0, 0, 0, 0);
c.setx(1);
c.sety(2);
c.setr(3);
c.seth(4);
c.showPoint();
c.showCircle();
c.showCone();
}
}
运行截图
遇到的问题
sqrt()方法要Math.sqrt()这样写
明天计划
练习面向对象习题