Page 3 of 3 FirstFirst 123
Results 41 to 55 of 55
Like Tree1Likes

Thread: What do I get?

  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    Look at using the ArrayList class for replacing an array.
    With a collection you will be using methods to add object references to the collection and for getting object references out of the collection. So instead of using just the one statment:
    floor[i].passengersWaiting
    my preference is to use two statements: One to get the reference to the desired object and two to use that reference to get at the contents of the object.

  2. #42
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Thanks, I will give it a shot! Thanks for the guidance...

  3. #43
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Ok, I created the following arrayLists:

    Java Code:
            ArrayList<Passenger> passengersHangingOut = new ArrayList<Passenger>();
            ArrayList<Passenger> passengersGoingUp = new ArrayList<Passenger>();
            ArrayList<Passenger> passengersGoingDown = new ArrayList<Passenger>();
            passengers = new Passenger[MAX_PASSENGERS];
              for (int i = 0; i < MAX_PASSENGERS; i++) {
              passengers[i] = new Passenger(i);
              }
              for (int i = 0; i < MAX_PASSENGERS; i++) {
               passengers[i].setPassengerCurrentFloor();
               passengers[i].setPassengerDestinationFloor();
                 if(passengers[i].getPassengerCurrentFloor() > passengers[i].getPassengerDestinationFloor()) {
    
                   passengersGoingDown.add(passengers[i]);
    
                 } else if (passengers[i].getPassengerCurrentFloor() < passengers[i].getPassengerDestinationFloor()) {
    
                   passengersGoingUp.add(passengers[i]);
    
                  } else {
    
                   passengersHangingOut.add(passengers[i]);
    
                  }
              }
    
    
    // The Random generator for my currentFloor and destinationFloor:
    
    
    public void setPassengerCurrentFloor() {
         Random randomGenerator = new Random();
           while(randomInt == 0) {
           for(int idx = 1; idx <=7; ++idx){
             randomInt = randomGenerator.nextInt(8);
            }
           }
           passengerCurrentFloor = randomInt;
         }
    
         public void setPassengerDestinationFloor() {
         Random randomGenerator = new Random();
           while(passengerCurrentFloor == randomInt || randomInt == 0) {
           for(int idx = 1; idx <=7; ++idx){
             randomInt = randomGenerator.nextInt(8);
            }
           }
           passengerDestinationFloor = randomInt;
         }
    My question is, how do I get out the individual objects in the ArrayList and then check the value of the currentFloor or Destination Floor?

    thanks

    Steve
    Last edited by Norm; 10-10-2011 at 03:50 PM. Reason: added code tags

  4. #44
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    how do I get out the individual objects in the ArrayList
    Look at the API doc for the ArrayList class. It has methods for getting individual objects from the ArrayList.

    Java Code:
    passengers = new Passenger[MAX_PASSENGERS];
    Why are you using arrays now?

    Why do you have 3 separate ArrayLists?

    What are you trying to have the program do with the code you posted?

  5. #45
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    per my homework, I need to create 3 collections (I used ArrayLists since not sure what the best collection is)...one collection for Passenger objects that are goingUp, one collection for Passenger objects that are goingDown, and one collection for Passenger objects that are just on the Floor of the building. I imagine once I create the 3 collections, I need to have my Elevator visit each Floor object and pick up the passengers "going in the correct direction" and then deposit them at the Destination Floor. Each passenger object has an instance variable of currentFloor and destinationFloor.

    In the code i posted I just created the ArrayLists, created the Passenger objects, and assigned the Passenger objects to the correct collection based on whether currentFloor was less than or greater than the destinationFloor.

    So now "what I think I need to do" is get the Passenger objects out of the correct collection (based on the Elevator direction), add the passengers to the elevator, and set the flag for the destinationFloor and then have the passengers exit the Elevator. I would put the passengers exiting the Elevator into the passengersHangingOut collection.

    I hope I have made sense in the above. Sorry my rookie status is showing.

  6. #46
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    In the code i posted I just created the ArrayLists, created the Passenger objects, and assigned the Passenger objects to the correct collection based on whether currentFloor was less than or greater than the destinationFloor.
    Work on the code for just this part. There is no need for an array and two loops here. You should do it in one loop.

    Also get rid of the coded numeric literals: 7 and 8. Define a final int for the number of floors.
    Last edited by Norm; 10-10-2011 at 03:52 PM.

  7. #47
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Thank you, I made the changes you suggested, and am left with the below question...

    Am I using the best collection for the purpose below? If I can use the ArrayList collection that may be the easiest for me, based on my experience level. The question is really how to get the object back out, and I want to test to see if the passengers currentFloor within the object is equal to the current floor that that Elevator is on, and then set the destinationFloor so that I stop later.

    So now "what I think I need to do" is get the Passenger objects out of the correct collection (based on the Elevator direction), add the passengers to the elevator, and set the flag for the destinationFloor and then have the passengers exit the Elevator. I would put the passengers exiting the Elevator into the passengersHangingOut collection.

  8. #48
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    how to get the object back out
    Look at the API doc for the class. It has methods for getting objects out of the collection.

    put the passengers exiting the Elevator into the passengersHangingOut collection.
    Is there a collection for each floor?

  9. #49
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Ok, I have been searching the web for an answer all day and I just can't find the correct solution for what I am trying to do.

    I have a ArrayList collection of Passenger objects:

    ArrayList<Passenger> passengersHangingOut = new ArrayList<Passenger>();
    ArrayList<Passenger> passengersGoingUp = new ArrayList<Passenger>();
    ArrayList<Passenger> passengersGoingDown = new ArrayList<Passenger>();

    *****************I populate my ArrayList with the following code:


    /**Create my array of passenger objects and put them in the appropriate collection */

    passengers = new Passenger[MAX_PASSENGERS];
    for (int i = 0; i < MAX_PASSENGERS; i++) {
    passengers[i] = new Passenger(i, i);
    passengers[i].setPassengerCurrentFloor();
    passengers[i].setPassengerDestinationFloor();
    elevatorStop[passengers[i].getPassengerDestinationFloor()] = true;
    if(passengers[i].getPassengerCurrentFloor() > passengers[i].getPassengerDestinationFloor()) {

    passengersGoingDown.add(passengers[i]);

    } else if (passengers[i].getPassengerCurrentFloor() < passengers[i].getPassengerDestinationFloor()) {

    passengersGoingUp.add(passengers[i]);

    } else {

    passengersHangingOut.add(passengers[i]);

    }
    }


    *****************I can print out the contents of the ArrayLists with the below:


    Iterator hangingItr = passengersHangingOut.iterator();
    while(hangingItr.hasNext()){
    System.out.println("Passengers hanging out!");
    hangingItr.next();

    }

    System.out.println("Passengers Going UP!");
    Iterator goingUpItr = passengersGoingUp.iterator();
    while(goingUpItr.hasNext()){
    System.out.println(goingUpItr.next());
    }

    System.out.println("Passengers Going Down!");
    Iterator goingDownItr = passengersGoingDown.iterator();
    while(goingDownItr.hasNext()){
    System.out.println(goingDownItr.next());
    }

    I get the following print out (example)...

    Passengers Going UP!
    Passengers current floor = 3
    Passengers destination floor = 5
    Passengers current floor = 1
    Passengers destination floor = 7
    Passengers current floor = 1
    Passengers destination floor = 7
    Passengers current floor = 2
    Passengers destination floor = 6
    Passengers current floor = 2
    Passengers destination floor = 4
    Passengers current floor = 1
    Passengers destination floor = 2
    Passengers current floor = 2
    Passengers destination floor = 6
    Passengers current floor = 5
    Passengers destination floor = 6
    Passengers current floor = 4
    Passengers destination floor = 5
    Passengers Going Down!
    Passengers current floor = 4
    Passengers destination floor = 1
    Passengers current floor = 4
    Passengers destination floor = 3
    Passengers current floor = 3
    Passengers destination floor = 1
    Passengers current floor = 7
    Passengers destination floor = 5
    Passengers current floor = 5
    Passengers destination floor = 2
    Passengers current floor = 3
    Passengers destination floor = 2
    Passengers current floor = 6
    Passengers destination floor = 2
    Passengers current floor = 5
    Passengers destination floor = 3
    Passengers current floor = 6
    Passengers destination floor = 4
    Passengers current floor = 3
    Passengers destination floor = 2
    Passengers current floor = 3
    Passengers destination floor = 2



    ****So I think I am "adding" passenger[i] objects to my ArrayList Collections*****let me know if you disagree...

    What I want to do is to be able to get the passenger[i] object out of the ArrayList Collection, and compare the current floor of this object with the currentFloor of my Elevator object and if equal do something...

    I am at a lost and don't see an docs on how to do this..

    thanks

  10. #50
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    What I want to do is to be able to get the passenger[i] object out of the ArrayList Collection
    Look at the API doc for the class. It has methods for getting objects out of the collection.
    What is the purpose of the array? The ArrayList should completely replace the need and usage of the array.
    The ArrayLists contain Passenger objects. There should not be any arrays needed or used.
    You should be creating Passenger objects and adding them to the ArrayLists. Get rid of the array.

  11. #51
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Ok, I got rid of the passengers[i] array and just put passengers object into the ArrayList. Any thought on previous question now that I have changed this:

    What I want to do is to be able to get the passengers object out of the ArrayList Collection, and compare the current floor of this object with the currentFloor of my Elevator object and if equal do something...

    I am at a lost and don't see an docs on how to do this..

    thanks

  12. #52
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    As an update:

    So I have stored passenger objects into my ArrayList collection. The passenger object has to instance variables.

    passengerCurrentFloor
    passengerDestinationFloor

    What I want to do is to get the passenger object out of the ArrayList, and then be able to check a local variable (currentFloor) against the passenger object instance variable (passengerCurrentFloor).

    Can this be done?

    thanks

  13. #53
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    I want to do is to be able to get the passengers object out of the ArrayList Collection ...
    I don't see an docs on how to do this..
    Go to this site and Find ArrayList in the lower left frame. Click on it and the API doc will be displayed in the righthand frame
    Java Platform SE 6

  14. #54
    smartino is offline Member
    Join Date
    Sep 2011
    Posts
    64
    Rep Power
    0

    Default Re: What do I get?

    Ok, I did look through the docs (I have done this before), my issue is my "rookie" status here and if you could guide me on the best methods to work with that would help my java education.

  15. #55
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What do I get?

    I want to do is to be able to get the passengers object out of the ArrayList Collection .
    Your question says what you want to do. Look at the get() method.

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •