Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: problem with toString and boolean operator

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with toString and boolean operator

    this compiles but i am not getting right output.......this is the output that i was hoping but somehow it is not getting right......can someone help me what is the problem with my code.

    public class Television
    {
    //Instance variables
    private int channel;
    private int volume;
    boolean power = false;
    String[] channelName = {"CBS","FOX","DISCOVERY","PBS","HBO","CNN","DISNEY ","CNN","TBS","USA"} ;


    //No argument constructors
    public Television ()
    {
    this(1,1);

    }

    //Overloaded constructor
    public Television(int ch, int vol)
    {
    setChannel (ch);
    setVolume (vol);
    }

    //Mutator methods
    public void powerOn()
    {
    this.power = true;
    }

    public void powerOff()
    {
    this.power = false;

    }

    public void increaseVolume()
    {
    volume++;
    }


    public void decreaseVolume()
    {
    volume--;
    }

    public void setVolume (int volume)
    {
    if (this.power == true)
    {
    if (volume < 0)
    {
    this.volume = 0;
    }

    if (volume > 10)
    {
    this.volume = 10;
    }
    }
    }

    public void setChannel (int channel)
    {
    if (this.power == true)
    {
    if (channel < 1)
    {
    this.channel = 1;
    }

    if (channel >10)
    {
    this.channel = 10;
    }
    }
    }


    //Accessor methods
    public int getVolume()
    {
    return volume;
    }

    public int getChannel()
    {
    return channel;
    }

    public String[] getChannelName()
    {
    if (this.power == true)
    {

    if (this.channel ==1)
    {
    channelName[0] = "CBS";;
    }
    if (this.channel ==2)
    {
    channelName[1] = "FOX";
    }
    if (this.channel ==3)
    {
    channelName[2] = "DISCOVERY";
    }
    if (this.channel ==4)
    {
    channelName[3] = "PBS";
    }
    if (this.channel ==5)
    {
    channelName[4] = "HBO";
    }
    if (this.channel ==6)
    {
    channelName[5] = "CNN";
    }
    if (this.channel ==7)
    {
    channelName[6] = "DISNEY";
    }
    if (this.channel ==8)
    {
    channelName[7] = "CNN";
    }
    if (this.channel ==9)
    {
    channelName[8] = "TBS";
    }
    if (this.channel ==10)
    {
    channelName[9] = "USA";
    }
    }
    return channelName;
    }


    public String toString()
    {

    return String.format(" TV State: \n Channel No: %d \n Channel Name: %s \n Volume: %d",getChannel(), getChannelName(), getVolume() );
    }
    } //end class



    //television app

    public class TelevisionApp
    {
    public static void main (String[] args)
    {
    Television T1 = new Television(3,5);
    T1.powerOn();

    Television T2 = new Television ();
    T2.powerOn();
    T2.setChannel(4);
    T2.setVolume(10);

    System.out.println (T1.toString());
    System.out.println ("T2 = "+T2);



    }
    }

    //output should be something like this

    TV Status: On
    Channel No: 4
    Channel Name: HBO
    volume : 6


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    What output are you getting instead?

    In future surround your code with code tags to retain formatting and make it easier to read.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    i am getting
    TV set:
    channel No: 0
    Channel Name: j.lang.String.@4563467
    volume: 0

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    Channel Name: j.lang.String.@4563467

    The getChannelName method returns the entire array and not a single name.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    I dont know how to return array in toString method

    Also how do i print TV state On using toString method?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    The toString method is calling the getChannelName method. Why is your getChannelName method returning the whole array of channel names instead of a single channel name?

    BTW you can simplify you getChannelName method to a single line of code.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    because if the channel number is 1, we want to display channel Name as "CBS". Since there are 10 channels that is why i am returning whole array using if statement. I dont know if that is the right way to do......

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    No!

    You only want to return a single channel name. If the channel is 1 you want it to only return CBS. Doesn't that make sense?
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    I have confusion with array. i thought this will return only one value of array.......so i should write like return channelName[]; ....could you please help me how to return one value of array ?

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    Simple, put a number inside the square brackets.
    Improving the world one idiot at a time!

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    but channelName is based on channel number. so I cannot put a fixed number.Are you talking about public String[] getChannelName() section? TelevisionApp program is controlling the value. Could you please write the code because I am not getting what you are trying to say........

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    Quote Originally Posted by centralguide View Post
    but channelName is based on channel number.
    Hello!!!!!!
    Improving the world one idiot at a time!

  13. #13
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    is it wrong?

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    I was pointing out that you answered your own question of what goes inside the square brackets.
    Improving the world one idiot at a time!

  15. #15
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with toString and boolean operator

    if I write
    return channelName[channel];

    it gives me compilation error. It says
    required: String[]
    found: String

  16. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with toString and boolean operator

    Change the return type of the method. I have been saying all along that method should be returning a String
    Improving the world one idiot at a time!

Similar Threads

  1. Boolean problem
    By RepersGhost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 07:05 AM
  2. boolean problem.
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2013, 05:32 PM
  3. toString problem
    By 93tomh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 28th, 2012, 10:59 AM
  4. Operator || cannot be applied to java.lang.String, boolean
    By djl1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2011, 06:00 PM
  5. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 09:50 AM