12 Replies Latest reply: Oct 25, 2008 3:38 PM by 807589 RSS

    Boolean as a parameter...

    807589
      Hi!

      I'm fairly new to JAVA programming and i have encountered a problem. I am making a "program" where i am using a constructor where the parameters are the name and a boolean status (true / false). The boolean is already defined as true in the instance variable, the point of the constructor is that the user can be able to change it. Anyone have an idea on how to do that? My code looks like this:

      public class TV
      {
      private String name;
      private boolean turnedOn = true;

      public TV (String name , boolean turnedOn)
      {
      this.name = name;
      ??????????????? <--- this is where my problem lies, i don't know what to put here ;)
      }

      }

      Hope someone understands my problem :P

      cheers,
      owg
        • 1. Re: Boolean as a parameter...
          796440
          How were you able to write the line above it and not know how to do exactly the same thing for the second parameter?
          • 2. Re: Boolean as a parameter...
            796440
            Also, since you're setting the boolean in the c'tor, there's no point in setting it at declaration.
            • 3. Re: Boolean as a parameter...
              807589
              Thank you for answering :D

              so it will be:

              this.switchedOn = switchedOn?

              what will you write in as a parameter then?

              TV = new TV ("myTV" , ???);

              will I have to use an IF control statement?

              Cheers,
              owg
              • 4. Re: Boolean as a parameter...
                796440
                Gunola wrote:
                Thank you for answering :D

                so it will be:

                this.switchedOn = switchedOn?
                Yup.
                what will you write in as a parameter then?

                TV = new TV ("myTV" , ???);
                Well, that parameter has to be a boolean, right? What values can a boolean have? I know you know at least one of them because it's there in your original code.
                will I have to use an IF control statement?
                I don't know. Depends what you're tyring to do.
                • 5. Re: Boolean as a parameter...
                  807589
                  Gunola wrote:
                  TV = new TV ("myTV" , ???);
                  You've specified the String literal "myTV" for the String. There are two literals available for a boolean, true and false, so a valid line of code could look like say this,
                  TV tv = new TV ("myTV" , false);
                  • 6. Re: Boolean as a parameter...
                    807589
                    Ah :)


                    A boolean can be 'true' or 'false', right?


                    I try to make it so that the user can decide the name of the TV and wether it is turned on or off...

                    i was thinking something like this:

                    public TV (String name, boolean switchedOn)
                    {

                    this.name = name;

                    if (switchedOn.equals("true"))
                    {
                    this.switchedOn = true;
                    }

                    else
                    {
                    this.switchedOn = false;
                    }
                    }

                    Is that a way to do it?
                    • 7. Re: Boolean as a parameter...
                      807589
                      Gunola wrote:
                      Is that a way to do it?
                      No. The boolean literals are not Strings, they're boolean literals. So the constructor will look like this,
                      public TV (String name, boolean switchedOn)
                      {
                         this.name = name;
                         this.switchedOn = switchedOn;
                      }
                      Note that you don't need to instantiate TV objects with literals. You can also pass variables, like,
                      String someString = ..........; // user supplied value assigned to variable
                      boolean someBoolean = ...........; // likewise
                      //
                      TV tv = new TV(someString, someBoolean); // TV object created based on what a user supplied
                      • 8. Re: Boolean as a parameter...
                        807589
                        Ah!


                        Is this a way to do it?

                        System.out.print("Is the TV on?");
                        String answer = scanner input.nextLine();


                        if (answer.equals("yes"))
                        TV = new TV ("myTV" , true)

                        else
                        TV = new TV ("myTV , false)


                        Thanks for all the help, btw
                        • 9. Re: Boolean as a parameter...
                          796440
                          Gunola wrote:
                          Ah :)


                          A boolean can be 'true' or 'false', right?


                          I try to make it so that the user can decide the name of the TV and wether it is turned on or off...

                          i was thinking something like this:

                          public TV (String name, boolean switchedOn)
                          {

                          this.name = name;

                          if (switchedOn.equals("true"))
                          {
                          this.switchedOn = true;
                          }

                          else
                          {
                          this.switchedOn = false;
                          }
                          }

                          Is that a way to do it?
                          No.

                          You're seriously overcomplicating it.
                          public TV (String name, boolean switchedOn) {
                            this.name = name;
                            this.switchedOn = switchedOn;
                          }
                          
                          ...
                          
                          TV tv1 = new TV("blah", true);
                          TV tv2 = new TV("zuh", false);
                          • 10. Re: Boolean as a parameter...
                            807589
                            AHA! I did it :)

                            Thank you a lot jverd and Tinkerbell... :) You have been great help :)

                            Cheers :)
                            • 11. Re: Boolean as a parameter...
                              807589
                              Gunola wrote:
                              Is this a way to do it?
                              That's more like it. There are two steps. One is asking the user for input. The input will be in the form of Strings. Then you need to convert the user input to a proper internal represeantation, for example a boolean, or an int or a double. It's only if the internal representation is a String no conversion is necessary. But, you will also need to check that the user has entered something valid.

                              The above process is called "parsing user input".
                              • 12. Re: Boolean as a parameter...
                                807589
                                yeah... i figured it out :)

                                had some problems about seeing the differense between the identifiers, it was all logical errors :)

                                thanks again for clearing it up for me :)

                                cheers