Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS150 - Introduction to Computing II mp03 (inheritance) - 100 pts Due: 11:59 pm

ID: 669665 • Letter: C

Question

CS150 - Introduction to Computing II mp03 (inheritance) - 100 pts Due: 11:59 pm Friday, September 25, 2015 Electronic submission: You must submit a single .zip archive file of your project folder. Use a utility that produces .zip files (Windows: right click | Send to | compress, Mac: right click | compress) Objectives: To use inheritance and polymorphism in order to understand the benefits ofered by each. Project setup: Use your IDE to create the project mp03. This project should include the class file, Main.java. Refer to the appropriate "how to" notes available on the course website, for instructions on how to create a project and manage its files. Language features used: arrays classes inheritance polymorphism Problem description: In this assignment you are going to modify mp02 by adding additional subclasses. This time around we want to also have SavingsAccounts as one of the available account types. This addition will require a few modifications, listed below. Implementation requirements: (1) Add the SavingsAccount class to the project. This class inherits from the Account class. • Add the accessors (get/set) and the descriptor (toString) • The descriptor returns the string: "[accountNumber], Opened: , by: Last, First Balance: $00.00, Interest rate: 00.00%" 1 / 4 Last updated: September 18, 2015 at 6:49 AM SavingsAccount -interestRate: double +SavingsAccount(interestRate: double, accountNumber: int, owner: Customer, double currentBalance) (2) Modify the CheckingAccount class. This class still inherits from the Account class. • Notice the new edited properties for this class. This time around this class only has the one boolean variable indicating whether the account ofers free checks or not • Add the accessors(get/set) and the descriptor(toString) • The descriptor returns the string: "[accountNumber], Opened: , by: Last, First Balance: $00.00, Free Checks: " (3) Modify the Account class. This time the superclass contains some of the properties that were stored in the subclass in mp02. Now since we have two subclasses, we will pull those out. • Add public getters, and private setters for all properties. • The withdraw method updates the current balance to reflect the amount withdrawn and returns the updated balance after the withdrawal is made. Assume that the amount withdrawn can be covered by the current balance. • The deposit method updates the current balance to reflect the amount deposited and returns the updated balance after the deposit is made. • The descriptor returns the string: "[accountNumber], Opened: , by: Last, First Balance: $00.00" (4) Modify the Bank class. This class represents the bank with all its accounts and customers. 2 / 4 Last updated: September 18, 2015 at 6:49 AM Bank -customers: ArrayList -accounts: ArrayList +Bank() +getCustomerAtIndex(index: int): Customer +getAccountAtIndex(index: int): Account +addCustomer(o: Customer): void +openAccount(o: Account): void +customerList(): String +accountList(): String Account -accountNumber: int -dateOpened: Date -owner: Customer -currentBalance: double #Account(accountNumber: int, owner: Customer, currentBalance: double) +withdraw(amount: double): double +deposit(amount: double): double CheckingAccount -freeChecks: boolean +CheckingAccount(freeChecks: boolean, accountNumber: int, owner: Customer, double currentBalance) • Notice the change to the two properties. This time around the arrays have been replaced with ArrayLists. This class provides all the data management functionality needed to store objects of the appropriate type. • The getAccountAtIndex() methods returns an Account type this time, since we now have two types of objects. • The openAccount() method accepts and Account type this time, since we now have two types of accounts, checking and savings. • The descriptor returns the string: Customers(3) Name: Apple, Adam Name: Bagel, Beatrice Name: Cucumber, Chris Accounts(3) [10100], Opened: Sep 17, 2015 7:27 PM, Owner: Apple, Adam Balace: $500.00, Free checks: No [10101], Opened: Sep 17, 2015 7:27 PM, Owner: Bagel, Beatrice Balance: $2000.00, Free checks: Yes [20100], Opened: Sep 17, 2015 7:27 PM, Owner: Cucumber, Chris Balance: $5000.00, Interest rate: 0.02% (5) Your driver should simply create a Bank object named bank. Add the three accounts shown above to this bank object, by opening three accounts with the shown account numbers and customer information. (6) The program should then list all bank customers, bank accounts and describe the bank object. Refer to the program output below. As you write your programs from here on out, documentation will be desirable and an essential part of your code. Add the following section to each of your programs to identify relevant information to anyone reading your code. The sample below is what I used for Main.java, so make the appropriate changes to reflect your current/accurate information. This is just a sample, so feel free to add to it if you want, but do not remove anything. /* File: Main.java * Name: Socratis Tornaritis * Revised: mm/dd/yyyy * Course: CS150 - Introduction to Computing II - 15 * * Desc: This program ... */ 3 / 4 Last updated: September 18, 2015 at 6:49 AM 4 / 4 Last updated: September 18, 2015 at 6:49 AM Customer list: -------------- Owner: Apple, Adam Owner: Bagel, Beatrice Owner: Cucumber, Chris Account list: ------------- [10100], Opened: Sep 18, 2015 6:48 AM, Owner: Apple, Adam Balance: $500.00, Free checks: No [10101], Opened: Sep 18, 2015 6:48 AM, Owner: Bagel, Beatrice Balance: $2000.00, Free checks: Yes [20100], Opened: Sep 18, 2015 6:48 AM, Owner: Cucumber, Chris Balance: $5000.00, Interest rate: 0.02% Bank record: ------------ Customers(3) Owner: Apple, Adam Owner: Bagel, Beatrice Owner: Cucumber, Chris Accounts(3) [10100], Opened: Sep 18, 2015 6:48 AM, Owner: Apple, Adam Balance: $500.00, Free checks: No [10101], Opened: Sep 18, 2015 6:48 AM, Owner: Bagel, Beatrice Balance: $2000.00, Free checks: Yes [20100], Opened: Sep 18, 2015 6:48 AM, Owner: Cucumber, Chris Balance: $5000.00, Interest rate: 0.02% BUILD SUCCESSFUL (total time: 0 seconds)

