国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

【Java語言程序設計(基礎篇)第10版 練習題答案】Practice_9_7

2019-11-06 07:45:27
字體:
來源:轉載
供稿:網友

(賬戶類 Account)設計一個名為 Account 的類,它包括:

一個名為 id 的 int 類型私有數據域(默認值為 0)。一個名為 balance 的 double 類型私有數據域(默認值為 0)。一個名為 annualInterestRate 的 double 類型私有數據域存儲當前利率(默認值為 0)。假設所有的賬戶都有相同的利率。一個名為 dateCreated 的 Date 類型的私有數據域,存儲賬戶的開戶日期。一個用于創建默認賬戶的無參構造方法。一個用于創建帶特定 id 和初始余額的賬戶的構造方法。id、balance 和 annualIntersRate 的訪問器和修改器。dateCreated 的訪問器。一個名為 getMonthlyInterestRate() 的方法,返回月利率。一個名為 withDraw 的方法,從賬戶提取特定數額。一個名為 deposit 的方法向賬戶存儲特定數額。 畫出該類的 UML 圖并實現這個類。

提示:方法 getMonthlyInterest() 用于返回月利息,而不是利息。月利息是 balance * monthlyInterestRate。monthlyInterestRate 是 annualInterestRate / 12。注意,annualInterestRate 是一個百分數,比如 4.5%。你需要將其除以 100。

編寫一個測試程序,創建一個賬戶 ID 為 1122、余額為 20 000 美元、年利率為 4.5% 的 Account 對象。使用 withDraw 方法取款 2500 美元,使用 deposit 方法存款 3000 美元,然后打印余額、月利息以及這個賬戶的開戶日期。

import java.util.Date;public class PRactice_9_7 { public static void main(String[] args) { Account account = new Account(1122, 20000); account.setAnnualInterestRate(4.5); account.withDraw(2500); account.deposit(3000); System.out.println("Balance: " + account.getBalance() + "/n" + "Monthly Interest Rate: " + account.getMonthlyInterestRate() + "/n" + "Date Created: " + account.getDateCreated()); }}class Account { private int id = 0; private double balance = 0; private static double annualInterestRate = 0; private Date dateCreated; public Account() { dateCreated = new Date(); } public Account(int id, double balance) { this.id = id; this.balance = balance; dateCreated = new Date(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public Date getDateCreated() { return dateCreated; } public double getMonthlyInterestRate() { double monthlyInterestRate = annualInterestRate / 12; return balance * monthlyInterestRate / 100; } public void withDraw(double money) { balance -= money; } public void deposit(double money) { balance += money; }}

輸出結果為:

Balance: 20500.0 Monthly Interest Rate: 76.875 Date Created: Sat Mar 04 12:37:56 CST 2017


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 林州市| 杭锦旗| 双江| 临夏市| 揭西县| 五河县| 邻水| 波密县| 郴州市| 玛沁县| 开封市| 玉屏| 伊金霍洛旗| 唐河县| 台东市| 阿瓦提县| 双鸭山市| 巴里| 江都市| 定安县| 武清区| 砚山县| 三门峡市| 盐亭县| 澳门| 资阳市| 会同县| 外汇| 肇州县| 忻城县| 河北区| 沽源县| 宁波市| 新蔡县| 佳木斯市| 高台县| 缙云县| 军事| 闻喜县| 获嘉县| 璧山县|