Results 1 to 6 of 6

Thread: Boolean Trouble

  1. #1
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Boolean Trouble

    I'm getting things going with classes, but, if you run this code, my print statement still shows "aV1.on" as "true", though "turnOff" has been activated. If I change line 5 to "turnOn", it also shows as "true". Are the other two reference variables in Main causing any conflict? I would expect "on" to reflect any toggling that's done.

    Java Code:
    public class VariousPractice {
    
    	public static void main(String[] args) {
    
    		Stereo aV1 = new Stereo();
    		aV1.turnOff();
    		aV1.adjStation(1200);
    		aV1.adjVolume(17);
    		
    		System.out.println("aV1's power is now " + aV1.on + ", it's station is set to " 
    		+ aV1.rStation + ", and the volume is set to " + aV1.rVolume + ".");		
    		
    	}
    
    }
    class Stereo {
    	boolean on = false;
    	int rStation = 1;
    	int rVolume = 1;
    	
    	Stereo() {
    	} 
    	public void turnOn() {
    		on = true;
    	}
    	public void turnOff() {
    		on = false;
    	}
    	public void adjStation(int newStation) {
    		if (on = true && newStation >= 700 && newStation <= 1400)
    			rStation = newStation; 
    			}
    	public void adjVolume(int newVolume) {
    		if (on = true && newVolume >= 1 && newVolume <= 20)
    			rVolume = newVolume;
    			}
    	
    }
    What's the problem, here?

    Edit: If I set "adjVolume" and "adjStation" to a number outside of their respective boundaries, "on" finally gets set to "false". It remains "false" even if I set it back to "turnOn".
    Last edited by burrish; 10-10-2011 at 11:12 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Boolean Trouble

    What happens when you remove

    Java Code:
    AV1.adjStation(1200);
    AV1.adjVolume(17);
    If that makes the problem go away you can starting looking within those methods for something that turned the radio on. (note that AV1.turnOff() is not really needed because the radio is off when it is created. Also get into the habit of using standard Java conventions: variables start with a lowercase letter.)

  3. #3
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Boolean Trouble

    Quote Originally Posted by pbrockway2 View Post
    What happens when you remove

    Java Code:
    AV1.adjStation(1200);
    AV1.adjVolume(17);
    If that makes the problem go away you can starting looking within those methods for something that turned the radio on. (note that AV1.turnOff() is not really needed because the radio is off when it is created. Also get into the habit of using standard Java conventions: variables start with a lowercase letter.)
    ...duly noted on the convention, pbrockway. I've adjusted the variables. Thanks for the advice!

    I put the "turnOff" method in there for no other reason than the possibility of switching the hypothetical stereo off. Adjusting the integers in the station and volume variables does indeed make a difference...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Boolean Trouble

    Adjusting the integers in the station and volume variables does indeed make a difference.
    (Edit: *)

    Good. That's important because it provides information: now we know that the problem (the radio being turned on) is happening inside the adjStation() and adjVolume() methods. These methods are not supposed to turn the radio on, but they are.

    Look at how how you have written those methods and check the syntax of each statement. Now you know what you are looking for - something that looks it has something to do with the radio being on. When you find such statements check their syntax until you are absolutely sure.

    -----

    Edit: in fact I meant removing the lines altogether, as this gives the clearest evidence about the locus of the problem
    Last edited by pbrockway2; 10-10-2011 at 11:40 PM.

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Boolean Trouble

    In the adjStation() and adjVolume() methods, you should use a double equal sign == when checking if on is true, since == is comparison, while = is assignment. Better yet, remove the "== true" part entirely, since on is a boolean, so saying "on==true" is redundant, as it has the exact same value as "on". Saying "if(on==true)..." is like saying "if the statement 'the radio is on' is true, then..." as opposed to just "if the radio is on, then...".

  6. #6
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Boolean Trouble

    I commented out the lines to test, and I also made the necessary adjustments to my assignment operators per Diamond's reply. It was a basic oversight on my part, and the problem has been corrected. Thanks to both of you for your help!

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 3
    Last Post: 09-13-2011, 01:01 AM
  3. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 10:04 AM
  4. Boolean help
    By DMarsh12 in forum New To Java
    Replies: 8
    Last Post: 04-11-2011, 10:41 PM
  5. use boolean as 0 or 1
    By joost_m in forum New To Java
    Replies: 10
    Last Post: 04-13-2010, 12:22 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
  •