Users.cs.jmu.edu
Reference for Questions 5 – 7. /** * The BankAccount class simulates a bank account. * @author Gaddis * @version V2 - updated by N. Harris - Nov 1, 2009 */public class BankAccount{ public static final double RATE = .01; // interest rate private static int nextAccount = 1234; // Next account number private static double allAccounts; // Balance in all accounts private int number; // Account name private double balance; // Account balance /** * This constructor sets the starting balance at 0.0 and assigns the account a number. */ public BankAccount() { number = nextAccount; nextAccount++; balance = 0.0; } /** * This overloaded constructor sets the starting balance to the value passed as an argument. * @param startBalance The starting balance. */ public BankAccount(double startBalance) { number = nextAccount; nextAccount++; if(startBalance > 0) balance = startBalance; else balance = 0; BankAccount.allAccounts = BankAccount.allAccounts + balance; } /** * The deposit method makes a deposit into the account. * @param amount The amount to add to the balance field. If negative, make sure * there is enough in the account * @return The amount deposited, will be zero if no deposit can be made. */ public double deposit(double amount) { if(amount > 0 || Math.abs(amount) <= balance) balance = balance + amount; else amount = 0; BankAccount.allAccounts = BankAccount.allAccounts + amount; return amount; } /** * The withdraw method withdraws an amount from the account. * @param amount The amount to subtract from the balance field. * @return amount withdrawn - will be zero if a withdrawal cannot take place */ public double withdraw(double amount) { if (balance >= amount || amount < 0) balance = balance - amount; else amount = 0; BankAccount.allAccounts = BankAccount.allAccounts - amount; return amount; } /** * This method adds interest to the account based on the current annual interest rate. * @return The new balance */ public double addInterest() { double interest; interest = balance * RATE; balance = balance + interest; BankAccount.allAccounts = BankAccount.allAccounts + interest; return balance; } /** * The setBalance method sets the account balance. * @param b The value to store in the balance field. */ public void setBalance(double bal) { BankAccount.allAccounts = BankAccount.allAccounts - balance; if (bal > 0) balance = bal; BankAccount.allAccounts = BankAccount.allAccounts + balance; } /** * The getBalance method returns the account balance. * @return The value in the balance field. */ public double getBalance() { return balance; } /** * The toString method returns a String representing this account balance */ public String toString() { return String.format("Account %d\tBalance: $%.2f", number, balance); } /** * This method returns a String summarizing the current accounts */ public static String toStringAccount() { return String.format("Accounts balance: $%.2f\tNext number: %d", BankAccount.allAccounts, BankAccount.nextAccount); }} Reference for questions 6-7/** * This program demonstrates the BankAccount class. */public class AccountTestV2{ public static void main(String[] args) { BankAccount nancy; BankAccount vinny; BankAccount[] cs139; nancy = new BankAccount(50.00); vinny = new BankAccount(100.00); System.out.println(nancy); System.out.println(vinny.toString()); // Answer Question 7 PART A questions here nancy.deposit(50.00); vinny.withdraw(250.00); nancy.deposit(-100.00); // Answer Question 7 PART B questions here cs139 = new BankAccount[5]; cs139[0] = nancy; cs139[1] = vinny; cs139[2] = nancy; cs139[3] = new BankAccount(250); cs139[4] = new BankAccount(); cs139[2].deposit(10.00); cs139[3].withdraw(50); vinny.withdraw(50); // Answer Question 7 PART C questions here for (BankAccount anAcct : cs139) anAcct.addInterest(); System.out.println(BankAccount.toStringAccount()); for (BankAccount anAcct : cs139) { System.out.println(anAcct); } // Answer Question 7 PART D questions here }}Grid below is available for the Tracing exercises. ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- understanding tostring method cse exam hacks
- introduction to xml
- centerbase developer s api
- cse 142 sample final exam 1 university of washington
- cs100j 03 dec 2003 sample questions with answers at the end
- message formatting is the process of assembling a message
- edu
- department of mathematics lehman college
- eda sezen bellisoy