직육면체(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); } }

좋은 웹페이지 즐겨찾기