按要求编写一个Java应用程序:
(1)定义一个类Rectangle,描述一个矩形,包含有长、宽两种属性(用length和width表示)和计算面积的方法(方面名定义为area)。d
(2)编写一个类Cuboid,继承自矩形类,同时该类描述长方体,具有长、宽、高(height)属性和计算体积的方法(方法名为volume)。
(3)编写一个测试类(类名为Test),对以上两个类进行测试,创建一个长方体c1,定义其长、宽、高分别为3.1、4.2,和5.3,输出其底面积和体积。
import java.util.Scanner;
public class Test_5_1Test {
public static class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
Test_5_1Coboid co = new Test_5_1Coboid(a,b,c);
System.out.printf("%.2f",co.Area());
System.out.println();
System.out.printf("%.2f",co.volume());
}
}
}
public class Test_5_1Rectangle {
private double length;
private double width;
private double area;
Test_5_1Rectangle(double length, double width){
this.length = length;
this.width = width;
}
public double Area(){
area = length*width;
return area;
}
}
public class Test_5_1Coboid extends Test_5_1Rectangle {
private double height;
Test_5_1Coboid(double length, double width, double height){
super(length,width);
this.height = height;
}
public double volume(){
return Area()*height;
}
}