Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: [OO] Boolean issue

  1. #1
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default [OO] Boolean issue

    I have the following code:

    Java Code:
    class Car {
    	String model;
    	String weight;
    	String year;
    	String color;
    	String drivername;
    	boolean hasEngine;
    	String hasEngineWord;{if (hasEngine == true){hasEngineWord="Yes";}else{hasEngineWord="No";}}
    	void StateData(){
    		System.out.println("Model: " + model);
    		System.out.println("Manufacture Year: " + year);
    		System.out.println("Color: " + color);
    		System.out.println("Weight: " + weight);
    		System.out.println("Has an engine: " + hasEngineWord);
    		System.out.println("Driver Name: " + drivername);
    		
    		
    	} // end StateData
    } // end Car class
    
    class MainProgram {
    	public static void main (String[] args) {
    	Car cooper = new Car();
    	cooper.model = "Mini Cooper S";
    	cooper.weight = "2,678 lb";
    	cooper.year = "2004";
    	cooper.color = "Red";
    	cooper.drivername = "Newbie";
    	cooper.hasEngine = true;
    	cooper.StateData();
    	} // end main
    } // end MainProgram
    I'm guessing it should be pretty easy to read since how small it is. However no matter what cooper.hasEngine is set to, it always returns false and says "No".

    true = Yes
    false = No

    This is the output, even when hasEngine is set to true.

    Java Code:
    Model: Mini Cooper S
    Manufacture Year: 2004
    Color: Red
    Weight: 2,678 lb
    Has an engine: No
    Driver Name: Newbie

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: [OO] Boolean issue

    Your code formatting is bad and this is making it difficult for you (and us) to see the error in your code. You have an initializer block that is setting the engine word to "no" before the engine word is ever set, and no degree of setting this will change it.

    Your code with better formatting:
    Java Code:
    class Car {
       String model;
       String weight;
       String year;
       String color;
       String drivername;
       boolean hasEngine;
       String hasEngineWord;
       
       // initializer block. called even before a constructor.
       {
          if (hasEngine == true) {
             hasEngineWord = "Yes";
          } else {
             hasEngineWord = "No";
          }
       }
    
       void StateData() {
          System.out.println("Model: " + model);
          System.out.println("Manufacture Year: " + year);
          System.out.println("Color: " + color);
          System.out.println("Weight: " + weight);
          System.out.println("Has an engine: " + hasEngineWord);
          System.out.println("Driver Name: " + drivername);
    
       } 
    }
    suggestions:
    • Get rid of the initializer block. There's no need for it and it will only confuse things.
    • use a real constructor to set the state of your class.
    • Decide on what String to output at the time the word is needed -- make getEngineWord() a method and return a String at that time that will depend on the state of the boolean.
    Last edited by Fubarable; 05-23-2012 at 11:55 PM.
    Vinx likes this.

  3. #3
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: [OO] Boolean issue

    Quote Originally Posted by Fubarable View Post
    Your code formatting is bad and this is making it difficult for you (and us) to see the error in your code. You have an initializer block that is setting the engine word to "no" before the engine word is ever set, and no degree of setting this will change it.

    Your code with better formatting:

    suggestions:
    • Get rid of the initializer block. There's no need for it and it will only confuse things.
    • use a real constructor to set the state of your class.
    • Decide on what String to output at the time the word is needed -- make getEngineWord() a method and return a String at that time that will depend on the state of the boolean.
    I've changed the code, I hope it's better in your eyes. It is in mine at-least, it works.
    Java Code:
    class Car {
       String model;
       String weight;
       String year;
       String color;
       String drivername;
       boolean hasEngine;
       String hasEngineWord;
        
       // initializer block. called even before a constructor.
      void getEngineWord() {
          if (hasEngine == true) {
             hasEngineWord = "Yes";
          } else {
             hasEngineWord = "No";
          }
       }
      
      void StateData() {
          System.out.println("Model: " + model);
          System.out.println("Manufacture Year: " + year);
          System.out.println("Color: " + color);
          System.out.println("Weight: " + weight);
          System.out.println("Has an engine: " + hasEngineWord);
          System.out.println("Driver Name: " + drivername);
     
       } // end StateData()
    } // end Car
    
    class MainProgram {
        public static void main (String[] args) {
        Car cooper = new Car();
        cooper.model = "Mini Cooper S";
        cooper.weight = "2,678 lb";
        cooper.year = "2004";
        cooper.color = "Red";
        cooper.drivername = "Newbie";
        cooper.hasEngine = true;
        cooper.getEngineWord();
        cooper.StateData();
        } // end main
    } // end MainProgram
    I have a question, though. I find more clarity through a person's opinion rather than a confusing definition from a dictionary.

    I want to verify.

    Both of these would be initializing blocks unless if there was a 'function'(I believe that's what it's called) before the method?
    Java Code:
    {
          System.out.println("Model: " + model);
          System.out.println("Manufacture Year: " + year);
          System.out.println("Color: " + color);
          System.out.println("Weight: " + weight);
          System.out.println("Has an engine: " + hasEngineWord);
          System.out.println("Driver Name: " + drivername);
     
       }
    Java Code:
     {
          if (hasEngine == true) {
             hasEngineWord = "Yes";
          } else {
             hasEngineWord = "No";
          }
       }
    Last edited by Vinx; 05-24-2012 at 12:35 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: [OO] Boolean issue

    An initializer block is a block of code without a method or constructor signature on top of it. I most often use it as a static initializer block -- to get static code up and running on class loading, and I rarely use non-static initializer blocks, though there are uses for them if you want several constructors to share the same start-up code.

    Also, don't do
    Java Code:
    if (foo == true) {
       baz();
    }
    You're checking if foo is true, so the == true is unnecessary and potentially confusing. Simplify by doing


    Java Code:
    if (foo) {
       baz();
    }
    And again, read up on and use constructor parameters to help you initialize your fields rather than having code manipulate a class's fields directly.

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

    Default Re: [OO] Boolean issue

    Java does not have functions (or procedures). It only has methods. All methods have what is called a signature. If you have a code block without a method signature then it is indeed an initialiser block. Note if you were to remove the braces around the code block it would not compile as the compiler thinks the code should be inside a method or constructor.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: [OO] Boolean issue

    Also note that it's neither necessary nor desirable to compare a boolean to true (nor false).
    Java Code:
    //if (hasEngine == true)
    if (hasEngine)
    Once you start comparing to true, why stop at once?
    Java Code:
    if ((((hasEngine == true) == true) == true) == true)
    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Boolean
    By must0995 in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 10:18 PM
  2. Boolean
    By Twixmaster123 in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 08:20 AM
  3. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  4. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  5. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 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
  •