(1)创建一个包,包名为:night.sunday
(2)创建矩形Rectangle类,添加double类型属性longth、width,分别表示长和宽。
(3)该类有两个构造方法:一个无参数;一个含有两个参数,用来给属性赋值。
(4)在Rectangle类中添加两个方法getGirth计算矩形周长、getArea计算面积。
(5)创建一个长方体类Cub,他有两个属性,一个是Rectangle类型,表示长方体的一个面,一个是double类型height,表示高。
(6)为长方体类Cub写一个带两个参数的构造方法。
(7)在Cub类中有一个方法getVolme用来计算长方体的体积。
(8)创建一个主类TestMain,在主类中调用getGirth方法和getArea方法分别计算矩形周长和面积,并打印输出。
(9)创建一个长方体类的对象object,调用getVolme方法计算该长方体的体积,并打印输出。
public class Rectangle {
private double width;
private double height;
Rectangle (double _width, double _height) {
this.width = _width;
this.height = _height;
}
Rectangle () {
}
public double computArea() {
return width*height;
}
public double computCircum() {
return (width + height)*2;
}
}
public class RectangleTest {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle (30, 40);
// 计算面积
double area = rectangle.computArea();
System.out.println("面积为:"+ area);
// 计算周长
double circum= rectangle.computCircum();
System.out.println("周长为:"+ circum);
}
}
public class TestApplication {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(3,4);
System.out.println(rectangle.getGirth());
System.out.println(rectangle.getArea());
Cub cub = new Cub(rectangle,5);
System.out.println(cub.getVolume());
}
}
class Rectangle{
private double length;
private double width;
public Rectangle() {
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getGirth(){
return 2*(length+width);
}
public double getArea(){
return length*width;
}
}
class Cub{
private Rectangle rectangle;
private double height;
public Cub(Rectangle rectangle, double height) {
this.rectangle = rectangle;
this.height = height;
}
public double getVolume(){
return height*rectangle.getArea();
}
}