11 Replies - 580 Views - Last Post: 01 December 2012 - 10:44 AM Rate Topic: -----

#1 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Inheritence

Posted 29 November 2012 - 03:37 PM

Need help with this practice for my lab exam. I am not sure how to code this. Can anyone help me??

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

Is This A Good Question/Topic? 0
  • +

Replies To: Inheritence

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Inheritence

Posted 29 November 2012 - 03:44 PM

You have step-by-step, very clear instructions on what you have to do. Follow them, do them, and come back when you have some code and need some help.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Inheritence

Posted 29 November 2012 - 04:30 PM

View Postmario6699, on 29 November 2012 - 06:37 PM, said:

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.

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" ?
Was This Post Helpful? 0
  • +
  • -

#4 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Re: Inheritence

Posted 29 November 2012 - 05:00 PM

I am having trouble with An equals( ) method which compares 2 account objects and returns a value of true only if their acctNum and balance fields are equal.

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;
}
}

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Inheritence

Posted 29 November 2012 - 05:10 PM

View Postmario6699, on 29 November 2012 - 08:00 PM, said:

I am having trouble with An equals( ) method which compares 2 account objects and returns a value of true only if their acctNum and balance fields are equal.

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
Was This Post Helpful? 0
  • +
  • -

#6 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Re: Inheritence

Posted 29 November 2012 - 06:01 PM

I am having trouble with The constructors for both the SavingsAccount and CheckingAccount classes should pass the amt parameter to the superclass by calling superclass constructor.

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);
}

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Inheritence

Posted 29 November 2012 - 06:06 PM

Based on the code posted in your #4 the Account class does not have a constructor that expects a double as parameter so you can't do super(amt);
Was This Post Helpful? 0
  • +
  • -

#8 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Re: Inheritence

Posted 29 November 2012 - 06:16 PM

What should I use then?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Inheritence

Posted 29 November 2012 - 06:23 PM

Depends what you want
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 ...
Was This Post Helpful? 0
  • +
  • -

#10 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Re: Inheritence

Posted 01 December 2012 - 08:45 AM

I am stuck trying to figure out 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 what I have so far.

Account(double amt){
    this("", new Account(), "100 000 000")
} 

Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Inheritence

Posted 01 December 2012 - 09:20 AM

The Java Tutorials ™ use a similar example for bicycle serial numbers to show how class variables, static variables common to all instances of the class, are useful. That's what your numOfAccounts variable is for. So, when a new account is created:

public Account( double amount )
{
    // any other required variables I haven't listed

    this.accountNumber = 100000001 + numOfAccounts++;

    // the rest . . . 
}

Was This Post Helpful? 0
  • +
  • -

#12 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Re: Inheritence

Posted 01 December 2012 - 10:44 AM

I am having trouble with"
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;

    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1