编程创建一个Rectangle类,在类中:定义两个私有变量length和width表示长、宽;定义一个方法setDemo(...)对长和宽进行初始化,当长或者宽<0时,......

编程创建一个Rectangle类,在类中:
(1)定义两个私有变量length和width表示长、宽;
(2)定义一个方法setDemo(...)对长和宽进行初始化,当长或者宽<0时,给出错误提示;
(3)定义一个方法area()求面积;
创建主类Test,要求在主类主方法中,创建Rectangle类的对象,求给定尺寸的长方形的面积(假设长、宽分别从键盘输入)

public class Rectangle {
    private Integer length;

    private Integer width;

    public void setDemo(Integer length, Integer width){
        if(length < 0){
            throw new RuntimeException("length 不能小于0");
        }

        if(width < 0){
            throw new RuntimeException("width 不能小于0");
        }

        this.length = length;
        this.width = width;
    }

    public Integer arer(){
        return length * width;
    }
}
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入length:");
        int length = scanner.nextInt();
        System.out.println("请输入width:");
        int width = scanner.nextInt();
        Rectangle rectangle = new Rectangle();
        rectangle.setDemo(length,width);
        System.out.println(rectangle.arer());
    }
}