仅提供简答题
四.简答题(共1题,5.0分)
1 定义长方形类Square,包括私有属性x,y,表示长和宽。
添加计算面积和周长的两个方法(该方法有返回值)。
添加没有返回值的info()方法,输出该长方形的长和宽。
在Main()方法中实例化两个对象,测试。
}
- Square.java
package hati.lsz.ch02;
public class Square {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int mianji() {
return x*y;
}
public int zhouchang(){
return (x+y)*2;
}
public void info() {
System.out.println("周长:"+zhouchang()+"面积:"+mianji());
}
}
- Base.java
package hati.lsz.ch02;
public class Base {
// 1
// 定义长方形类Square,包括私有属性x,y,表示长和宽。
//
// 添加计算面积和周长的两个方法(该方法有返回值)。
//
// 添加没有返回值的info()方法,输出该长方形的长和宽。
//
// 在Main()方法中实例化两个对象,测试。
public static void main(String[] args) {
Square square1 = new Square();
square1.setX(5);
square1.setY(9);
Square square2 = new Square();
square2.setX(8);
square2.setY(9);
square1.info();
square2.info();
}
}
运行效果: