public class Rectangle{
private int length;
private int width;
public Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public void set(int length,int width){
this.length=length;
this.width=width;
}
public void get(){
System.out.println("此长方形的长为"+length+",宽为"+width);
}
public int getLength(){
return length;
}
public int getWidth(){
return width;
}
public int calculatePerimeter(){
return 2*(length+width);
}
public int calculateArea(){
return (length*width);
}
public void print(){
System.out.println("此长方形的周长为"+calculatePerimeter()+",面积为"+calculateArea());
}
public String toString(){
String str;
str = "The perimetre of the rectangle is: " + calculatePerimeter() +
", Area is: "+calculateArea();
return str;
}
public static void main(String [ ] args){
Rectangle myrectangle=new Rectangle(15,10);
Rectangle myrec02=new Rectangle(12,36);
myrectangle.get();
myrectangle.print();
myrectangle.set(20,15);
System.out.println("长方形的长是"+myrectangle.getLength()+
",宽是"+ myrectangle .getWidth());
myrectangle.print();
System.out.println(myrectangle);
System.out.println(myrec02);
}
}
/**
public class HelloWorld {
public static void main(String[] args) {
System.out.println("我在编程中国学JAVA");
System.out.println();
// 练习一下循环的使用
for (int i=1; i<=20; i++) {
System.out.printf("我爱编程中国 %d 次\n", i);
}
System.out.printf("\n\n绘制一个心形图案:");
float x, y;
for (y = (float)1.5; y > -1.5; y -= 0.1) {
for (x = (float)-1.5; x < 1.5; x += 0.05) {
float a = x * x + y * y - 1;
if ((a * a * a - x * x * y * y * y) <= 0.0) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
*/