For this program, you will be the creator of a class (in Part1) and later the us
ID: 3767102 • Letter: F
Question
For this program, you will be the creator of a class (in Part1) and later the user of the class (in Part2). You will have to be familiar with classes, methods, passing things in and out of methods, constructors, static data/methods, exception handling, interfaces, and “aggregate” classes.
The problem:
You are to write a class called BankAccount, which will simulate a user’s bank account. You can use my Program5Driver.java as the “driver” that will create instances and tell them to do things (through their methods).
Then you will create your own driver called Program5.java to create other instances and tell them to do things.
The BankAccount class: this class should implement AccountInterface and have the following:
Data: it should have the following data. I have suggested variable names to help in understanding what they do and to make the explanations clearer. You can name them anything you want (as long as the name is meaningful).
A double called balance, which will hold the balance for that account. A String called name, which will hold the name for that account. A java.util.Date called creationDate, which will contain a reference to a java.util.Date that holds the creation date. You can declare it as a java.util.Date or import java.util.*. But…if you import, be sure that you do not have another Date class in the same directory or the compile will use the one in the directory. A boolean called frozen, which keeps track of whether or not the account is “frozen” (cannot be accessed). A double called limit, which will hold the maximum that can be withdrawn at one time from that account. A double called MAXLIMIT, which will be a constant (“final”) set to 500. This will be the maximum that the withdrawal limit can be set to. An int called accountNumber, which will hold the account number for that account.
A static int called howMany. The BankAccount class will use this to keep track of how many individual accounts have been created.
Constructors: it should have the following 2 constructors.
A default constructor (which receives no arguments). It should set the account’s name field to “classified” and the other fields should be set to the same initial values as the other constructor (below). If you want, you can make this work by calling the other constructor with the statement: this(“classified”);
A parameterized constructor which receives 1 argument – the name (a String) of the person who has the account. It should do the following: Set the account’s name to the name that is passed in Create the creationDate using the default constructor of the java.util.Date class. It will initialize the creationDate to the current time (from the computer clock). Set frozen to false; Set the limit to 300 (always, does not depend on what is passed in). Add 1 to howMany (so the class knows how many accounts are now created) Then set the accountNumber to howMany (if there are now 3 accounts, then this will be #3) Set the balance to 0. Methods: Since BankAccount implements AccountInterface, it will automatically have to have the methods defined in the interface. It should also have the static method shown below. Therefore the following methods should be implemented.
a static method called getNumAccounts(), which receives nothing and returns an int showing how many accounts have been created. Also implement all of the other methods listed in the AccountInterface. Please see the comments in AccountInterface.java for their descriptions.
Part 1:
Write your BankAccount class and compile it (with the java.util.Date class as an “aggregate” class). To test it, you can download Program5Driver.java from Blackboard and use it as a “driver” to create accounts and tell them to do things. The expected results from running Program5Driver.java are also on Canvas as expected.txt.
Part 2:
Once your BankAccount class is working, create another “driver” program of your own called Program5.java. Have it implement the following functionality inside main. Notice there are 4 sections (create/use 3 accounts and then ask the BankAccount how many accounts have been created). Put each of the 4 sections into a separate try-catch block; for each one make sure that every possible type of exception can be caught (either by having multiple catch blocks or by setting it up to catch every possible type of exception). You can just print the exception if it is caught.
Create a new account (use any variable name you want) – pass in “Brad Pitt”. Tell it to deposit 439.50 Tell it to deposit 10 Tell it to withdraw 60 Tell it to get the balance and print “The balance is: “ and then the balance that was returned.
Create a new account (use any variable name you want) – pass in YOUR name Tell it to deposit 45.00 Tell it to withdraw 20 Print it.
Create a new account (use any variable name you want) – pass in “Scam Artist” Tell it to deposit 10 Tell it to withdraw 260
Ask the BankAccount class how many accounts have been created and print the results (refer to the tester if needed).
Extra Credit (optional, 6 pts):
The variables that will hold money should be doubles (because of decimals). However, they will not automatically print out like money. After your program is working, make the following changes to have them print out like money. Put the following import statement in your program (both BankAccount and Program5) import java.text.NumberFormat; Somewhere in the class (before you print things as money), put the following line: NumberFormat money = NumberFormat.getCurrencyInstance(); You have created an instance of the NumberFormat class and called it “money.” Now, every time you want to print out a money variable, you can tell money (your variable) to format it so it looks like currency. Instead of using the variable in the print statement, use money.format(<the variable>). For example, if you have a variable called theDeposit, you can change it from this: System.out.println(“ Total income: ” + theDeposit); to this: System.out.println( " Total income: " + money.format(theDeposit));
Please note that negative numbers will come out with ( )’s instead of a minus sign. This is the way accountants do it and it is OK to leave it like this…
Extra Credit (optional, 6 pts)
Create a new class called IllegalWithdrawalException. Make it a subclass of RunTimeException. It will not need any data or methods (all will be inherited). It should have one constructor which accepts a single String as an argument. Have that constructor do the same thing as its parent’s constructor would do with the same String.
Once your IllegalWithdrawalException compiles, change the BankAccount code so that it throws a new IllegalWithdrawalException instead of an IllegalArgumentException for any of the bad conditions in the withdraw method.
Finally, go back to your Part2 and change the try-catch blocks everywhere so that they will also catch an IllegalWithdrawalException. You can do this either by adding another catch block to the existing code everywhere or by changing the catch blocks so that the same catch block will the IllegalArgumentExceptions, IllegalWithdrawalExceptions, and IllegalStateExceptions.
Comments and indenting:
At the top of the program (class) should be a short comment which describes its purpose. It should also have your name and class on a separate line.
In the code itself, indent inside the class and then again inside main. Indent inside while loops and for loops. Also indent inside if statements and the else part. Put a blank line and then a comment as needed to describe “sections” of code.
I need help coding part 2.
For this program, you will be the creator of a class (in Part1) and later the user of the class (in Part2). You will have to be familiar with classes, methods, passing things in and out of methods, constructors, static data/methods, exception handling, interfaces, and “aggregate” classes.
The problem:
You are to write a class called BankAccount, which will simulate a user’s bank account. You can use my Program5Driver.java as the “driver” that will create instances and tell them to do things (through their methods).
Then you will create your own driver called Program5.java to create other instances and tell them to do things.
The BankAccount class: this class should implement AccountInterface and have the following:
Data: it should have the following data. I have suggested variable names to help in understanding what they do and to make the explanations clearer. You can name them anything you want (as long as the name is meaningful).
A double called balance, which will hold the balance for that account. A String called name, which will hold the name for that account. A java.util.Date called creationDate, which will contain a reference to a java.util.Date that holds the creation date. You can declare it as a java.util.Date or import java.util.*. But…if you import, be sure that you do not have another Date class in the same directory or the compile will use the one in the directory. A boolean called frozen, which keeps track of whether or not the account is “frozen” (cannot be accessed). A double called limit, which will hold the maximum that can be withdrawn at one time from that account. A double called MAXLIMIT, which will be a constant (“final”) set to 500. This will be the maximum that the withdrawal limit can be set to. An int called accountNumber, which will hold the account number for that account.
A static int called howMany. The BankAccount class will use this to keep track of how many individual accounts have been created.
Constructors: it should have the following 2 constructors.
A default constructor (which receives no arguments). It should set the account’s name field to “classified” and the other fields should be set to the same initial values as the other constructor (below). If you want, you can make this work by calling the other constructor with the statement: this(“classified”);
A parameterized constructor which receives 1 argument – the name (a String) of the person who has the account. It should do the following: Set the account’s name to the name that is passed in Create the creationDate using the default constructor of the java.util.Date class. It will initialize the creationDate to the current time (from the computer clock). Set frozen to false; Set the limit to 300 (always, does not depend on what is passed in). Add 1 to howMany (so the class knows how many accounts are now created) Then set the accountNumber to howMany (if there are now 3 accounts, then this will be #3) Set the balance to 0. Methods: Since BankAccount implements AccountInterface, it will automatically have to have the methods defined in the interface. It should also have the static method shown below. Therefore the following methods should be implemented.
a static method called getNumAccounts(), which receives nothing and returns an int showing how many accounts have been created. Also implement all of the other methods listed in the AccountInterface. Please see the comments in AccountInterface.java for their descriptions.
Part 1:
Write your BankAccount class and compile it (with the java.util.Date class as an “aggregate” class). To test it, you can download Program5Driver.java from Blackboard and use it as a “driver” to create accounts and tell them to do things. The expected results from running Program5Driver.java are also on Canvas as expected.txt.
Part 2:
Once your BankAccount class is working, create another “driver” program of your own called Program5.java. Have it implement the following functionality inside main. Notice there are 4 sections (create/use 3 accounts and then ask the BankAccount how many accounts have been created). Put each of the 4 sections into a separate try-catch block; for each one make sure that every possible type of exception can be caught (either by having multiple catch blocks or by setting it up to catch every possible type of exception). You can just print the exception if it is caught.
Create a new account (use any variable name you want) – pass in “Brad Pitt”. Tell it to deposit 439.50 Tell it to deposit 10 Tell it to withdraw 60 Tell it to get the balance and print “The balance is: “ and then the balance that was returned.
Create a new account (use any variable name you want) – pass in YOUR name Tell it to deposit 45.00 Tell it to withdraw 20 Print it.
Create a new account (use any variable name you want) – pass in “Scam Artist” Tell it to deposit 10 Tell it to withdraw 260
Ask the BankAccount class how many accounts have been created and print the results (refer to the tester if needed).
Extra Credit (optional, 6 pts):
The variables that will hold money should be doubles (because of decimals). However, they will not automatically print out like money. After your program is working, make the following changes to have them print out like money. Put the following import statement in your program (both BankAccount and Program5) import java.text.NumberFormat; Somewhere in the class (before you print things as money), put the following line: NumberFormat money = NumberFormat.getCurrencyInstance(); You have created an instance of the NumberFormat class and called it “money.” Now, every time you want to print out a money variable, you can tell money (your variable) to format it so it looks like currency. Instead of using the variable in the print statement, use money.format(<the variable>). For example, if you have a variable called theDeposit, you can change it from this: System.out.println(“ Total income: ” + theDeposit); to this: System.out.println( " Total income: " + money.format(theDeposit));
Please note that negative numbers will come out with ( )’s instead of a minus sign. This is the way accountants do it and it is OK to leave it like this…
Extra Credit (optional, 6 pts)
Create a new class called IllegalWithdrawalException. Make it a subclass of RunTimeException. It will not need any data or methods (all will be inherited). It should have one constructor which accepts a single String as an argument. Have that constructor do the same thing as its parent’s constructor would do with the same String.
Once your IllegalWithdrawalException compiles, change the BankAccount code so that it throws a new IllegalWithdrawalException instead of an IllegalArgumentException for any of the bad conditions in the withdraw method.
Finally, go back to your Part2 and change the try-catch blocks everywhere so that they will also catch an IllegalWithdrawalException. You can do this either by adding another catch block to the existing code everywhere or by changing the catch blocks so that the same catch block will the IllegalArgumentExceptions, IllegalWithdrawalExceptions, and IllegalStateExceptions.
Comments and indenting:
At the top of the program (class) should be a short comment which describes its purpose. It should also have your name and class on a separate line.
In the code itself, indent inside the class and then again inside main. Indent inside while loops and for loops. Also indent inside if statements and the else part. Put a blank line and then a comment as needed to describe “sections” of code.
I need help coding part 2.
For this program, you will be the creator of a class (in Part1) and later the user of the class (in Part2). You will have to be familiar with classes, methods, passing things in and out of methods, constructors, static data/methods, exception handling, interfaces, and “aggregate” classes.
The problem:
You are to write a class called BankAccount, which will simulate a user’s bank account. You can use my Program5Driver.java as the “driver” that will create instances and tell them to do things (through their methods).
Then you will create your own driver called Program5.java to create other instances and tell them to do things.
The BankAccount class: this class should implement AccountInterface and have the following:
Data: it should have the following data. I have suggested variable names to help in understanding what they do and to make the explanations clearer. You can name them anything you want (as long as the name is meaningful).
A double called balance, which will hold the balance for that account. A String called name, which will hold the name for that account. A java.util.Date called creationDate, which will contain a reference to a java.util.Date that holds the creation date. You can declare it as a java.util.Date or import java.util.*. But…if you import, be sure that you do not have another Date class in the same directory or the compile will use the one in the directory. A boolean called frozen, which keeps track of whether or not the account is “frozen” (cannot be accessed). A double called limit, which will hold the maximum that can be withdrawn at one time from that account. A double called MAXLIMIT, which will be a constant (“final”) set to 500. This will be the maximum that the withdrawal limit can be set to. An int called accountNumber, which will hold the account number for that account.
A static int called howMany. The BankAccount class will use this to keep track of how many individual accounts have been created.
Constructors: it should have the following 2 constructors.
A default constructor (which receives no arguments). It should set the account’s name field to “classified” and the other fields should be set to the same initial values as the other constructor (below). If you want, you can make this work by calling the other constructor with the statement: this(“classified”);
A parameterized constructor which receives 1 argument – the name (a String) of the person who has the account. It should do the following: Set the account’s name to the name that is passed in Create the creationDate using the default constructor of the java.util.Date class. It will initialize the creationDate to the current time (from the computer clock). Set frozen to false; Set the limit to 300 (always, does not depend on what is passed in). Add 1 to howMany (so the class knows how many accounts are now created) Then set the accountNumber to howMany (if there are now 3 accounts, then this will be #3) Set the balance to 0. Methods: Since BankAccount implements AccountInterface, it will automatically have to have the methods defined in the interface. It should also have the static method shown below. Therefore the following methods should be implemented.
a static method called getNumAccounts(), which receives nothing and returns an int showing how many accounts have been created. Also implement all of the other methods listed in the AccountInterface. Please see the comments in AccountInterface.java for their descriptions.
Part 1:
Write your BankAccount class and compile it (with the java.util.Date class as an “aggregate” class). To test it, you can download Program5Driver.java from Blackboard and use it as a “driver” to create accounts and tell them to do things. The expected results from running Program5Driver.java are also on Canvas as expected.txt.
Part 2:
Once your BankAccount class is working, create another “driver” program of your own called Program5.java. Have it implement the following functionality inside main. Notice there are 4 sections (create/use 3 accounts and then ask the BankAccount how many accounts have been created). Put each of the 4 sections into a separate try-catch block; for each one make sure that every possible type of exception can be caught (either by having multiple catch blocks or by setting it up to catch every possible type of exception). You can just print the exception if it is caught.
Create a new account (use any variable name you want) – pass in “Brad Pitt”. Tell it to deposit 439.50 Tell it to deposit 10 Tell it to withdraw 60 Tell it to get the balance and print “The balance is: “ and then the balance that was returned.
Create a new account (use any variable name you want) – pass in YOUR name Tell it to deposit 45.00 Tell it to withdraw 20 Print it.
Create a new account (use any variable name you want) – pass in “Scam Artist” Tell it to deposit 10 Tell it to withdraw 260
Ask the BankAccount class how many accounts have been created and print the results (refer to the tester if needed).
Extra Credit (optional, 6 pts):
The variables that will hold money should be doubles (because of decimals). However, they will not automatically print out like money. After your program is working, make the following changes to have them print out like money. Put the following import statement in your program (both BankAccount and Program5) import java.text.NumberFormat; Somewhere in the class (before you print things as money), put the following line: NumberFormat money = NumberFormat.getCurrencyInstance(); You have created an instance of the NumberFormat class and called it “money.” Now, every time you want to print out a money variable, you can tell money (your variable) to format it so it looks like currency. Instead of using the variable in the print statement, use money.format(<the variable>). For example, if you have a variable called theDeposit, you can change it from this: System.out.println(“ Total income: ” + theDeposit); to this: System.out.println( " Total income: " + money.format(theDeposit));
Please note that negative numbers will come out with ( )’s instead of a minus sign. This is the way accountants do it and it is OK to leave it like this…
Extra Credit (optional, 6 pts)
Create a new class called IllegalWithdrawalException. Make it a subclass of RunTimeException. It will not need any data or methods (all will be inherited). It should have one constructor which accepts a single String as an argument. Have that constructor do the same thing as its parent’s constructor would do with the same String.
Once your IllegalWithdrawalException compiles, change the BankAccount code so that it throws a new IllegalWithdrawalException instead of an IllegalArgumentException for any of the bad conditions in the withdraw method.
Finally, go back to your Part2 and change the try-catch blocks everywhere so that they will also catch an IllegalWithdrawalException. You can do this either by adding another catch block to the existing code everywhere or by changing the catch blocks so that the same catch block will the IllegalArgumentExceptions, IllegalWithdrawalExceptions, and IllegalStateExceptions.
Comments and indenting:
At the top of the program (class) should be a short comment which describes its purpose. It should also have your name and class on a separate line.
In the code itself, indent inside the class and then again inside main. Indent inside while loops and for loops. Also indent inside if statements and the else part. Put a blank line and then a comment as needed to describe “sections” of code.
I need help coding part 2.
Explanation / Answer
Executable code:
import java.util.Date;
interface AccInface
{
// method to deposit the amount
public void ac_deposit(double theMoney);
// method to deposit the money
public double ac_withdraw(double theMoney);
// method to return the account balance
public double ac_getBalance();
// method changes its status to "frozen"
public void ac_freeze();
// method changes its status to not "frozen"
public void ac_unfreeze();
// method to set the account limit
public void ac_setLimit(double newLimit);
// method to return the account limit
public double ac_getLimit();
// method to return its representation as a String.
public String toString();
}
class BankAccount implements AccInface
{
private double ac_balance;
private String ac_name;
private Date ac_creationDate;
private boolean ac_frozen;
private double ac_limit;
private static final double ac_MAXLIMIT=500;
private static int ac_accountNumber=0;
public static int ac_howMany;
// Constructor
public BankAccount()
{
this("classified");
}
// constructor with parameter
public BankAccount(String ac_name)
{
this.ac_name = ac_name;
this.ac_creationDate = new Date();
this.ac_frozen = false;
this.ac_limit = 300;
ac_howMany = ac_howMany +1;
this.ac_balance = 0;
ac_accountNumber=ac_accountNumber+1;
}
// method to return the account number
public static int getAccountNum()
{
return ac_accountNumber;
}
// Method to return number of accounts
public static int getNumAccounts()
{
return ac_howMany;
}
// method to deposit
public void ac_deposit(double theMoney)
{
//the deposit amount is negative
if(theMoney<0)
{
throw new IllegalArgumentException();
}
// the account is frozen
if(ac_frozen)
{
throw new IllegalStateException();
} else
{
ac_balance = ac_balance + theMoney;
}
}
// method to withdraw
public double ac_withdraw(double theMoney)
{
// to check the withdrawal amount is negative
if(theMoney<0)
{
throw new IllegalArgumentException();
}
// to check the account does not have enough money
else if(theMoney>ac_balance)
{
throw new IllegalArgumentException();
}
// to check the withdrawal amount is more than the ac_limit for that account
else if(theMoney>ac_limit)
{
throw new IllegalArgumentException();
}
// to check the withdrawal amount is not a multiple of 20
else if(theMoney%20!=0)
{
throw new IllegalArgumentException();
}
// to check account is frozen
if(ac_frozen)
{
throw new IllegalStateException();
} else
{
ac_balance = ac_balance - theMoney;
}
return theMoney;
}
// method to get account ac_balance
public double ac_getBalance()
{
return ac_balance;
}
// Method to ac_freeze the account
public void ac_freeze()
{
//changes its status to "ac_frozen"
ac_frozen = true;
}
// Method to ac_unfreeze the account
public void ac_unfreeze()
{
//changes its status to not "frozen"
ac_frozen = false;
}
// Method to set the account ac_limit
public void ac_setLimit(double newLimit)
{
// the new ac_limit is negative
if (newLimit < 0)
{
throw new IllegalArgumentException();
}
// the new ac_limit is larger than ac_MAXLIMIT
else if (newLimit > ac_MAXLIMIT)
{
throw new IllegalArgumentException();
}
if(ac_frozen)
{
throw new IllegalStateException();
}else
{
ac_limit = newLimit;
}
}
// method to get the ac_limit
public double ac_getLimit()
{
//returns the current ac_limit for the account
return ac_limit;
}
// method to display account information
public String toString()
{
return "Account Information "+
"Name:"+ac_name+" "+
"Account Number:"+getAccountNum()+" "+
"Balance:"+ac_balance+" "+
"Withdrawal Limit:"+ac_limit+" "+
"Creation Date:"+ac_creationDate;
}
}
// Driver class
public class Program5Driver
{
public static void main(String args[])
{
// Declare the required variables
double cash;
BankAccount swissBankAccount;
BankAccount myAccount,myAccount2,myAccount3;
//account creation using default constructor
System.out.println(" ****** BankAccount creation using default constructor ");
swissBankAccount = new BankAccount();
// try catch for exception handling
try
{
System.out.println(" ****** Printing ");
System.out.println(swissBankAccount);
}
catch(IllegalArgumentException er)
{
er.printStackTrace();
}
catch(Throwable er)
{
System.out.println("(WRONG EXCEPTION)");
er.printStackTrace();
}
//create a new BankAccount
System.out.println(" ****** BankAccount creation using the parametrized constructor ");
// try catch for exception handling
try
{
myAccount = new BankAccount("Bill Gates");
myAccount.ac_deposit(435.50);
myAccount.ac_deposit(10);
myAccount.ac_withdraw(60);
//print it (to see what the constructor put in)
System.out.println(" ****** Printing it to see what is in it ");
System.out.println(myAccount);
}
catch(IllegalArgumentException er)
{
er.printStackTrace();
}
catch(Throwable er)
{
System.out.println("(WRONG EXCEPTION)");
er.printStackTrace();
}
try
{
myAccount2 = new BankAccount("Karlson Vinayak");
myAccount2.ac_deposit(45);
myAccount2.ac_withdraw(20);
System.out.println(" ****** Printing it to see what is in it ");
System.out.println(myAccount2);
}
catch(IllegalArgumentException er)
{
er.printStackTrace();
}
catch(Throwable er)
{
System.out.println("(WRONG EXCEPTION)");
er.printStackTrace();
}
try
{
myAccount3 = new BankAccount("Scam Artist");
myAccount3.ac_deposit(10);
myAccount3.ac_withdraw(260);
System.out.println(" ****** Printing it ");
System.out.println(myAccount3);
}
catch(IllegalArgumentException er)
{
er.printStackTrace();
}
catch(Throwable er)
{
System.out.println("(WRONG EXCEPTION)");
er.printStackTrace();
}
try
{
//ask the BankAccount class how many accounts have been created - note that
//it is a static method so we ask the class.
System.out.println(" ****** Telling BankAccount to return the number of accounts ");
System.out.println("BankAccount reports " + BankAccount.getNumAccounts() + " account(s) so far");
}
catch(IllegalArgumentException er)
{
er.printStackTrace(); //will print the exception and then the call stack
}
catch(Throwable er)
{
System.out.println("(WRONG EXCEPTION)");
er.printStackTrace(); //will print the exception and then the call stack
}
}
}
Result:
****** BankAccount creation using default constructor
****** Printing
Account Information
Name:classified
Account Number:1
Balance:0.0
Withdrawal Limit:300.0
Creation Date:Tue Dec 01 09:21:14 IST 2015
****** BankAccount creation using the parametrized constructor
****** Printing it to see what is in it
Account Information
Name:Bill Gates
Account Number:2
Balance:385.5
Withdrawal Limit:300.0
Creation Date:Tue Dec 01 09:21:14 IST 2015
****** Printing it to see what is in it
Account Information
Name:Karlson Vinayak
Account Number:3
Balance:25.0
Withdrawal Limit:300.0
Creation Date:Tue Dec 01 09:21:14 IST 2015
java.lang.IllegalArgumentException
at BankAccount.ac_withdraw(Program5Driver.java:94)
at Program5Driver.main(Program5Driver.java:252)
****** Telling BankAccount to return the number of accounts
BankAccount reports 4 account(s) so far
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.