Results 1 to 7 of 7
- 01-13-2011, 09:36 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Can you help with this class? Boolean variables acting weird
Hi folks
This is my first post, and I'd like to begin by saying thanks for your help. I've constructed a class of type MoneyFrog, here's the code:
Now, when the method setColour() is evoked, the getter methods are both set to false, even though I've explicitly set then to different values in the consturctor (setLastGiven(true), setLastReceived(false).Java Code:import ou.*; /** * Class MoneyFrog. MoneyFrog class takes another object * MoneyFrog as an argument. Both objects compare balances. Where * one object balance is greater than the other a share() message is * sent where the richer MoneyFrog object will transfer half of the difference * of the balance to the poorer object. Both objects will change colour on * successful transfer. Where the balances of each object are equal * no transfer takes place and the objects will not change colour. * * @author (James Monteith) * @version (0.1) */ public class MoneyFrog extends Frog { /* instance variables */ private boolean lastReceived; private boolean lastGiven; private Account account; /** * Sets the value of the receiver to the value of the argument */ public void setLastReceived(boolean lastReceived) { this.lastReceived = lastReceived; } /** * Returns the value of the receiver */ public boolean getLastReceived() { return this.lastReceived; } /** * Sets the value of the receiver to the value of the argument */ public void setLastGiven(boolean lastGiven) { this.lastGiven = lastGiven; } /** * Returns the value of the receiver */ public boolean getLastGiven() { return this.lastGiven; } /** * Returns the value of the argument account of * object type Account */ public Account getAccount(Account account) { return this.account; } /** * Constructor for MoneyFrog */ public MoneyFrog (String holderName, String accountNumber, double anAmount) { super(); setLastGiven(false); setLastReceived(true); account = new Account(); account.setHolder(holderName); account.setNumber(accountNumber); account.setBalance(anAmount); } /** Method to test the value of the argument. Invokes the * logic if the return value is true */ public void setColour(OUColour aColour) { System.out.println(getLastReceived()); if (this.getLastReceived()) { System.out.println(" FROM Set Colour getLastReceived is " + getLastReceived()); } if (this.getLastGiven()) { System.out.println("FROM set Colour getLastGiven is " + getLastGiven()); } } public void reportValues() { System.out.println("getLastReceived:started as " + lastReceived); System.out.println("getLastReceived: " + getLastReceived()); System.out.println("getLastReceived: started as " + lastGiven); System.out.println("getLastReceived: " + getLastGiven()); } }
But when I envoke the reportValues() method the instance variables are reported as their correct values from the constructor. Why can't the setColour() method see the correct ruturn values?
Once again, thanks for your help.
- 01-13-2011, 11:30 PM #2
Chances are the error occurs in code you did not post. Possibly between creating the MonkeyFrog object and calling the setColour method, one or both of the boolean setter methods are being called.
- 01-13-2011, 11:44 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Hi Junky. Thanks for taking the time to reply. Not sure what you mean though. When I want to create my new object (I'm using BlueJ by the way) I invoke the main method (if this is correct, still learning here) with the following code:
The arguments specified are for the Account object created in the constructor.Java Code:MoneyFrog f = new MoneyFrog("jazzer", "1234", 100.00);
Once the MoneyFrog object is created, I inspect this and the boolean instance variables lastReceived and lastGiven have the correct values (set in the constructor), but the setColourAs() method befaults both getter methods to false (even though the reportValues() method returns the correct values). I'm so confused. Please help!:confused:
- 01-13-2011, 11:57 PM #4
What I was suggesting was something along the lines of
Java Code:MonkeyFrog frog = new MonkeyFrog(); // boolean values ok here frog.setLastReceived(false); // boolean value(s) not ok here frog.setColour();
- 01-14-2011, 05:02 AM #5
Is this some other method or a typo? I can't see any method setColourAs() here.
There is a possibility as Junky has suggested, that may be your setter methods are getting called somewhere out of this class.
Can you post the sequence of your calling? Like when you create class object, then when you call reportValues() and when you call setColour() methods. That would give a clear idea about what is happening.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-14-2011, 12:24 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Hi folks
First, I'd like to say thanks for looking into this for me. Junky, you hit the nail onthe head, thanks. My problem was that when creating the new object of MoneyFrog I wasn't calling on the setColour() method but invoking the setLastReceived() boolean values.
Thanks again folks. No doubt I'll be back
- 01-14-2011, 04:08 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Mabey I was a bit quick making this as resolved. I'm still having issues with this. Let me try to explain.
My method setColout() is Overloading the same method from it's superclass. I wrote the following code:
I then construct the object, and send the messages as per Junk's post, i.e.:Java Code:import ou.*; /** * Class MoneyFrog. MoneyFrog class takes another object * MoneyFrog as an argument. Both objects compare balances. Where * one object balance is greater than the other a share() message is * sent where the richer MoneyFrog object will transfer half of the difference * of the balance to the poorer object. Both objects will change colour on * successful transfer. Where the balances of each object are equal * no transfer takes place and the objects will not change colour. * * @author (James Monteith) * @version (0.1) */ public class MoneyFrog extends Frog { /* instance variables */ private boolean lastReceived; private boolean lastGiven; private Account account; /** * Sets the value of the receiver to the value of the argument */ public void setLastReceived(boolean lastReceived) { this.lastReceived = lastReceived; } /** * Returns the value of the receiver */ public boolean getLastReceived() { return this.lastReceived; } /** * Sets the value of the receiver to the value of the argument */ public void setLastGiven(boolean lastGiven) { this.lastGiven = lastGiven; } /** * Returns the value of the receiver */ public boolean getLastGiven() { return this.lastGiven; } /** * Returns the value of the argument account of * object type Account */ public Account getAccount(Account account) { return this.account; } /** * Constructor for MoneyFrog */ public MoneyFrog (String holderName, String accountNumber, double anAmount) { //super(); this.setColour(OUColour.GREEN); this.setLastGiven(true); this.setLastReceived(false); this.account = new Account(); this.account.setHolder(holderName); this.account.setNumber(accountNumber); this.account.setBalance(anAmount); } /** Method to test the value of the argument. Invokes the * logic if the return value is true */ public void setColour() { if (getLastReceived()) { setColour(OUColour.BLUE); System.out.println(" FROM Set Colour getLastReceived is " + getLastReceived()); } if (getLastGiven()) { setColour(OUColour.RED); System.out.println("FROM set Colour getLastGiven is " + getLastGiven()); } } }
And it all works perfectly. The original object is creates as green as per the constructor, boolean values are correct. When the messages 2 and 3 above are sent the boolean values change appropriately and the object changes colour.Java Code:MoneyFrog j = new MoneyFrog("James", "100", 100.00); j.setLastReceived(true); j.setColour();
Brill
Trouble is, I need the method setColour() to OVERRIDE the superclass method. The superclass method signature is as follows:
So I amended my method to the following:Java Code:public void setColour(OUColour aColour)
Full code hereJava Code:public void setColour(OUColour aColour) { aColour = OUColour.GREEN; if (getLastReceived()) { aColour = OUColour.BLUE; setColour(aColour); System.out.println(" FROM Set Colour getLastReceived is " + getLastReceived()); } if (getLastGiven()) { aColour = OUColour.RED; setColour(aColour); System.out.println("FROM set Colour getLastGiven is " + getLastGiven()); } }
Now, where the problem lies is this. I need the override method setColour(OUColour aColour) to take the initial values set by the constructor at the point where I create my object. For example, say I set setLastReceived(true) in the constructor. When I create my object is should envoke the setColour() method, and my object should now be blue as the getLastReceived() method is returning true. But this doesn't happen, when I create my object and it runs through the code is sets the booleans to a default of false (i.e. ignoring the constructor). It does the same with the this.setColourAs(OUColour.GREEN) within the constructor. It's as though the new object is ignoring the constructor values and defaulting it's attributes. Why is this?Java Code:import ou.*; /** * Class MoneyFrog. MoneyFrog class takes another object * MoneyFrog as an argument. Both objects compare balances. Where * one object balance is greater than the other a share() message is * sent where the richer MoneyFrog object will transfer half of the difference * of the balance to the poorer object. Both objects will change colour on * successful transfer. Where the balances of each object are equal * no transfer takes place and the objects will not change colour. * * @author (James Monteith) * @version (0.1) */ public class MoneyFrog extends Frog { /* instance variables */ private boolean lastReceived; private boolean lastGiven; private Account account; /** * Sets the value of the receiver to the value of the argument */ public void setLastReceived(boolean lastReceived) { this.lastReceived = lastReceived; } /** * Returns the value of the receiver */ public boolean getLastReceived() { return this.lastReceived; } /** * Sets the value of the receiver to the value of the argument */ public void setLastGiven(boolean lastGiven) { this.lastGiven = lastGiven; } /** * Returns the value of the receiver */ public boolean getLastGiven() { return this.lastGiven; } /** * Returns the value of the argument account of * object type Account */ public Account getAccount(Account account) { return this.account; } /** * Constructor for MoneyFrog */ public MoneyFrog (String holderName, String accountNumber, double anAmount) { //super(); this.setColour(OUColour.GREEN); this.setLastGiven(true); this.setLastReceived(false); this.account = new Account(); this.account.setHolder(holderName); this.account.setNumber(accountNumber); this.account.setBalance(anAmount); } /** Method to test the value of the argument. Invokes the * logic if the return value is true */ public void setColour(OUColour aColour) { aColour = OUColour.GREEN; if (getLastReceived()) { aColour = OUColour.BLUE; setColour(aColour); System.out.println(" FROM Set Colour getLastReceived is " + getLastReceived()); } if (getLastGiven()) { aColour = OUColour.RED; setColour(aColour); System.out.println("FROM set Colour getLastGiven is " + getLastGiven()); } } }
Thanks again folks and sorry for the long post.:confused:
Similar Threads
-
Acting Java as if it's C problem
By reis3k in forum New To JavaReplies: 13Last Post: 10-18-2010, 09:10 AM -
Call variables from one class to another
By mneskovic in forum New To JavaReplies: 17Last Post: 08-10-2010, 08:37 AM -
getting class to recognize variables from another class
By shadycharacter in forum New To JavaReplies: 1Last Post: 04-26-2010, 10:14 PM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM -
boolean variables
By ravian in forum New To JavaReplies: 3Last Post: 12-31-2007, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks