第五章题库答案

2026/4/23 22:34:27

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;


第五章题库答案.doc 将本文的Word文档下载到电脑
搜索更多关于: 第五章题库答案 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219