Explanation / Answer

example program

import java.util.Scanner;

public class BankAccount {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            int userChoice;

            boolean quit = false;

            do {

                  System.out.print("Your choice, 0 to quit: ");

                  userChoice = in.nextInt();

                  if (userChoice == 0)

                        quit = true;

            } while (!quit);

      }

}

main program

import java.util.Scanner;

public class BankAccount {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            int userChoice;

            boolean quit = false;

            do {

                  System.out.println("1. Deposit money");

                  System.out.println("2. Withdraw money");

                  System.out.println("3. Check balance");

                  System.out.print("Your choice, 0 to quit: ");

                  userChoice = in.nextInt();

                  switch (userChoice) {

                  case 1:

                        // deposit money

                        break;

                  case 2:

                        // withdraw money

                        break;

                  case 3:

                        // check balance

                        break;

                  case 0:

                        quit = true;

                        break;

                  default:

                        System.out.println("Wrong choice.");

                        break;

                  }

                  System.out.println();

            } while (!quit);

            System.out.println("Bye!");

      }

}

import java.util.Scanner;

public class BankAccount {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            int userChoice;

            boolean quit = false;

            float balance = 0f;

            do {

                  System.out.println("1. Deposit money");

                  System.out.println("2. Withdraw money");

                  System.out.println("3. Check balance");

                  System.out.print("Your choice, 0 to quit: ");

                  userChoice = in.nextInt();

                  switch (userChoice) {

                  case 1:

                        float amount;

                        System.out.print("Amount to deposit: ");

import java.util.Scanner;

public class BankAccount {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            int userChoice;

            boolean quit = false;

            float balance = 0f;

            do {

                  System.out.println("1. Deposit money");

                  System.out.println("2. Withdraw money");

                  System.out.println("3. Check balance");

                  System.out.print("Your choice, 0 to quit: ");

                  userChoice = in.nextInt();

                  switch (userChoice) {

                  case 1:

                        float amount;

                        System.out.print("Amount to deposit: ");

                        amount = in.nextFloat();

                        if (amount <= 0)

                             System.out.println("Can't deposit nonpositive amount.");

                        else {

                             balance += amount;

                             System.out.println("$" + amount + " has been deposited.");

                        }

                        break;

                  case 2:

                        System.out.print("Amount to withdraw: ");

                        amount = in.nextFloat();

                        if (amount <= 0 || amount > balance)

                             System.out.println("Withdrawal can't be completed.");

                        else {

                             balance -= amount;

                             System.out.println("$" + amount + " has been withdrawn.");

                        }

                        break;

                  case 3:

                        System.out.println("Your balance: $" + balance);

                        break;

                  case 0:

                        quit = true;

                        break;

                  default:

                        System.out.println("Wrong choice.");

                        break;

                  }

                  System.out.println();

            } while (!quit);

            System.out.println("Bye!");

      }

}

import java.util.Scanner;

public class BankAccount {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            int userChoice;

            boolean quit = false;

            float balance = 0f;

            do {

                  System.out.println("1. Deposit money");

                  System.out.println("2. Withdraw money");

                  System.out.println("3. Check balance");

                  System.out.print("Your choice, 0 to quit: ");

                  userChoice = in.nextInt();

                  switch (userChoice) {

                  case 1:

                        float amount;

                        System.out.print("Amount to deposit: ");

                        amount = in.nextFloat();

                        if (amount <= 0)

                             System.out.println("Can't deposit nonpositive amount.");

                        else {

                             balance += amount;

                             System.out.println("$" + amount + " has been deposited.");

                        }

                        break;

                  case 2:

                        System.out.print("Amount to withdraw: ");

                        amount = in.nextFloat();

                        if (amount <= 0 || amount > balance)

                             System.out.println("Withdrawal can't be completed.");

                        else {

                             balance -= amount;

                             System.out.println("$" + amount + " has been withdrawn.");

                        }

                        break;

                  case 3:

                        System.out.println("Your balance: $" + balance);

                        break;

                  case 0:

                        quit = true;

                        break;

                  default:

                        System.out.println("Wrong choice.");

                        break;

                  }

                  System.out.println();

            } while (!quit);

            System.out.println("Bye!");

      }

}

                        amount = in.nextFloat();

                        balance += amount;

                        break;

                  case 2:

                        System.out.print("Amount to withdraw: ");

                        amount = in.nextFloat();

                        balance -= amount;

                        break;

                  case 3:

                        System.out.println("Your balance: $" + balance);

                        break;

                  case 0:

                        quit = true;

                        break;

                  default:

                        System.out.println("Wrong choice.");

                        break;

                  }

                  System.out.println();

            } while (!quit);

            System.out.println("Bye!");

      }

}