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: Please solve my problem

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Please solve my problem

    Write a public method called halfCount, which is passed an int as a parameter and which performs successive (integer) divisions by 2 until 0 is reached. The method returns the number of divisions required until 0 is reached.

    Example: 2 --> 2 (2/2 = 1, then 1/2 = 0, so two successive divisions by 2 get you to zero)
    Example: 10 --> 4 (10/2 = 5; 5/2 = 2; 2/2 = 1; 1/2 = 0)

    (Hint: use a while loop in the body, and set up the loop so that at each loop iteration, the input value is halved. Use an auxiliary variable to count the number of loop iterations. When the loop finishes, return this count.)

    Note that in this example you must write a complete method, including the method header line.

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Please solve my problem

    Hi,

    We don't do other people's homework or give complete solutions here. This forum is for people to get specific help on specific issues. If you're stuck on a particular problem in that assignment, post your code (inside code tags), describe the undesired behavior, and mention what you've tried that hasn't worked.

    Best of luck!

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    Yea!!Sorry that! I got the general idea for this question, can I post my answer and get some advices?

    --- Update ---

    I don't know why my answer still not currect;

    public int halfCount(int j){
    while (j > 0){
    j = j / 2;
    numDivisions++;
    }
    return numDivisions;
    }

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please solve my problem

    I don't know why my answer still not currect;
    Hi, what is your expected answer ? What is wrong here ?
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    When I put my code into Java, it said that: Screen Shot 2017-10-25 at 7.47.21 AM.jpg
    My code should return a number of divisions.
    Attached Images Attached Images

  6. #6
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    When I put my code into Java, it said that: Screen Shot 2017-10-25 at 7.47.21 AM.jpg
    My code should return a number of divisions.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Please solve my problem

    The code you posted looks good to me. I think you have a syntax error somewhere above that while loop? If you can't find it, please post the entire project code including static void main.

Similar Threads

  1. Help Me to Solve this Problem..
    By hardik mehta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2014, 06:13 AM
  2. Please solve my problem
    By Shambhavi Singh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 17th, 2014, 02:51 AM
  3. Please solve my problem
    By Supun Pramoda in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 7th, 2013, 11:14 PM
  4. please solve my problem
    By anarica in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2013, 07:35 AM
  5. Please solve my problem
    By Nurhafizah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2012, 09:11 PM