CS150 - Introduction to Computing II mp02 (inheritance) - 100 pts You must submi
ID: 668317 • Letter: C
Question
CS150 - Introduction to Computing II
mp02 (inheritance) - 100 pts
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 offered by
each.
Project setup:
Use your IDE to create the project mp02. 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 machine problem you are to use inheritance and composition to create an application
that keeps track of bank accounts and customers. Here is the output of the program to start
things off.
Implementation requirements:
The requirements for this program are as follows:
(1) Add the Customer class to the project. This class represents a single customer record.
Add sccessors and a descriptor ro the class.
The getName () method returns the name as "Last, First''.
The descriptor returns the description : '' Name : Last, First"
(2) Add the Account class to the project. This is the superclass and represents a bank account.
Add a descriptor to the class.
The ctor creates the date object based on the date the account was opened. This should be
the current date.
The descriptor returns the description: "Opened: <date>". Use the following code to
format the date.
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.SHORT);
String formattedDate = df.format(getDateOpened());
(3) Add the CheckingAccount class to the project. This class represents a checking account
and it inherits from the Account class.
Add getters, private setters, and a descriptor.
This class has no public setters, since normally the account is created with an account
number and customer. Once created, this information should not change, rather, the
account is closed and recreated.
The descriptor returns the description: "[accountNumber], Opened: <date>, Name:
Last, First
(4) Add the Bank class to the project. This class represents the bank with all its accounts and
customers.
Add getters (for the counts) and a descriptor.
Allow for 100 customers and 200 accounts.
The getCustomerAtIndex() method returns the customer at the given index. Assume the
index is in the range [0..customerCount - 1].
The getAccountAtIndex() method returns the account at the given index. Assume the
index is in the range [0..accountCount - 1].
The addCustomer() method should add the customer to the end of the customers array.
The openAccount() method does two things. It first adds the account to the end of the
accounts array and then calls the addCustomer() method to add its owner to the
customers array.
The customerList() returns a string containing all customers, one per line.
The accountList() returns a string containing all accounts, one per line.
The descriptor returns the description:
(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 above.
(7) Extra credit [10 pts]: Modify the Bank in such a way that the two arrays are dynamic. Each
time a customer is added or an account is opened, the arrays are resized. Hint: Since the
arrays are now dynamic, the size of the array also represents its length, so remove the
properties that are not needed.
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: 6/4/2015
* Course: CS150 - Introduction to Computing II - Summer 15
*
* Desc: This program uses the Angle class that stores an angular
* measurement. This test program tests all the functionality
* provide by this class.
*/
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!");
}
}