java程序设计教程(第六版)课后习题答案(6)

2021-02-21 12:19

public Sphere(){//构造方法:无参数

this.diameter = 1.0;

}

public Sphere(double d){ //构造方法:带一个参数

this.diameter = d;

}

public void setDiameter(double d) {//设置直径值的方法 this.diameter = d;

}

public double getDiameter(){//获取直径值的方法

return this.diameter;

}

public double volume(){//计算球的体积

return 4*Math.PI*Math.pow(this.diameter/2,3)/3; }

public double area(){//计算球的表面积

return 4*Math.PI*Math.pow(this.diameter/2,2);

}

public String toString(){

String out = "该球体的直径为:" + this.diameter + "\n" + "该球体的表面积为:" + this.area() + "\n" +

"该球体的体积为:" + this. volume();

return out;

}

}

//MultiSphere.java

import java.util.Scanner;

public class MultiSphere

{

public static void main(String[] args){

Scanner scan = new Scanner(System.in);

Sphere sphere1 = new Sphere();

Sphere sphere2 = new Sphere(3.5);

System.out.println("sphere1: " + sphere1 + "\n"); System.out.println("sphere2: " + sphere2 + "\n");

System.out.println("sphere1和sphere2分别调用无参构造方法" +

"和带一个参数的构造方法进行初始化。");

System.out.print("现在,请输入一个数作为球sphere1的直径值:");

sphere1.setDiameter(scan.nextDouble());

System.out.println("\n" + "更改过的sphere1: " + sphere1);

}


java程序设计教程(第六版)课后习题答案(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:三年级科学空气有重量

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: