Results 1 to 8 of 8

Thread: ReUsing methods

  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default ReUsing methods

    I'm having trouble reusing this method signature but using different variables. Here's what I mean.

    I have two classes:
    BankAccount.java
    SavingsAccount.java

    Java Code:
    public class BankAccount {
    	private double balance ;
    	private double myMonthlyInterest ;
    
    public double calculateInterest() {
    	myMonthlyInterest = balance * (rate / 12) ;
    	return myMonthlyInterest ;
    }
    
    }

    Java Code:
    public class SavingsAccount extends BankAccount {
    
    	private double balance2 ;
    	private double myMonthlyInterest2 ;
    }
    So I want to use the variables in SavingsAccount balance2 and myMonthlyInterest2; plug them in the calculateInterest() method in BankAccount then return a calculated myMonthlyInterest2.

    I was thinking super(keyword) Can someone shine some light. Much appreciated.
    Last edited by random0munky; 10-16-2009 at 09:17 AM.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Why does SavingsAccount need two balances? Doesn't a savings acount only have one?

    I guess I'm trying to figure out what you added a balance to your SavingsAccount class. Why don't you use the balance that exists in the BankAccount class?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    Why does SavingsAccount need two balances? Doesn't a savings acount only have one?

    I guess I'm trying to figure out what you added a balance to your SavingsAccount class. Why don't you use the balance that exists in the BankAccount class?
    There's two balances because there's a normal bankaccount and in the savings account which is pretty much totally different. I didn't write out all the code but the savings account and bank account manipulate balance really differently. In the main you would initialize such as:

    BankAccount b1 = new BankAccount(John John, 0.6) ;
    SavingsAccount s1 = new SavingsAccount(John John, 0.6)

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    OK, then your setup is wrong. By having your SavingsAccount extend BankAccount, you are saying that a "SavingsAccount is a BankAccount". However, from the description you just gave, a SavingsAccount has a BankAccount.

    If it's the later, then instead of extending bank account, you should include a bank account as a private member field in SavingsAccount.

    Java Code:
    public class SavingsAccount {
    
    	private double balance ;
    	private double myMonthlyInterest ;
    	private BankAccount bankAccount;
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    =/ Sorry the homework assignment tells me to have
    Java Code:
    public class SavingsAccount extends BankAccount  { 
    }
    Well for calculateInterest() in BankAccount how would I be able to use the method when I'm in Savings Account.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by random0munky
    Well for calculateInterest() in BankAccount how would I be able to use the method when I'm in Savings Account. I'm going to see about reusing the variables in BankAccount
    Since SavingsAccount extends BankAccount, all methods are inherited.

    Java Code:
    SavingsAccount mySavings = new SavingsAccount("My name", balance, interestRate);
    System.out.println("This month you earned " + mySavings.calculateInterest()).


    If possible, could you post the directions, because a savings account with two balances is "weird" and "confusing".

    Since the assignment has SavingsAccount extending BankAccount, this would imply that you could have, for example, a CheckingAccount that also extends BankAccount. The BankAccount class would contain fields and methods resposible for handling the account.

    Then, by declaring the calculateInterest method in the BankAccount class, you can use it for a SavingsAccount or CheckingAccount (if such a class existed and extended BankAccount).
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You haven't understood inheritance yet. Revise it. It's actually the point of the exercise you have been given.

  8. #8
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    Since SavingsAccount extends BankAccount, all methods are inherited.

    Java Code:
    SavingsAccount mySavings = new SavingsAccount("My name", balance, interestRate);
    System.out.println("This month you earned " + mySavings.calculateInterest()).


    If possible, could you post the directions, because a savings account with two balances is "weird" and "confusing".

    Since the assignment has SavingsAccount extending BankAccount, this would imply that you could have, for example, a CheckingAccount that also extends BankAccount. The BankAccount class would contain fields and methods resposible for handling the account.

    Then, by declaring the calculateInterest method in the BankAccount class, you can use it for a SavingsAccount or CheckingAccount (if such a class existed and extended BankAccount).

    I sent a private message to you.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  3. Get and Set Methods
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-27-2008, 03:06 PM
  4. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 06:31 AM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •