java课后习题

上传人:第*** 文档编号:34207543 上传时间:2018-02-21 格式:DOC 页数:23 大小:157KB
返回 下载 相关 举报
java课后习题_第1页
第1页 / 共23页
java课后习题_第2页
第2页 / 共23页
java课后习题_第3页
第3页 / 共23页
java课后习题_第4页
第4页 / 共23页
java课后习题_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《java课后习题》由会员分享,可在线阅读,更多相关《java课后习题(23页珍藏版)》请在金锄头文库上搜索。

1、8.7 设计一个名为 Account 类,它包括:一个名为 id 的 int 类型私有帐户数据域(默认值 0)。一个名为 balance 的 double 类型私有帐户数据域(默认值 0)。一个名为 annualInterestRate 的 double 类型私有数据域存储当前利率(默认值 0)。假设所以的帐户都有相同的利率。一个名为 dateCreated 的 Date 类型私有数据域存储帐户的开户日期。一个能创建默认帐户的无参构造方法。一个能创建带特定 id 和初始余额的帐户的构造方法。Id balance annualInterstRate 的访问器和修改器。dateCreated 的访

2、问器。一个名为 getMonthlyInterestRate()的方法返回月利率。一个名为 withDraw 的方法从帐户提取特定数额。一个名 deposit 的方法向帐户存储特定的数额。画出该类的 UML 图。实现这个类。编写一个测试程序,创建一个帐户 ID 为 1122,余额为 20000 美元,年利率为 4.5%的 Account 对象。使用 withdraw 方法取款 2500 美元,使用 deposit 方法存款 3000 美元,然后打印余额,月利息以及这个帐户的开户日期。源程序:public class Account private int id; /账户号码private do

3、uble balance; /账户余额private double annualInterestRate; /当前利率private java.util.Date dateCreated = new java.util.Date(); /开户日期public Account()this.id = 0;this.balance = 0;this.annualInterestRate = 0;public Account(int id,int balance)this.id = id;this.balance =balance; public int getId()return this.id;p

4、ublic void steId(int id)this.id = id;public double getBalance()return this.balance;public void setBalance(double balance)this.balance = balance;public double getannualInterestRate()return this.annualInterestRate;public void setAnnualInterestRate(double annualInterestRate)this.annualInterestRate = an

5、nualInterestRate;public java.util.Date getDateCreated()return this.dateCreated; public double getMonthlyInterestRate()return this.annualInterestRate/12;public double withDraw(double count)this.balance -= count;return this.balance;public double deposit(double count)this.balance += count;return this.b

6、alance;public static void main(String args)Account account = new Account(1122,20000);account.setAnnualInterestRate(0.045);account.withDraw(2500);account.deposit(3000);System.out.println(余额:+account.getBalance();System.out.println(月利息: +account.getMonthlyInterestRate()*account.getBalance();System.out

7、.println(开户日期:+account.getDateCreated().toString();运行结果:余额:20500.0月利息:76.875开户日期:Mon Apr 16 20:54:13 CST 20128.8 设计一个名为 Fan 的类来表示一个风扇。这个类包括:三个名为 SLOW、MEDIUM 和 FAST 而值是 1、2 和 3 的常量表示风扇的速度。一个名为 speed 的 int 类型私有数据域表示风扇的速度(默认值 SLOW)。一个人名为 on 的 boolean 类型私有数据域表示风扇是否打开(默认值为 false)。一个名为 radius 的 double 类型私

8、有数据域表示风扇的半径(默认值 5)。一个名为 color 的 string 类型数据域表示风扇的颜色(默认值为 blue)。这四个数据域的访问器和修改器。一个创建默认风扇的无参构造方法。一个名为 toString()方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就会返回一个由“fan is off”和风扇颜色及半径组合成的字符串。画出该类的 UML 图。实现这个类。编写一个测试程序,创建两个 Fan 对象。将第一个对象设置为最大速度、半径 10、颜色为 yellow、状态为打开。将第二个对象设置为中等速度、半径为

9、 5、颜色为 blue、状态为关闭。通过调用它们的 toSrting 方法显示这些对象。源程序:public class Fan final static int SLOW = 1;final static int MEDIUM = 2;final static int FAST = 3;private int speed = SLOW;private boolean on = false;private double radius = 5;private String color = blue;public int getSpeed() return speed;public void se

10、tSpeed(int speed) this.speed = speed;public boolean isOn() return on;public void setOn(boolean on) 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 = colo

11、r;public Fan()public String toString()if(on=true)return 风扇速度:+this.getSpeed()+n+风扇半径:+this.getRadius()+n+风扇颜色:+this.getColor()+n;elsereturn fan is off+n+风扇半径:+this.getRadius()+n+风扇颜色:+this.getColor()+n;public static void main(String args)Fan fan = new Fan();fan.setColor(yello);fan.setRadius(10.0);fa

12、n.setSpeed(FAST);fan.setOn(true);Fan fan1 = new Fan();fan1.setColor(bule);fan1.setOn(false);fan1.setRadius(5.0);System.out.println(fan.toString();System.out.println(fan1.toString();运行结果:风扇速度:3风扇半径:10.0风扇颜色:yellofan is off风扇半径:5.0风扇颜色:bule8.9 在一个正 n 边形中,所以边的长度都相同,且所有角的度数都相同(即这个多边形是等边等角的)。设计一个名为 Regul

13、arPolygon 的类,该类包括:一个名为 int 型的私有数据域定义多边形的边数,默认值 3。一个名为 side 的 double 型私有数据域存储边的长度,默认值 1。一个名为 x 的 double 型私有数据域,它定义多边形中点的 x 坐标,默认值 0。一个名为 y 的 double 型私有数据域,它定义多边形中点的 y 坐标,默认值 0。一个创建带默认值的正多边形的无参构造方法。一个能创建带指定边数和边长度、中心在(0,0)的正多边形的构造方法。一个能创建带指定边数和边长度、中心在(x,y)的正多边形的构造方法。所有数据域的访问器和修改器。一个返回多边形周长的方法 getPerime

14、ter()。一个返回多边形面积的方法 getArea().计算多边形面积的公式是:面积=(n*s*s)/(4*tan(p/n)画出该类的UML图。实现这个类。编写一个测试程序,分别使用无参构造方法、RegularPolygon(6,4)和RegularPolygon(10,4,5.6,7.8)创建三个RegularPolygon对象。显示每个对象的周长和面积。源程序:public class RegularPolygon private int n = 3;private double side = 1;private double x = 0;private double y = 0;pub

15、lic RegularPolygon()public RegularPolygon(int n,double side)this.n = n;this.side = side;public RegularPolygon(int n,double side,double x,double y)this(n,side);this.x = x;this.y = y;public int getN() return n;public void setN(int n) this.n = n;public double getSide() return side;public void setSide(d

16、ouble side) this.side = side;public double getX() return x;public void setX(double x) this.x = x;public double getY() return y;public void setY(double y) this.y = y;public double getPerimeter()return n*side;public double getArea()return (n*side*side)/(4*Math.tan(Math.PI/n);public static void main(String args) RegularPolygon re

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号