Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default help with arguments

    Hi and thanks for takin a look at my problem:)

    I have to build a share method which checks to see if two balances are equal then which ever is the larger balance it transfers 50% of the funds to the smaller account.
    I have work it out so it deducts the 50%, and can recognise when the two balances are equal.

    The problem is that it is not working out which is the larger balance and i have no idea how to set the reciever this.account.getBalance as an argument for transfer() in the type of MoneyFrog the class.:(:mad:

    My transfer method and share method in MoneyFrog class
    Java Code:
       
       /**
        * transfers half of the balance of the receiver frog to the argument frog.
        */
       public void transfer(MoneyFrog aFrog)
       {
          double anAmount = this.account.getBalance()/2;
          this.account.transfer(aFrog.account, anAmount); 
       }
     
       public void share(MoneyFrog aFrog)
       {
          this.lastReceived = false;
          this.lastGiven = false;
          if(aFrog.account.getBalance() != this.account.getBalance()) 
          {
             if(aFrog.account.getBalance() < this.account.getBalance())
          {
             this.transfer(aFrog);
             aFrog.setLastReceived(true);
             this.setLastGiven(true);
             
          }
          else
          {
             if(aFrog.account.getBalance() > this.account.getBalance())
             {
             
             this.transfer(aFrog);
             this.lastReceived = true;
             aFrog.lastGiven = true;
          }
       }
    }
    }
    The transfer method in the account class
    Java Code:
     public boolean transfer(Account toAccount, double anAmount)
       {
          if (this.debit(anAmount))
          {
             toAccount.credit(anAmount);
             return true;
          }
          else
          {
             return false;
          }
       }
    Any other parts needed pls ask.
    Pls any help would be great even a point in the right direction.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    So what is the problem?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Maybe this is the problem
    Java Code:
    if(aFrog.account.getBalance() < this.account.getBalance()) {
        this.transfer(aFrog);
    } else if(aFrog.account.getBalance() > this.account.getBalance()) {
        this.transfer(aFrog);
    }
    Regardless if which if statement is true you do the same thing. Does that sound right to you?

  4. #4
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    loool soz thought i made that clear :rolleyes:

    The problem is that the if loop is not working.
    It works up to the point that it recognises if two balances are equal

    however after that it is suppose to only transfer the balance of the larger balance to the smaller balance. Which it is not doing.
    Regardless of the size of the balances it just transfers 50% of the balance of the reciever to the balance of the arugument.

    When it should not make a difference if it is the reciever or arguemnt.

    I am only allowed to give one argument to the method share().
    and the input i am suppose to use to make the code work is this
    Java Code:
    MoneyFrog mf1 = new MoneyFrog("dave", "2345", 500);
    MoneyFrog mf2 = new MoneyFrog("dave", "2345", 400);
    mf1.share(mf2);
    mf2.share(mf1);
    hope that helps


    sorry spelling dyslexic

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    See second reply ;)

  6. #6
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    yes!! u got it
    i think i need to put in the argument of transfer() another input rather than aFrog as it was used first and instead put a reference to the receiver.
    However
    i cant work out how to do this
    i cant use
    this.account.balance ....etc as the arugment needs to be in the type of moneyFrog and not of account

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Java Code:
    this.transfer(aFrog);
    If I understand correctly, that will transfer money from aFrog to this. So in one of your if statements you want to do the opposite, transfer money from this to aFrog. Surely you can see how you fix this with a simple change.

  8. #8
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    for example i have inputted mf1.share(mf2)
    but mf2 is the larger balance it should take the money from mf2 and transfer it to mf1
    but mf1 is the reciever which i do not know what to use to represent it as an argument
    unlike mf2 which would be aFrog

  9. #9
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    ummm well u would think that it so would i :(
    but whateva i do it gives me errors

    whateva i put in the () of transfer gives me an error if its not aFrog

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Java Code:
    aFrog.transfer(this);

  11. #11
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    ive tried this.transfer(this.account).....no its not an account
    this.transfer()
    aFrog.transfer()
    aFrog.transfer(this.MoneyFrog).....no var called money frog
    etc
    as u can see this is one of the brain blocks
    Ive been up 24hrs straight working(not just on this) :P this is my first time asking for help and i hate it...:) so thanks 4 helpin

  12. #12
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    awwwwwwww now i feel dumb.........:((( thanks darlin

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Stop guessing, relax, clear your mind and think.

  14. #14
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    indeed....now on to my non changing colour problem :). Thanks again

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by smilinglady View Post
    for example i have inputted mf1.share(mf2)
    but mf2 is the larger balance it should take the money from mf2 and transfer it to mf1
    but mf1 is the reciever which i do not know what to use to represent it as an argument
    unlike mf2 which would be aFrog
    The code you have here:
    Java Code:
            this.transfer(aFrog);
    will transfer funds from this to aFrog, right?. So all you need to do is figure out how to use the same method to transfer from aFrog to this, right? Keep in mind that both this and aFrog are MoneyFrog objects.

    By the way, this is an excellent example of why choosing really good names for methods and parameters is so important. Imagine if instead of
    Java Code:
            public void transfer(MoneyFrog aFrog) {
    you had this?

    Java Code:
            public void transferTo(MoneyFrog toFrog) {
    (and even this assumes that somehow Frog is a good name to begin with -- but I'm guessing you're stuck with that)

    -Gary-

  16. #16
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Looks like I was a little slow with my response. Oh well, I'll leave it there anyway.

    -Gary-

  17. #17
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default

    Well thank u too its nice to know there is help i was pulling my hair out and its always with things so simple.
    :)

  18. #18
    smilinglady is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default new problem

    Im sorry guys but i cant work out why my colours are not changing its 1.43 in the morning now if you could help a bit more i would appreciate it

    Someone else had the same problem but his just didn't make sense

    The problem is this....
    I have set the colour using super. in the constructor as the superclass defines the colour to be green.
    then i have a method which should override it and change the colours depending on the boolean values.
    The boolean values change but the colour doesn't
    if i invoke the method setcolour into where they should change it tells me cant find method or the variable acolour
    i have written it how the question asked but its still not working


    my construtor:
    Java Code:
      public MoneyFrog (String holderName, String accountNumber, double anAmount)
         {
             super();
             this.lastGiven = false;
             this.lastReceived = false;
             this.account = new Account();
             this.account.setHolder(holderName);
             this.account.setNumber(accountNumber);
             this.account.setBalance(anAmount);
             super.setColour(OUColour.GREEN);
      
         }

    My setColour method
    Java Code:
     /**
         * sets the colour to blue if LastReceived is true or red if Lastgiven is true
         */
        public void setColour(OUColour aColour) 
        {
          if (getLastReceived() == (true))
          {
             aColour = OUColour.BLUE;
             
           }
          if (getLastGiven() == (true)) 
          {
             aColour = OUColour.RED;
          }
       }

    and then the place where the boolean values are defind in transfer(again:rolleyes:)
    Java Code:
       public void share(MoneyFrog aFrog)
       {
          this.lastReceived = false;
          this.lastGiven = false;
          if(aFrog.account.getBalance() != this.account.getBalance()) 
          {
             if(aFrog.account.getBalance() < this.account.getBalance())
          {
             this.transfer(aFrog);
             aFrog.setLastReceived(true);
             this.setLastGiven(true);
             
             
          }
          else
          {
             if(aFrog.account.getBalance() > this.account.getBalance())
             {
             
             aFrog.transfer(this);
             this.setLastReceived(true);
             aFrog.setLastGiven(true);
              
          }
       }
    and lastly the only other place set colour is mention is in the superclass constructor
    Java Code:
     public Frog()
       {
          super(); 
          this.setColour(OUColour.GREEN);
          this.setPosition(1);
       }

    plsss even an direction would be good

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Is this a GUI? If so you will need to make a call to repaint(). If not, more info needed.

    Btw making boolean comparisons is pointless.
    Java Code:
    if(boolean == true)
    same as
    if(boolean)
    and
    if(boolean == false)
    same as
    if(! boolean)

  20. #20
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Well, if you're only setting the color in the constructor, then why would you expect it to change later on? Maybe you want to call setColour() within your setLastReceived() and setLastGiven() methods?

    -Gary-

Page 1 of 2 12 LastLast

Similar Threads

  1. Arguments
    By maya700 in forum New To Java
    Replies: 12
    Last Post: 02-01-2011, 05:57 PM
  2. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  3. Default Arguments
    By Mark Robert in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 02:56 PM
  4. Replies: 6
    Last Post: 04-05-2010, 09:58 PM
  5. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 AM

Posting Permissions

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