d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java, Would This Work?
Add Reply New Topic New Poll
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Sep 27 2014 08:41pm
load: a public method that returns an int and takes one parameter that is a Queue of Passengers. It removes the passengers from the queue and pushes them onto passengers. Then it returns the number of passengers loaded. Note: the only methods you may call on a Queue of Passengers are add(), element(), offer(), peek(), poll(), and remove() (some of these methods won’t be needed).

would this follow that method description? passengers is the stack

Code

public int load(Queue x)
{
int loaded = 0;
Iterator iter = x.iterator();
while(iter.hasNext())
{
x.remove(passengers);
loaded +=1;
}
return loaded;
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 27 2014 08:45pm
No. you have the signature right, but you're not doing what you're supposed to be doing.

1) It removes the passengers from the queue
2) pushes them onto passengers.
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Sep 27 2014 08:47pm
Quote (carteblanche @ Sep 27 2014 10:45pm)
No. you have the signature right, but you're not doing what you're supposed to be doing.

1) It removes the passengers from the queue
2)pushes them onto passengers.


So, what would the x.remove(passengers) do then? I thought it would remove the object and place it into passengers.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 27 2014 09:31pm
Quote (pyromaniac09 @ Sep 27 2014 10:47pm)
So, what would the x.remove(passengers) do then? I thought it would remove the object and place it into passengers.


1) does that even compile? i dont think there's a remove(Object) or remove(Stack) method in Queue
2) what made you think that? the documentation seems pretty clear: http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#remove()

/edit: your profile says you're in columbus. do you go to CSU by any chance? friend of mine is getting her degree there and she said the cs teachers don't care about the class and dont teach well :/

This post was edited by carteblanche on Sep 27 2014 09:38pm
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Sep 27 2014 10:17pm
Quote (carteblanche @ Sep 27 2014 11:31pm)
1) does that even compile? i dont think there's a remove(Object) or remove(Stack) method in Queue
2) what made you think that? the documentation seems pretty clear: http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#remove()

/edit: your profile says you're in columbus. do you go to CSU by any chance? friend of mine is getting her degree there and she said the cs teachers don't care about the class and dont teach well :/


it compiles fine which is why i was confused.

yes I go to CSU, and I have been wondering lately if its worth it... I'm not sure what my other options are but I really hate switching schools....
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 27 2014 10:19pm
I would assume by the context that the class that contains the load method you are implementing contains some field like this...



Code
private Stack<Passenger> passengers;


in which case the method would be defined as such

Code

//Load a Queue of Passengers in the Stack of Passengers
//Return the number of passengers loaded
public int load(Queue<Passenger> toLoad)
{
//do some load stuff
}


If that is the case then you are looking to pop Passenger objects off the Queue, and push them onto the Stack until the Queue is empty. Keep a counter of how many passengers were loaded, and return that.

In psuedocode you would want to do something like this

Code

//Load a Queue of Passengers in the Stack of Passengers
//Return the number of passengers loaded
public int load(Queue<Passenger> toLoad)
{
//1. declare and initialize an integer counter to
//keep track of the number of passengers loaded

//2. loop while there are still passengers in the queue using peek()

//2a. pop() a passenger off the toLoad queue
//2b. push() a passenger onto the passengers stack
//2c. increment the counter by 1

//3. return the integer counter

}

Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 27 2014 10:22pm
Quote (carteblanche @ Sep 27 2014 10:31pm)
1) does that even compile? i dont think there's a remove(Object) or remove(Stack) method in Queue


Queue is a Sub-Interface of Collection, which means it implements remove(Object), which is why it compiles.
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Sep 27 2014 10:30pm
Quote (Minkomonster @ Sep 28 2014 12:19am)
I would assume by the context that the class that contains the load method you are implementing contains some field like this...



Code
private Stack<Passenger> passengers;


in which case the method would be defined as such

Code
//Load a Queue of Passengers in the Stack of Passengers
//Return the number of passengers loaded
public int load(Queue<Passenger> toLoad)
{
    //do some load stuff
}


If that is the case then you are looking to pop Passenger objects off the Queue, and push them onto the Stack until the Queue is empty. Keep a counter of how many passengers were loaded, and return that.

In psuedocode you would want to do something like this

Code
//Load a Queue of Passengers in the Stack of Passengers
//Return the number of passengers loaded
public int load(Queue<Passenger> toLoad)
{
    //1. declare and initialize an integer counter to
    //keep track of the number of passengers loaded
 
    //2. loop while there are still passengers in the queue using peek()
   
    //2a. pop() a passenger off the toLoad queue
    //2b. push() a passenger onto the passengers stack
    //2c. increment the counter by 1
   
  //3. return the integer counter 
 
}


you would be correct here is the description my instructor gave us.

Imagine an elevator that is so narrow that passengers have to stand one in front of the other which means the first person on is the last to get off (probably not one you would want to use). When the elevator arrives at a floor all of the passengers have to get off one at a time so that the passengers for that floor can exit and then the other passengers have to get back on in reverse order so that their original relative positions in the elevator are preserved. When passengers get on the elevator, the passengers already on the elevator have to move back to make room for them. That’s how this elevator works.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 27 2014 10:34pm
Code
public class Elevator
{
private Stack<Passenger> passengers;

public Elevator()
{
passengers = new Stack<Passenger>();
}

//Load a Queue of Passengers in the Stack of Passengers
//Return the number of passengers loaded
public int load(Queue<Passenger> toLoad)
{
//1. declare and initialize an integer counter to
//keep track of the number of passengers loaded

//2. loop while there are still passengers in the queue using peek()

//2a. pop() a passenger off the toLoad queue
//2b. push() a passenger onto the passengers stack
//2c. increment the counter by 1

//3. return the integer counter

}
}
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Sep 27 2014 10:55pm
Quote (Minkomonster @ Sep 28 2014 12:34am)
Code
public class Elevator
{
    private Stack<Passenger> passengers;
   
    public Elevator()
    {
        passengers = new Stack<Passenger>();
    }
   
    //Load a Queue of Passengers in the Stack of Passengers
    //Return the number of passengers loaded
    public int load(Queue<Passenger> toLoad)
    {
        //1. declare and initialize an integer counter to
        //keep track of the number of passengers loaded
 
        //2. loop while there are still passengers in the queue using peek()
   
        //2a. pop() a passenger off the toLoad queue
        //2b. push() a passenger onto the passengers stack
        //2c. increment the counter by 1
   
      //3. return the integer counter 
 
    }
}


thats exactly what i was thinking i guess i was just confused by the code I was trying and what it was actually doing. thanks for the help you all are very helpful
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll