没有合适的资源?快使用搜索试试~ 我知道了~
实验1 类和对象的实验.doc
资源详情
资源评论
资源推荐
实验 1 类和对象的实验
一 实验内容
1、设计一个名为 Account 的类,它包括:
(1)一个名为 id 的 int 类型私有账户数据域(默认值为 0);
(2)一个名为 balance 的 double 类型私有账户数据域(默认值为 0);
(3)一个名为 annualInterestRate 的 double 类型私有数据域存储当前利率(默认值为 0)。
假设所有的账户都有相同的利率。
(4)一个名为 dateCreate 的 Date 类型私有数据域存储账户的开户日期;
(5)一个能创建默认账户的无参构造方法;
(6)一个能创建带特定 id 和初始余额的账户的构造方法;
(7)id、balance 和 annualInterestRate 的访问器和修改器;
(8)dateCreated 的访问器;
(9)一个名为 getMonthInterestRate()的方法返回月利率;
(10)一个名为 withDraw 的方法从账户提取特定数额;
(11)一个名为 deposit 的方法向账户存储特定数额;
画出该类的 UML 图,实现这个类,编写一个测试程序,创建一个账户 ID 为 1122、余额为
20000 元,年利率为 5%的 Account 对象,使用 withDraw 方法取款 3000 元,使用 deposit 方
法存款 5000 元,然后打印余额、月利息以及这个账户的开户日期。
2、定义一个 Account 类来建模一个银行账户。一个账户有账号、余额、年利率、开户日期
等属性,以及存款和取款等方法,创建两个检测支票账户( checking account)和储蓄账户
(saving account)的子类,支票账户有个透支限定额,但储蓄账户不能透支。
画出这些类的 UML 图,实现这些类,编写一个测试程序,创建 Account、SAVINGACCOUNT
和
CheckingAccount 的对象,然后调用它们的 toString()方法。
3、根据要求设计一个新的 Account 类:
(1)添加一个 String 类型的新数据 name 类存储客户的名字;
(2)添加一个新的构造方法,该方法创建一个带指定名字、id 和收支额的账户;
(3)添加一个名为 transactions 的新数据域,它的类型是 ArrayList,可以为账户存储交易,
每笔交易都是一个 Transaction 类的实例,Transaction 类的定义如下图所示。
(4)修改 withdraw 和 deposit 方法,向 transactions 数组线性表添加一笔交易。
(5)其它所有属性和方法都和练习题 2 一样。
编写一个测试程序,创建一个年利率为 1.5%、收支额为 10000、id 为 1122 而名字为江山的
Account。向该账户存入 500 元、1000 元和 5000 元并从该账户中取出 100 元、2000 元和
3000 元。打印账户清单,显示账户持有者名字、利率、收支额和所有交易。
在类中提供了这些数据域的 get
和 set 方法,在 uml 图中省略了
Transaction 类描述银行账户的一笔交易
4、利用上述的 Account 类,模拟一台 ATM 机。创建一个有 10 个账户的数组,其 id 为
0,1,........,9,并初始化收支为 100 元人民币,系统提示用户输入一个 id,如果输入的 id 不
正确,提示用户输入正确的 id,一旦接受一个 id,就显示下图所示的主菜单,可以选择 1
来查看当前的收支,选择 2 表示取钱,选择 3 表示存钱,选择 4 表示退出主菜单。一旦退
出,系统就会提示再次输入 id。因此系统一旦启动就不会停止。
画出类的 UML 图,实现这个类,编写一个测试程序完成上述功能。
Enter an id: 3
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 100.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 2
Enter an amount to withdraw: 1000
The amount is too large, ignored
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 2
Enter an amount to withdraw: 30
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 3
Enter an amount to deposit: 12
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 4
Enter an id: 3
二 实验目的
(1) 理解类的结构、类和对象之间的关系
(2) 理解变量、构造方法、方法
(3) 理解接口、抽象类
(4) 理解和掌握类的数据封装和隐藏、类的继承和多态的意义和使用
(5) 理解和掌握修饰符对变量、方法、类修饰的含义和使用
三 实验过程
(1) 问题分析
在解决问题前,一定要愤青这个 account 对象的状态和行为。状态就是钱的额
度是否符合取出来的,行为就是存和取。
(2) 代码编写
import java.util.Date;
public class Account {
protected int id;//账户数据域
protected double balance=0;//账户余额数据域
protected double annualInterestRate=0;//数据域存储当前率,假设每个账户
都有相同的利率
protected Date dateCreate;//开户日期
public Account()//创建默认账户的无参构造
{
super();
this.dateCreate=new Date();
}
protected Account(int id,double balance) {
this.id = id;
this.balance = balance;
dateCreate = new Date();
}
protected Account(int id,double balance,double annualIntegerRate) {
this(id,balance);
dateCreate = new Date();
this.annualInterestRate = annualIntegerRate;
}
//账户,余额,利率访问器和修改器
public int getId()
{
return id;
}
public void setId(int id)
{
this.id=id;
}
public double getBalance()
{
return balance;
}
public void setBalance(double balancce)
{
this.balance=balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate=annualInterestRate;
}
//访问器和修改器结束
//开户日期访问器
public Date getDateCreate()
{
return dateCreate;
}
public void setDateCreate(Date dateCreate)
{
this.dateCreate=dateCreate;
}
public double getMonthInterRate()
{
return annualInterestRate;
}
public void withDraw(double Decreasemoney)//取钱
{
if(Decreasemoney>balance)
{
剩余16页未读,继续阅读
weixin_54027347
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0