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 7 of 7

Thread: boolean for increment method

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default boolean for increment method

    Hello my original attempted was to make increment method in the class. But my teacher want it to be in boolean instead of void

    public void increment() {
       if (count < maxValue) 
          count++;
       else
          System.out.println("Counter overflow.  Increment ignored.");
    };
    so it going to being something like this, but i am not very familiar with boolean. Anyone please point out a way to improve this. Thank you.
    public boolean increment() {
       return true;
    };


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: boolean for increment method

    Booleans aren't that hard to understand: they're either true or false, on or off, yes or no, etc.

    What it sounds like your teacher wants you to do is modify the first method to return true or false to indicate if the increment succeeded.

    A good way to think about booleans is to phrase a true/false question:

    The increment method succeeded in increasing count's value. True or false?

    The increment method failed to increase count's value. True or false?

    Depending on which of these two questions you want to answer your method's return value will mean something different. From what you've posted it isn't clear which your teacher wants, though I have a tendency to lean towards the first because that's the approach usually taken.

    You just need to modify your first method to properly answer this question without using System.out. This is console output, different from returning a value from a method.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: boolean for increment method

    This is what i came up with. It run but it doesn't say anything if it false.
    public boolean increment() {
    	if (count < maxValue)
    		{
                    count++;
    		return true;
                     }
    	else
    	//System.out.println("Increment ignored.");
    		return false;
    	};

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: boolean for increment method

    Normally the intent is for a method to perform work and return a result. The calling method will then decide what it wants to do with that result. This could be perform more computations, write it to a file, print out a message to the console, etc.

    I don't have your assignment so I'm not positive if that's what your teacher wants, but if I had to guess that is what I would do. It's best to read the assignment or check with a member of the teaching staff to ensure you have an acceptable solution.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: boolean for increment method

    Quote Originally Posted by helloworld922 View Post
    Normally the intent is for a method to perform work and return a result. The calling method will then decide what it wants to do with that result. This could be perform more computations, write it to a file, print out a message to the console, etc.

    I don't have your assignment so I'm not positive if that's what your teacher wants, but if I had to guess that is what I would do. It's best to read the assignment or check with a member of the teaching staff to ensure you have an acceptable solution.
    The problem here is there not actually an assignment. He just gave everyone the skeleton of the program, and expect the result. So I don't know what really to do . Well if there a better way to put that code please help me out, or the edit one be ok?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: boolean for increment method

    You're guess is as good as mine. My suggestion is to ask other students for what they did, and/or ask your teacher (the teacher is the best option because they most likely know what they want).

    That aside, if I were the teacher I would say your modified code is close to matching what I would consider to be good programming practice, the only things I can tell that are missing are comments.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: boolean for increment method

    thank you very much .

Similar Threads

  1. I need a little help with a boolean method.
    By bankston13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2012, 08:02 PM
  2. Method Equals, Boolean...
    By boys8goods in forum Object Oriented Programming
    Replies: 1
    Last Post: June 25th, 2011, 03:34 PM
  3. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 09:18 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:56 AM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 05:45 PM