学习内容:以圆类 Circle 及立体图形类 Solid 为基础设计圆锥类 Cone
代码示例:
import java.util.Scanner;
class Point4{
private double x;
private double y;
public Point4(double x,double y) {
this.x=x;
this.y=y;
System.out.println("Point Constructor run");
}
public void setX(double x) {//设置x坐标
this.x=x;
}
public double getX(){//返回x坐标
return x;
}
public void setY(double y) {//设置y坐标
this.y=y;
}
public double getY(){//返回y坐标
return y;
}
public void show() {//显示点的坐标
System.out.println("Point(X="+x+",Y="+y+")");
}
}
abstract class Plane4 extends Point4{
public Plane4(double x, double y) {
super(x, y);
}
double length() {
return 0;
}
double area() {
return 0;
}
}
class Circle2 extends Plane2 {
public Circle2(double x, double y,double radius) {
super(x,y);
this.x=x;
this.y=y;
this.radius=radius;
System.out.println("Circle Constructorrun");
}
private double x;
private double y;
final double PI=3.14159;
private double radius;
public void setX(double x) {//设置x坐标
this.x=x;
}
public final double getX(){//返回x坐标
return x;
}
public void setY(double y) {//设置y坐标
this.y=y;
}
public final double getY(){//返回y坐标
return y;
}
public void setR(double r) {
radius=r;
}
public final double getR() {
return radius;
}
public final void show() {
System.out.println("Circle(Point("+x+","+y+"),Radius="+radius+")");
}
public final double area() {
double area=PI*radius*radius;
System.out.println("Area="+area);
return 0;
}
public final double length() {
double length=2*PI*radius;
System.out.println("Length="+length);
return 0;
}
}
abstract class Solid1 extends Circle{
private double x;
private double y;
private double radius;
public Solid1(double x, double y, double radius) {
super(x, y, radius);
this.x=x;
this.y=y;
this.radius=radius;
}
public double volume() {//计算体积
return 0;
}
public double s_Area() {//计算表面积
return 0;
}
}
public class Cone extends Solid1{
private double x;
private double y;
private double radius;
private double height;
public void setX(double x) {//设置x坐标
this.x=x;
}
public void setY(double y) {//设置y坐标
this.y=y;
}
public void setR(double r) {
radius=r;
}
public void setH(double height) {
this.height=height;
}
public double getH() {
return height;
}
public Cone(double x, double y, double radius,double height) {
super(x, y, radius);
this.x=x;
this.y=y;
this.radius=radius;
this.height=height;
System.out.println("Cone Constructorrun");
}
public final void show() {
System.out.println("Cone(Circle(Point("+x+","+y+"),Radius="+radius+"),Height="+height+")");
}
public final double s_Area() {
double l=Math.sqrt(radius*radius+height*height);
double sarea=PI*radius*(radius+l);
System.out.println("s_Area="+sarea);
return 0;
}
public final double volume() {
double volume=(1.0/3.0)*PI*radius*radius*height;
System.out.println("Volume="+volume);
return 0;
}
public static void main(String[] args) {
double h;
System.out.println("请输入h:");
Scanner sc=new Scanner(System.in);
h=sc.nextDouble();
Cone co1=new Cone(1,2,3,4);
Cone co2=co1;
co1.show();
System.out.println();
co1.area();
co1.length();
co1.s_Area();
co1.volume();
System.out.println("………………………………………………………………………………..");
co2.setH(h);
co2.show();
System.out.println();
co2.area();
co2.length();
co2.s_Area();
co2.volume();
}
}
运行截图:
明天任务:**异常处理
**
手机扫一扫
移动阅读更方便
你可能感兴趣的文章