【发布时间】:2021-02-18 11:06:36
【问题描述】:
这些是说明... 编写一个类来计算行星的测量值。
-
用这些方法编写一个类。
- 以英里为单位初始化行星圆周的构造函数或 公里
- 计算行星测量值的方法 - 圆、面积、直径、半径、曲面、面积、体积
-
使用你的类编写一个程序 - 询问用户行星的周长 - 创建一个行星对象 - 用逗号和小数打印行星的测量值 - 确保小数对齐。
package com.company;
import java.util.Scanner;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) {
Main planet = new Main();
Scanner in = new Scanner(System.in);
classP cP = new classP();
//cP.getRadius();
//cP.getCirleArea();
//cP.getcirc();
}
}
package com.company;
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;
public class classP {
private double circ, radius, circleArea, surfArea, volume;
//public classP radius;
//public double circ;
//public double circleArea;
public Main planet = new Main();
public classP() {
this.circ = circ;
this.radius = radius;
this.circleArea = circleArea;
this.surfArea = surfArea;
this.volume = volume;
Main mainClass = new Main();
Scanner in = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("###,###,###.##");
System.out.print("What is the planet's circumference in miles? ");
double circ = in.nextDouble();
double radius = circ / (Math.PI * 2);
double circleArea = Math.PI * Math.pow(radius, 2);
System.out.printf("%nThe radius of the planet is: " + "%,12.1f\n" , radius);
System.out.printf("%nThe area of the planet is: " + "%,12.1f\n", circleArea);
double diameter = circ / Math.PI;
System.out.printf("%nThe diameter of the planet is: " + "%,12.1f\n", diameter);
double surfArea = 4 * Math.PI * Math.pow(radius, 2);
System.out.printf("%nThe surface area of the planet is: " + "%,12.1f\n", surfArea);
double volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
System.out.printf("%nThe volume of the planet is: " + "%,12.1f\n", volume);
}
//public final double getcirc(){
// return this.circ;
//}
//public final classP getRadius(){
// return this.radius;
//}
//public final double getCirleArea() {
// return this.circleArea;
//}
}
这是我的输出,示例用户输入为:100
What is the planet's circumference in miles? 100
The radius of the planet is: 15.9
The radius of the planet is: 15.9
The area of the planet is: 795.8
The area of the planet is: 795.8
The diameter of the planet is: 31.8
The surface area of the planet is: 3,183.1
The volume of the planet is: 16,886.9
【问题讨论】:
标签: java