public Rectangle() { }
public Rectangle(double width, double height , String color) { this.width = width; this.height = height; this.color = color; }
public double getWidth() { return width; }
public void setWidth(double width) { this.width = width; }
public double getHeight() { return height; }
public void setHeight(double height) { this. height = height; }
public static String getColor() { return color; }
public static void setColor(String color) { this.color = color; }
public double findArea() { return width*height; }
public static void main(String[] args) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle();
r1.setWidth(2.0); r1.setHeight(3.0); r1.setColor(“红色”); r2.setWidth(5.0); r2.setHeight(4.0); r2.setColor(“黄色”);
System.out.println(r1.getWidth()); System.out.println(r1.getHeight()); System.out.println(r1.getColor()); System.out.println(r1.findArea()); System.out.println(r2.getWidth()); System.out.println(r2.getHeight());
}
}
System.out.println(r2.getColor()); System.out.println(r2.findArea());
19.写一个名叫Fan的类模拟风扇,属性为speed、on、radius和color。要求为属性提供访问器方法,并提供方法toString,反活包含类中所有属性值的字符串。假设风扇有三种固定速度,用常数1、2、3表示慢、中、快。
写一个用户程序测试Fan类。在用户程序中创建一个Fan对象,设置最大速度为10,黄色、打开状态。调用toString方法显示该对象。
class Fan {
public static int SLOW = 1; public static int MEDIUM = 2; public static int FAST = 3;
private int speed = SLOW; private boolean on = false; private double radius = 5; private String color = \white\
public Fan () { }
public int getSpeed() {
return speed; }
public void setSpeed(int speed) {
this.speed = speed; }
public boolean isOn() {
return on; }
public void setOn(boolean trueOrFalse) {
this.on = on; }
public double getRadius() {
return radius; }
public void setRadius(double radius) {
this.radius = radius; }
public String getColor() {
return color; }
public void setColor(String color) {
this.color = color; }
public String toString() {
return \ + \
+ \ + \ } }
public class Test {
public static void main(String[] args) {
Fan fan = new Fan ();
fan.setSpeed(Fan.MEDIUM); fan.setRadius(5.5); fan.setOn(true); fan.setColor(\
System.out.println(fan.toString()); } }
20.写一个名为Account的类模拟帐户。该类包含帐户、余额、年利率等属性,包含提款、存款方法和访问器方法,类结构如下表。
写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率为4.5%的Account对象。使用withdraw方法提款2500,使用deposit方法存款3000,并打印余额。
Account private int id private double balance private double annualInterestRate public Account() public Account(int id, double balance , double annualInterestRate) public int getId() public double getBalance() public double getAnnualInterestRate () public void setId(int id) public void setBalance(double balance) public void setAnnualInterestRate (double annualInterestRate) public void withdraw(double amount) public void deposit(double amount)
public class Test {
public static void main(String[] args) {
Account account = new Account (1122, 20000,0.045);
account.withdraw(2500);
System.out.println(\
account.deposit(3000);
System.out.println(\
} }
class Account {
private int id
private double balance
private double annualInterestRate
public Account () { id=0; balance = 0; annualInterestRate = 0;
}
public Account(int id, double balance , double annualInterestRate) { this.id = id; this.balance = balance; this. annualInterestRate = annualInterestRate; }
public String getId() {
return id; }
public String getBalance () {
return balance; }
public double getAnnualInterestRate () {
return annualInterestRate;

