직육면체(java)
3211 단어 자바
제목 설명
(1)定义矩形类Rectangle :这个类中定义了private的属性length、width,2个构造方法Rectangle()和Rectangle(double length,double width),另外还有如下成员方法(getWidth()、getLength()、setWidth(…)、setLength(…)、perimeter()、 area())分别用来获取属性、设置属性、求周长和面积,还有一个方法showInfo()按“长:XXXX,宽:XXXX”的格式输出长方形的长和宽,小数点后面保持两位。 (2)创建这个类的子类长方体Cuboid ,增加private的hight属性;另外,它包括2个构造方法,其中有一个使用super(…)初始化柱体底面的矩形属性;还包括计算长方体的表面积和体积的方法surfaceArea()、volume(),要求它们的方法体中调用Rectangle的 perimeter()或 area()方;提供showInfo()按“高:XXXX,长:XXXX,宽:XXXX”的方式输出长方体的信息,小数点后面同样只保持2位。 (3)再定义一个主类,输入三个double类型的数表示长宽高,用它们去创建一个长方体对象,然后再由该对象调用相应的方法输出长方体的高长宽、表面积和体积。
输入样例
1.0 2.0 3.0
输出样例
高:3.00,长:1.00,宽:2.00 表面积:22.00,体积:6.00
import java.util .*;
class Rectangle
{
private double length=2;
private double width=3;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public Rectangle (double length,double width)
{
//super();
System.out.println(length+" "+width);
this.setLength(length);
this.setWidth(width);
//System.out.println("Rectangle (x,y)~");
System.out.printf("%.2f %.2f
", this.length,this.width);
}
public Rectangle ()
{
//System.out.println("Rectangle ()~");
}
public double perimeter() {
return length*width;
}
public void show(double width,double height, double length)
{
System.out.printf(" :%.2f, :%.2f, :%.2f
",height,this.length,this.width);
}
}
class Couboid extends Rectangle
{
private double hight;
public double surfaceArea;
public double volum;
public Couboid(double length,double width,double height) {
this.hight=height;
super.setWidth(width);
super.setLength(length);
}
public Couboid(double hight)
{
this.hight=hight;
}
public void surfaceArea()
{
surfaceArea=(super.getLength()*super.getWidth())*2+(hight*super.getLength())*2+(hight*super.getWidth())*2;
//System.out.printf(" :%.2f",surfaceArea);
}
public void volume()
{
//System.out.printf("%.2f %.2f
", super.getLength(),super.getWidth());
volum=super.getLength()*super.getWidth()*hight;
super.show(super.getWidth(),hight,super.getLength());
}
public double perimeter() {
return hight*getWidth()*getLength();
}
}
public class Main {
public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
double chang,kuan,gao;
chang=in.nextDouble();
kuan=in.nextDouble();
gao=in.nextDouble();
//System.out.printf("%.2f %.2f %.2f
",chang,kuan,gao);
Couboid st=new Couboid(chang,kuan,gao);
Rectangle r = new Rectangle();
st.volume();
st.surfaceArea();
System.out.printf(" :%.2f, :%.2f",st.surfaceArea,st.volum);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.