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 1 of 2 12 LastLast
Results 1 to 25 of 35

Thread: Set the status

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

    Post Set the status

    So I wrote this code but I got two errors message "int can not be dereferenced" and "boolean can not be dereferenced"
    Can somebody help please?


    * Set the status of the ambulance whose ID is given.
    * @param id The ambulance's ID.
    * @param area The ambulance's new area.
    * @param free Whether it is now free or not.

     
        private ArrayList<Ambulance> cars;
     
        public AmbulanceControl()
        {
            cars = new ArrayList<Ambulance>();
     
        }
     
        public void setStatus(int id, int area, boolean free)
        {
            Iterator<Ambulance> ab = cars.iterator();
            while(ab.hasNext()){
                Ambulance ambulance = ab.next();
                int carsID = ambulance.getID();
                if(carsID == id ){
                    area.moveTo(); "int can not be dereferenced"
                    free.setBusy(); "boolean can not be  dereferenced"
     
                }
            }
     
        }
    Last edited by starm1x; November 16th, 2020 at 09:01 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    "int can not be dereferenced" and "boolean can not be dereferenced"
    int and boolean are primitives. Primitive data types do not have fields or methods.

    What statements have those errors?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    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: Set the status

    I fix it! i got the errors where i am trying to set whether is free or not free

    area.moveTo();
    free.setBusy();
    Last edited by starm1x; November 16th, 2020 at 09:02 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    area.moveTo();
    free.setBusy();
    You can not call methods using primitives.
    What class is the moveTo method in? What variable refers to an instance of that class?
    The same for the setBusy method.
    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: Set the status

    So I wrote this, and it works.

     public void setStatus(int id, int area, boolean free)
        {
            Iterator<Ambulance> ab = cars.iterator();
            while(ab.hasNext()){
                Ambulance ambulance = ab.next();
                if(ambulance.getID() == id){
                    ambulance.moveTo(area);
                    ambulance.setBusy();
                }
            }
        }

    but I have one test condition in which it does not work so how i should fix it?
     assertEquals(!oldStatus, selected.isFree());

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    test condition in which it does not work
    Please explain what "does not work" means?
    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: Set the status

    So I have this special test condition for that "setStatus" and sometimes it works but in some condition, it does not work. And when it does not work it shows this ' assertEquals(!oldStatus, selected.isFree()); ' part of the condition does not work and I can not understand why?

      public void moveAmbulance()
        {
            // Select a random Ambulance to be moved.
            Ambulance selected = shadow.get(rand.nextInt(shadow.size()));
            int oldArea = selected.getArea();
            boolean oldStatus = selected.isFree();
            control.setStatus(selected.getID(), oldArea + 1, !oldStatus);
            assertEquals(oldArea + 1, selected.getArea());
            assertEquals(!oldStatus, selected.isFree());
        }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    assertEquals(!oldStatus, selected.isFree());
    Why do you think those values should be equal?
    Where is the code that changes the value returned by the isFree() method?
    What does the setStatus method do?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    I did not write this method. There is no

    --- Update ---

    I think I fix it and now it is works

      public void setStatus(int id, int area, boolean free)
        {
            Iterator<Ambulance> ab = cars.iterator();
            while(ab.hasNext()){
                Ambulance ambulance = ab.next();
                if(ambulance.getID() == id){
                    ambulance.moveTo(area);
                    ambulance.setBusy();
                }
                if(free){
                    ambulance.setFree();
                } else{
                    ambulance.setBusy();
                }
     
            }
     
        }


    --- Update ---

    I need to do this, method called getNumberOfAmbulances. This one takes a single
    integer parameter representing an area and returns the number of
    Ambulance objects in the collection that are currently in that area.
    I do not know where to start? absolutely
      /**
         * 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)
        {
     
        }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    Does the Ambulance object have a location field (its area)?
    Is there a list of Ambulances that can be searched to see if any are in that area?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    Yes, there is.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    Ok, then look at all the Ambulance objects in the list for a matching location.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    I am trying too much and then return but I just can not it just does not work. I just can not understand it.

    public int getNumberOfAmbulances(int area)
        {
            for(Ambulance ab : cars){
                if(ab.getID() == area){
                    return true;
                }
            }
            return false;
        }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    it just does not work.
    Please explain. If there are error messages, copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    So I got ArraList ambulance. When the user will type area for example 1. it must return the number of the ambulances in that area.

    but i am doing something wrong. because when i am trying to return false or true it says that boolean can not be converted to int.

    because inside the loop i will need to check each Ambulance to see if it matches the area and, if it does, increment a counter.

     
      private ArrayList<Ambulance> cars;
     
        public AmbulanceControl()
        {
            cars = new ArrayList<Ambulance>();
     
        }
      public int getNumberOfAmbulances(int area)
        {
            for(Ambulance ab : cars){
                if(ab.getID() == area){
                    return true;
                }
            }
            return false;
        }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

     public int getNumberOfAmbulances(int area)
    If the method is supposed to count the number of Ambulances, why is it returning a boolean value like true or false?
    What is the method supposed to do? What value is it supposed to return?

    inside the loop i will need to check each Ambulance to see if it matches the area and, if it does, increment a counter.
    Where is the code for that?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    it suppose to return the number of an ambulance in that area

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    Ok, where is the code for that?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    it is everything what I got. The code must be in the getNumberOfAmbulance.


      private ArrayList<Ambulance> cars;
     
        public AmbulanceControl()
        {
            cars = new ArrayList<Ambulance>();
     
        }
     
      public void addAmbulance(Ambulance ambulance)
        {
            cars.add(ambulance);
        }
     
      public int getNumberOfAmbulances()
        {
            return cars.size();
        }
     
     public int getNumberOfAmbulances(int area)
        {
            for(Ambulance ab : cars){
                if(area == 0){
                    return ;
                }
            }
            return area;
        }

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    Your post about the method says:
    inside the loop i will need to check each Ambulance to see if it matches the area and, if it does, increment a counter.
    That sounds about right. Change the code so that it does what that comment says it should do.
    At the end of the method return the counter.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    I do not know-how. I am stuck where I should create a for each loop i just can not creat one.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    The method has the needed for loop. Use that one.
            for(Ambulance ab : cars){
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    than i am doing this: what should i return?

     if(ab.getID() == area){
     
                }

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set the status

    what should i return?
    What is the method supposed to do? Return that.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Set the status

    so i return the area:

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

Page 1 of 2 12 LastLast

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