jTruBela

Assignment questions

Nov 18th, 2020 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. A Flexible Account Class
  2. File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then modify it as follows:
  3. 1. Overload the constructor as follows:
  4. • public Account (double initBal, String owner, long number) - initializes the balance, owner, and account number as specified
  5. • public Account (double initBal, String owner) - initializes the balance and owner as specified; randomly generates the account number.
  6. • public Account (String owner) - initializes the owner as specified; sets the initial balance to 0 and randomly generates the account number.
  7. 2. Overload the withdraw method with one that also takes a fee and deducts that fee from the account.
  8. File TestAccount.java contains a simple program that exercises these methods. Save it to your directory, study it to see what it does, and use it to test your modified Account class.
  9. //************************************************************
  10. // Account.java
  11. //
  12. // A bank account class with methods to deposit to, withdraw from,
  13. // change the name on, and get a String representation
  14. // of the account.
  15. //************************************************************
  16. public class Account
  17. {
  18. private double balance;
  19. private String name;
  20. private long acctNum;
  21. //-------------------------------------------------
  22. //Constructor -- initializes balance, owner, and account number
  23. //-------------------------------------------------
  24. public Account(double initBal, String owner, long number)
  25. {
  26. balance = initBal;
  27. name = owner;
  28. acctNum = number;
  29. }
  30. //-------------------------------------------------
  31. // Checks to see if balance is sufficient for withdrawal.
  32. // If so, decrements balance by amount; if not, prints message.
  33. //-------------------------------------------------
  34. public void withdraw(double amount)
  35. {
  36. if (balance >= amount)
  37. else
  38. balance -= amount;
  39. System.out.println("Insufficient funds");
  40. }
  41. //-------------------------------------------------
  42. // Adds deposit amount to balance.
  43. //-------------------------------------------------
  44. Chapter 7: Object-Oriented Design 109
  45.  
  46. public void deposit(double amount)
  47. {
  48. balance += amount;
  49. }
  50. //-------------------------------------------------
  51. // Returns balance.
  52. //-------------------------------------------------
  53. public double getBalance()
  54. {
  55. return balance;
  56. }
  57. //-------------------------------------------------
  58. // Returns a string containing the name, account number, and balance.
  59. //-------------------------------------------------
  60. public String toString()
  61. {
  62. return "Name:" + name +
  63. "\nAccount Number: " + acctNum +
  64. "\nBalance: " + balance;
  65. }
  66. }
  67. //************************************************************
  68. // TestAccount.java
  69. //
  70. // A simple driver to test the overloaded methods of
  71. // the Account class.
  72. //************************************************************
  73. import java.util.Scanner;
  74. Scanner scan = new Scanner(System.in);
  75. public class TestAccount
  76. {
  77. public static void main(String[] args)
  78. {
  79. String name;
  80. double balance;
  81. long acctNum;
  82. Account acct;
  83. System.out.println("Enter account holder's first name");
  84. name = scan.next();
  85. acct = new Account(name);
  86. System.out.println("Account for " + name + ":");
  87. System.out.println(acct);
  88. System.out.println("\nEnter initial balance");
  89. balance = scan.nextDouble();
  90. acct = new Account(balance,name);
  91. System.out.println("Account for " + name + ":");
  92. System.out.println(acct);
  93. 110
  94. Chapter 7: Object-Oriented Design
  95.  
  96. System.out.println("\nEnter account number");
  97. acctNum = scan.nextLong();
  98. acct = new Account(balance,name,acctNum);
  99. System.out.println("Account for " + name + ":");
  100. System.out.println(acct);
  101. System.out.print("\nDepositing 100 into account, balance is now ");
  102. acct.deposit(100);
  103. System.out.println(acct.getBalance());
  104. System.out.print("\nWithdrawing $25, balance is now ");
  105. acct.withdraw(25);
  106. System.out.println(acct.getBalance());
  107. System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");
  108. acct.withdraw(25,2);
  109. System.out.println(acct.getBalance());
  110. }
  111. System.out.println("\nBye!");
  112. }
  113. Chapter 7: Object-Oriented Design 111
  114.  
  115. Opening and Closing Accounts
  116. 1. Suppose the bank wants to keep track of how many accounts exist.
  117. File Account.java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then write the following additional code:
  118. a. Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will be initialized (to 0, since it’s an int) automatically.
  119. b. Add code to the constructor to increment this variable every time an account is created.
  120. c. Add a static method getNumAccounts that returns the total number of accounts. Think about why this method should be static - its information is not related to any particular account.
  121. d. File TestAccounts1.java contains a simple program that creates the specified number of bank accounts then uses the getNumAccounts method to find how many accounts were created. Save it to your directory, then use it to test your modified Account class.
  122. 2. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Also decrement the total number of accounts.
  123. 3. Add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned. Two important rules of consolidation:
  124. • Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number.
  125. • Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your money!
  126. Check these conditions before creating the new account. If either condition fails, do not create the new account or close the old ones; print a useful message and return null.
  127. 4. Write a test program that prompts for and reads in three names and creates an account with an initial balance of $ 100 for each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account. Now print the accounts again, including the consolidated one if it was created.
  128. //************************************************************
  129. // TestAccounts1
  130. // A simple program to test the numAccts method of the
  131. // Account class.
  132. //************************************************************
  133. import java.util.Scanner;
  134. public class TestAccounts1
  135. {
  136. public static void main(String[] args)
  137. {
  138. Account testAcct;
  139. Scanner scan = new Scanner(System.in);
  140. Chapter 7: Object-Oriented Design 113
  141.  
  142. System.out.println("How many accounts would you like to create?"); int num =
  143. scan.nextInt();
  144. for (int i=1; i<=num; i++)
  145. {
  146. testAcct = new Account(100, "Name" + i); System.out.println("\nCreated account " + testAcct); System.out.println("Now there are " + Account.numAccounts () +
  147. " accounts");
  148. }
  149. }
  150. }
  151. 114 Chapter 7: Object-Oriented Design
  152.  
Add Comment
Please, Sign In to add comment