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

Thread: Find the nearest free iterator

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

    Default Find the nearest free iterator

    So I am not sure if I am doing it correctly. I need to iterate through the list of an ambulance and then check if it is free. Then work out the distance of each area and return which is the nearest!

    I need help with it.


    /**
         * Find the nearest free ambulance to the given area.
         * Distance is measured by the absolute value of the
         * difference between two areas.
         * For instance, the distance between areas 1 and 5 is 4,
         * and the distance between areas 6 and 4 is 2.
         * If more than one ambulance is free and nearest to the given
         * area then the one with the lowest ID must be returned.
         * @param area Look for the ambulance nearest to this area.
         * @return the nearest ambulance, or null if there are no free ambulances.
         */
        public Ambulance findNearestFree(int area)
        {
            Iterator<Ambulance> ab = cars.iterator();
            while(ab.hasNext()){
                Ambulance ambulacne = ab.next();
                if(ambulacne.getArea() == area){
     
                }
            }
            return null;
        }
    Last edited by starm1x; November 19th, 2020 at 08:11 PM.

  2. #2
    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: Find the nearest free iterator

    1)check if it is free.
    2)Then work out the distance of each area and
    3)return which is the nearest!
    How do you do each of those three items in the above list?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Find the nearest free iterator

    So how I can check if it is free?

  4. #4
    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: Find the nearest free iterator

    What fields and methods does the class have? Do any of them have a value that says it is free?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Find the nearest free iterator

    so I need to run through the list and find which is free. Does not

    public class AmbulanceControl
    {
        private ArrayList<Ambulance> cars;
     
        public AmbulanceControl()
        {
            cars = new ArrayList<Ambulance>();
     
        }
       public Ambulance findNearestFree(int area)
        {
            Iterator<Ambulance> ab = cars.iterator();
            while(ab.hasNext()){
                Ambulance ambulacne = ab.next();
                if(ambulacne.getArea() == area){
                    return ambulacne;
                }
     
            }
           return null;
        }
     
    }


    --- Update ---

    Then I got test contention to test

     
     
     
        @Test
        /**
         * Find the nearest ambulance when there are none free.
         */
        public void findAmbulanceNoneFree()
        {
            shadow.clear();
            control = new AmbulanceControl();
            makeAmbulance(1, 1, false);
            assertEquals(null, control.findNearestFree(1));
        }
     
     
        @Test
        /**
         * Find the nearest ambulance when there is one free.
         */
        public void findAmbulanceOneFree()
        {
            shadow.clear();
            control = new AmbulanceControl();
            makeAmbulance(1, 1, false);
            Ambulance ambulance = makeAmbulance(2, 1, true);
            assertEquals(ambulance, control.findNearestFree(1));
        }
     
     
        @Test
        /**
         * Find the nearest ambulance when there is more than one free.
         */
        public void findAmbulanceLowest()
        {
            shadow.clear();
            control = new AmbulanceControl();
            makeAmbulance(2, 1, true);
            Ambulance ambulance = makeAmbulance(1, 1, true);
            assertEquals(ambulance, control.findNearestFree(1));
        }
     
        @Test
        /**
         * Find the nearest ambulance when there are none.
         */
        public void findAmbulanceLowestMultiple()
        {
            shadow.clear();
            control = new AmbulanceControl();
            for(int id = 5; id >= 2; id--) {
                makeAmbulance(id, 1, true);
            }
            Ambulance ambulance = makeAmbulance(1, 1, true);
            assertEquals(ambulance, control.findNearestFree(1));
        }
     
        @Test
        /**
         * Find the nearest ambulance in neighbouring areas.
         */
        public void findAmbulanceNeighbours()
        {
            shadow.clear();
            control = new AmbulanceControl();
            Ambulance ambulance = makeAmbulance(1, 1, true);
            makeAmbulance(2, 3, true);
            assertEquals(ambulance, control.findNearestFree(2));
        }
     
        @Test
        /**
         * Find the nearest ambulance in neighbouring areas
         * when only one is free.
         */
        public void findAmbulanceNeighboursOneFree()
        {
            shadow.clear();
            control = new AmbulanceControl();
            makeAmbulance(1, 1, false);
            Ambulance ambulance = makeAmbulance(2, 3, true);
            assertEquals(ambulance, control.findNearestFree(2));
        }
     
        @Test
        /**
         * Find the nearest ambulance in neighbouring areas.
         */
        public void findAmbulanceNeighboursFurther()
        {
            shadow.clear();
            control = new AmbulanceControl();
            makeAmbulance(1, 2, false);
            Ambulance ambulance = makeAmbulance(5, 1, true);
            assertEquals(ambulance, control.findNearestFree(3));
        }
    Last edited by starm1x; November 20th, 2020 at 06:58 AM.

  6. #6
    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: Find the nearest free iterator

    Where is the code that tests if the Ambulance is free?
    Why post all that code that has nothing to do with the problem of finding if the Ambulance is free?

    Work on one problem at a time. How do you determine if an Ambulance is free?

    Be sure to include these comments with your code:
    /**
         * Find the nearest free ambulance to the given area.
         * Distance is measured by the absolute value of the
         * difference between two areas.
         * For instance, the distance between areas 1 and 5 is 4,
         * and the distance between areas 6 and 4 is 2.
         * If more than one ambulance is free and nearest to the given
         * area then the one with the lowest ID must be returned.
         * @param area Look for the ambulance nearest to this area.
         * @return the nearest ambulance, or null if there are no free ambulances.
         */
        public Ambulance findNearestFree(int area)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Find the nearest free iterator

    so i did in this way but i think it can be done better!!
    [
    Last edited by starm1x; November 20th, 2020 at 08:38 AM.

  8. #8
    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: Find the nearest free iterator

    Who wrote that code? Do you have a good helper now?

    What happened to the iterator?

    Note: math is a very poor name for a variable that holds the distance between two areas.
    Also what is being held in the variable named: lengh?


    NOTE: OP deleted the code from the last post
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: September 7th, 2013, 10:35 AM
  2. Any free jar to exe free ware there to use?
    By tangara in forum Java SE APIs
    Replies: 1
    Last Post: May 14th, 2013, 08:18 AM
  3. [SOLVED] How to make Math.round() round to the nearest .1?
    By bdennin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 2nd, 2013, 07:23 PM
  4. Replies: 3
    Last Post: November 26th, 2012, 09:01 AM