Results 1 to 7 of 7
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Red face 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:

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

    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.

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

    Default

    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.

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.
    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:

    Java Code:
    MoneyFrog f = new MoneyFrog("jazzer", "1234", 100.00);
    The arguments specified are for the Account object created in the constructor.

    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:

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

    Default

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

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by jazzermonty View Post
    but the setColourAs() method befaults both getter methods to false
    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Talking

    Quote Originally Posted by Junky View Post
    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();
    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

  7. #7
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by jazzermonty View Post
    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
    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:

    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());
          }
       }
    }
    I then construct the object, and send the messages as per Junk's post, i.e.:

    Java Code:
    MoneyFrog j = new MoneyFrog("James", "100", 100.00);
    j.setLastReceived(true);
    j.setColour();
    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.

    Brill

    Trouble is, I need the method setColour() to OVERRIDE the superclass method. The superclass method signature is as follows:

    Java Code:
     public void setColour(OUColour aColour)
    So I amended my method to the following:

    Java 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());
          }
         
       }
    Full code here

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

    Thanks again folks and sorry for the long post.:confused:

Similar Threads

  1. Acting Java as if it's C problem
    By reis3k in forum New To Java
    Replies: 13
    Last Post: 10-18-2010, 09:10 AM
  2. Call variables from one class to another
    By mneskovic in forum New To Java
    Replies: 17
    Last Post: 08-10-2010, 08:37 AM
  3. getting class to recognize variables from another class
    By shadycharacter in forum New To Java
    Replies: 1
    Last Post: 04-26-2010, 10:14 PM
  4. Replies: 1
    Last Post: 01-03-2010, 09:48 PM
  5. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 04:58 AM

Tags for this Thread

Posting Permissions

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