class Rectangle{
private double length, width; //矩形的长、宽
Rectangle (double l, double w){ length =l; width =w; }
void introduce(){ System.out.println(“这是一个矩形!”); }
double area(){ return length * width ; }
}
请再写一个矩形柱体类Cylinder。要求:
- Cylinder继承rectangle,加入变量high (柱体的高);
- Cylinder的构造方法是设置柱体的长、宽、高;
- 重写方法introduce(),改为输出“这是一个矩形柱体!”;
- 写个求矩形柱体的体积的方法(利用继承的方法);
- 在主方法中以自己学号的个位数加1、十位数加1、十位数与个位数之和分别作为柱体的长、宽、高建立柱体对象,然后输出“这是一个矩形柱体!”和体积。
import java.util.*; class Rectangle{ private double length, width; //矩形的长、宽 Rectangle (double l,double w){ length =l; width =w; } void introduce(){ System.out.println("这是一个矩形!"); } double area(){ return length * width ; } } class Cylinder extends Rectangle{ double high; public Cylinder(double length,double width,double high) { super(length, width); this.high=high; } void introduce() { System.out.println("这是一个矩形柱体!"); } double tiji() { return area()*high; } } public class driver{ public static void main(String[] args) { System.out.println("输入你的学号个位数:"); Scanner sc=new Scanner(System.in); int x=sc.nextInt(); System.out.println("输入你的学号十位数:"); int y=sc.nextInt(); Cylinder cylinder=new Cylinder(x+1, y+1, x+y); cylinder.introduce(); double t=cylinder.tiji(); System.out.println("体积为:"+t); } }