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.

Page 2 of 2 FirstFirst 12
Results 26 to 35 of 35

Thread: Set the status

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Set the status

    so i return the area:
    Is that what the method is supposed to return? What about this statement of yours:
    inside the loop i will need to check each Ambulance to see if it matches the area and, if it does, increment a counter.
    And this:

      /**
         * Get the number of ambulances in the given area.
         * @param area The area of the ambulances.
         * @return the number of ambulances in the given area.
         */
        public int getNumberOfAmbulances(int area)
        {
     
        }
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Nov 2020
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set the status

    i know that it must increment so many like this

      if(ab.getID() == area){
                    area++;
                    return area;
                }


    --- Update ---

    I do not know I am struggling with this part of the code unbelievable.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Set the status

    If you have a pile of playing cards and the task was to count all the Kings in the pile, how would you count them?
    Pick one up, check if a King and if so add 1 to the count. Then pick up the next card and continue. When all the cards had been examined, the count would say how many Kings there were.


    In your code, do not return the count until ALL the cars have been examined.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Junior Member
    Join Date
    Nov 2020
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set the status

    Do i need to use else to return?

    --- Update ---

    So i can check that all cars has been checked?

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Set the status

    Maybe you should take a break for today and come back tomorrow when you have rested. Your questions make no sense.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Junior Member
    Join Date
    Nov 2020
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set the status

    yea you are right.

  7. #32
    Junior Member
    Join Date
    Nov 2020
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set the status

    Hi. I manage to solve some of the problems.

     public int getNumberOfAmbulances(int area)
        {
            int index;
            for(Ambulance ab : cars){
                if(ab.getID() == area){
                    index = cars.indexOf(ab);
                    index++;
                }
            }
            return area;
        }

    But I have one test condition where it sometimes works but also does not works. What is wrong with it?

    This is the condition which is testing!!

        @Test
        /**
         * Test the number of ambulances for location 1.
         */
        public void sizeChosenArea()
        {
            assertEquals(numberInChosenArea, 
                         control.getNumberOfAmbulances(chosenArea));
        }

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Set the status

    What is the purpose of this statement? It changes the value of the counter (index) and the current value of the count is lost.
      index = cars.indexOf(ab);

    Also index is a poor name for a counter. counter is a better name.
                if(ab.getID() == area){
    getID is a confusing name for a method that returns the area that the car is in. getArea() would be a better name
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Junior Member
    Join Date
    Nov 2020
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set the status

    it works. Thank you

    public int getNumberOfAmbulances(int area)
        {
            int index = 0;
            for(Ambulance ab : cars){
                if(ab.getArea() == area){
                   index++;
                }
            }
            return index;
        }

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Set the status

    You also need to change index to counter to make the code clearer.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. 404 Status Error
    By preeti91 in forum Java Servlet
    Replies: 2
    Last Post: September 5th, 2012, 07:10 AM
  2. HTTP Status 500
    By giri121 in forum Exceptions
    Replies: 5
    Last Post: April 10th, 2012, 09:58 AM
  3. HTTP Status 404
    By Star_pro in forum Java Servlet
    Replies: 0
    Last Post: February 28th, 2011, 02:17 PM
  4. Checking the Status of a checkbox
    By michaelz in forum Object Oriented Programming
    Replies: 16
    Last Post: August 9th, 2010, 06:44 AM
  5. HTTP Status 500 -
    By mqt in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 01:09 AM