Inheritence
Page 1 of 111 Replies - 225 Views - Last Post: 01 December 2012 - 10:44 AM
#1
Inheritence
Posted 29 November 2012 - 03:37 PM
Here is the practice. We did not spend much time covering this material.
All classes should include a package statement and place all classes in the package lab910. Every class must have a toString method. Each class should have a public static void main method that will contain code to test the class’s methods.
1. A withdraw( ) method that checks that the amount being withdrawn is not greater than the current balance. If the amount to be withdrawn is less than balance, then subtract the amount from the balance and return true; else, leave the balance alone and return false.
2. A deposit( ) method that adds an amount to the balance. It should check that the amount deposited is >0, otherwise do not add the amount.
3. An equals( ) method which compares 2 account objects and returns a value of true only if their acctNum and balance fields are equal.
4. A static field numOfAccounts which keeps track of the number of accounts.
5. A static method getNumOfAccounts which returns the number of accounts.
6. The 1-argument Account constructor should assign an account number to each new account created; the assigned number should equal 100,000,000 plus the number of existing accounts plus 1. The constructor should then increase the number of accounts by one. (Note that the no-argument constructor must call the 1-argument constructor or this code will need to be repeated.)
Add 2 subclasses of class Account. Note the following:
7. The addInterest method of the SavingsAccount class and should deposit an amount of interest calculated by multiplying the balance to the intRate field multiplied by the specified no. of days / 365.
8. The constructors for both the SavingsAccount and CheckingAccount classes should pass the amt parameter to the superclass by calling superclass constructor.
9. The overProtect field of the CheckingAccount class stores a maximum overdraft amount (in dollars) by which the account may be overdrawn. One constructor allows the overdraft protection to be set, otherwise it defaults to 0 (i.e. there is no overdraft protection).
10. The CheckingAccount withdraw method does the following check: If there is an insufficient balance to cover the amount to be withdraw and there is overdraft protection then attempt to cover the difference (balance - amt). If there is insufficient protection, then the entire transaction fails, the balance is unaffected and a false result is returned.
Add Money class
11. The Money class implements the Countable interface. It keeps separate counts of the number of bank accounts and the number of coins owned. It accumulates the total balance in all accounts into the deposits field and total value of all coins into the cash field. (HINT: use the instanceof operator to determine whether a Countable passed to the addMoney method is a coin or a bank account.)
Create AccountTest class
12. Create an array that is able to hold 4 Account objects.
13. Create an Account object using default constructor. Set name to John Smith, deposit 1000.00 and withdraw 200.00. If there is enough money to withdraw then the system should print account number, owner and balance else print no withdraw insufficient funds. Put the Account object into the array.
14. Create a second Account object using one arg constructor. Set name to Mary Jones, deposit 1000.00 and withdraw 600.00. Should print as given in Step 1. Put the Account object into the array.
15. Create a SavingsAccount object with 500.00 for balance and 5% for rate. Name should be set to Jackson Brown. Add Interest for 365 days. The system should print account number, owner and balance. Put the SavingsAccount object into the array.
16. Create a CheckingAccount object with a balance of 500.00 and a overdraft of 200.00. Set name to George Jackson. Withdraw 600.00. If there is enough money to withdraw then the system should print account number, owner and balance else print no withdraw insufficient funds. Put the CheckingAccount object into the array.
17. Withdraw another 200.00 from the same Checking Account and print as in Step 4
18. Create a Coin object called quarter - values .25 and”Quarter”
19. Create a Coin object called dime - values 0.10 and “Dime”
20. Create a Money object called myMoney and add the SavingsAccount, CheckingAccount, quarter and dime object to myMoney.
21. Call getNetWorth to check your results.
22. Output the content of each object in the array using the object’s toString method.
Output should be similar to below
Withdrawal: Account: 100000001
Owner: John Smith
has balance of $800.0
No Withdrawal: Account: 100000002
Owner: Mary Jones
has Insufficient Funds
Balance: Account: 100000003
Owner: Jackson Brown
has balance of $525.0
Balance: Account: 100000004
Owner: George Jackson
has balance of $0.0
No Withdrawal: Account: 100000004
Owner: George Jackson
has Insufficient Funds
Net Worth: 1325.35
Replies To: Inheritence
#2
Re: Inheritence
Posted 29 November 2012 - 03:44 PM
#3
Re: Inheritence
Posted 29 November 2012 - 04:30 PM
mario6699, on 29 November 2012 - 06:37 PM, said:
I fully agree with that and I always do that. Many people post code here without a main() method in their classes. It is rery useful for unit testing.
Congratulate your teacher wjo is probably monitoring this forum anyhow
Beside that your instructions are quite clear and describe every step as the one quoted here.
Don't hesitate to post here if you have problem with the code.
And why is your topic title named "Inheritence" ?
#4
Re: Inheritence
Posted 29 November 2012 - 05:00 PM
Also I am not sure about the 1-argument Account constructor should assign an account number to each new account created; the assigned number should equal 100,000,000 plus the number of existing accounts plus 1. The constructor should then increase the number of accounts by one. (Note that the no-argument constructor must call the 1-argument constructor or this code will need to be repeated.)
Here is the code I have so far
public class Account {
private String Name;
private static int numOfAccounts=0;
double balance;
int accNum;
public boolean withdraw(double amount){
if (amount < balance){
amount-=balance;
}
return true;
}
public void deposit(double amount)
{
if(amount<=0)
balance += amount;
}
public boolean equals(double amount)
{
}
public static int getNumOfAccounts()
{
return numOfAccounts;
}
}
#5
Re: Inheritence
Posted 29 November 2012 - 05:10 PM
mario6699, on 29 November 2012 - 08:00 PM, said:
Seems weird to me ...
If the account numbers are the same sure that their balance will be the same
If you want to make "equals" to accounts with the same balance should simply be
public boolean equals(Account other) {
return balance == other.balance;
}
if you want to test on the Account number sure that
public boolean equals(Account other) {
return (number == other.number) && (balance == other.balance);
}
but as I said this situation is impossible unless you allow two Account objects with the same number which would definitively be a bug
#6
Re: Inheritence
Posted 29 November 2012 - 06:01 PM
This is what I have so far:
public class SavingsAccount extends Account {
private double intRate;
public SavingsAccount(){
}
// public void addInterest(int days){
public SavingsAccount(double amt){
super (amt);
}
#7
Re: Inheritence
Posted 29 November 2012 - 06:06 PM
#9
Re: Inheritence
Posted 29 November 2012 - 06:23 PM
Do you want to be able to create an account (whatever type) passing a balance as parameter ?
If it is the case your super class should have a constructor having that characteristic ...
#10
Re: Inheritence
Posted 01 December 2012 - 08:45 AM
Here is what I have so far.
Account(double amt){
this("", new Account(), "100 000 000")
}
#11
Re: Inheritence
Posted 01 December 2012 - 09:20 AM
public Account( double amount )
{
// any other required variables I haven't listed
this.accountNumber = 100000001 + numOfAccounts++;
// the rest . . .
}
#12
Re: Inheritence
Posted 01 December 2012 - 10:44 AM
8. The constructors for both the SavingsAccount and CheckingAccount classes should pass the amt parameter to the superclass by calling superclass constructor.
9. The overProtect field of the CheckingAccount class stores a maximum overdraft amount (in dollars) by which the account may be overdrawn. One constructor allows the overdraft protection to be set, otherwise it defaults to 0 (i.e. there is no overdraft protection).
Here is what I have so far:
public class CheckingAccount extends Account {
private double overProtect;
public CheckingAccount(double amt){
super (amt);
}
public CheckingAccount(double amt, double protect){
overProtect=protect;
}
}
|
|

New Topic/Question
Reply



MultiQuote




|