Java入门程序---JavaBean
需求:
按照课堂上所讲要求定义一个长方形类,并定义其求周长和面积的方法,然后定义一个测试类进行测试。
package com.company;
public class Test {
public static void main(String[] args) {
int length;
int width;
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长");
int length = sc.nextInt();
System.out.println("请输入长方形的宽");
int width =sc.nextInt();
Rectangle rec=new Rectangle();
rec.setLength(length);
rec.setWidth(width);
System.out.println("周长是:"+rec.getPerimeter());
System.out.println("面积是:"+rec.getArea());
}
}
class Rectangle{
private int length;
private int width;
public Rectangle(){}
public void setLength(int length){
this.length=length;
}
public void setWidth(int width){
this.width=width;
}
public int getPerimeter(){
return int (length + width)*2;
}
public void getArea(){
return int width*length;
}
}
你數過天上的星星嗎

浙公网安备 33010602011771号