How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Eric M. Your Own Question
Eric M.
Eric M., Tutor
Category: Homework
Satisfied Customers: 2198
Experience:  9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
Type Your Homework Question Here...
Eric M. is online now
A new question is answered every 9 seconds

Hi, can you help me with a java programming assignment? Its

This answer was rated:

Hi, can you help me with a java programming assignment? It's the same one as the user who asked you for help with their freight train and railroad cars assignment.
Welcome here.
My name is XXXXX XXXXX I will help you.

Please provide me details.
If you have any file(s), Zip all files; upload the zipped file to or and post download link here.
Hey! Thanks for requesting me, I'll be glad to help. I'm sorry about the miscommunication there, the system should lock this question for me for a few minutes when you request me.

Have you already started the assignment? If so, could you provide the code you've generated? If not, I can provide my thoughts to you that I provided previously.

Customer: replied 3 years ago.

For Eric M,


Here is the assignment. It's long.


Sorting Railroad Cars


Suppose a freight train has n railroad cars. The train is going to visit n stations dropping off one car at each station. The railroad cars are unloaded from a ship and can arrive in any arbitrary order. We want to attach the railroad cars to the engine in the order such that at each station, we can simply detach the last car. The different stations are numbered 1 through n and the train will visit the stations in the order n, n-1, n-2, ...,2, 1. Each railroad car is labeled with the number of its destination station. Sorting the cars in the order 1 through n from front to back will allows us to simply detach the last car at each station.

The cars unloaded from the ship are on one end of a piece of track (the input track) and they are leave from the other end (the output track). Between these two ends are a number of holding tracks at right angles to the main track which are used to temporarily park cars in the sorting process. The diagram below shows an initial and final configuration with three holding tracks. Initially, all the cars are on the input side. At the end, they should all be on the output side in the correct order.





The following moves are permitted.

  • The front car in the input track can be moved directly to the output track

  • The front car in the input track can be moved to the top of a holding track

  • A car can be moved from the top of a holding track to the output track


Each holding track will be represented by a generic stack. We don't know how many holding tracks we will have in advance so we will use an ArrayList of generic Stacks (due to the way generics are handled in Java, there is a problem with creating a simple array of generic Stacks. The only coding difference you will encounter is that instead of accessing an element using the array notation , you will use the methods get(i) and set(i,element)).


The strategy for sorting the cars is to examine each car on the input track from front to back. If the current car is the next one needed on the output track, then move it to the output track, otherwise we move it to the top of a holding track.


If the current car moves from the input track to the output track, then the next car needed on the output track may already be on the top of a holding track. In fact the next several needed cars may be on the holding tracks. So after moving a car from the input track to the output track, we do the following. While the minimum car on the top of a holding track is the next one needed on the output track, move the minimum car from the top of its holding track to the output.


If instead the current car moves to the top of a holding track, which holding track should we use? We don't want to place a bigger car on top of a smaller because this would block the smaller car which we need to access first. So we will only place the car on top of a larger car. Suppose we have a choice of such tracks. E.g. suppose the current car is 2 and the top of two holding tracks are 9 and 5. Suppose we place car 2 on top of car 9 and that the next car is 7. Now we can't place the 7 on either track (2 and 5 are on top). If instead we had placed the 2 on the 5, we would be able to continue by placing the 7 on the 9. By placing the 2 on the 9, we have eliminated the potential holding spaces for cars 6, 7, and 8 on top of car 9. Hence when there is a choice, we should place the car on top of the smaller one in order to leave as many places open as possible for future cars. In general, if we need to move car i to a holding track, we place it on the holding track whose top element is the smallest element greater than i. If no holding track has a top element greater than i, then we must place the car on an empty track (if no such track is available, we need to report back that we cannot sort the cars). If you are having trouble making sense of this, see the sample output file and figure out why this strategy results in the indicated moves.



Thanks! Yep, that's the one I helped your classmate with. Have you already gotten started on it, and are you running into any particular errors? If so, I can help you with what you're having a difficulty with. Otherwise, I can give you an example for how it can be done.

Customer: replied 3 years ago.

Is it something like this?


public class TowersOfHanoiShowingStates


// data member

private static ArrayStack [] tower; // the towers are tower[1:3]

/** n disk Towers of Hanoi problem */

public static void towersOfHanoi(int n){

tower = new ArrayStack[4];

for (int i = 1; i <= 3; i++)

tower = new ArrayStack();

for (int d = n; d > 0; d--) // initialize

tower[1].push(new Integer(d)); // add disk d to tower 1

showTowerStates(n, 1, 2, 3);}

public static void showTowerStates(int n, int x, int y, int z){

if (n > 0)


showTowerStates(n-1, x, z, y);

Integer d = (Integer) tower[x].pop(); // move d from top

// of tower x to

tower[y].push(d); // top of tower y

System.out.println("Move disk " + d + " from tower "

+ x + " to top of tower " + y);

showTowerStates(n-1, z, y, x);




It's a bit confusing since I am not sure where to start.

The logic is similar, yes. Basically, in the Towers of Hanoi, you always put the next disc on the smallest available larger disc. Similarly, here, you always put the next car on the track with the nearest available later car.

Have you started on the actual code for the problem, though? Could you upload the file if you've made any changes to it (or even if you haven't)?
Customer: replied 3 years ago.

Here is the link to view the file:

Alright, looks like this is the same empty method. Want me to just write a sample working way of approaching the problem for you? I didn't save your classmate's file so it won't be the same, but it'll work.
Customer: replied 3 years ago.

Yes, please. Also, can you lay it out what I am supposed to do? I know I make a working code for the problem, but is that it?

Yep! Basically, you implement the sort() function.

The basic logic of the function is this:

While there are cars that have not been added to the output track:
- If the next needed car is available, add it to the output track.
- If the next needed car is not available, add it to the best available holding track.
- If none of the holding tracks will hold the car, say that you can't sort the cars.

The third case will only be hit if all the tracks are filled with cars that need to go before the next input car. So, if the output track needs car #1, the holding tracks are currently holding car #2, car #3, and car #4, and the next car on the input track is car #5, there is nowhere to put car #5 that will not permanently block one of the other cars.

Does that make sense? If so, I'll get started on the sample code ot make this happen. Thanks!
Customer: replied 3 years ago.

It makes some sense. It is like the towers of hanoi, which I became familiar with during my class Discrete Math. I just never implemented it into a code. That's the issue for me.

Alright, here you go: (link)

That should do it! If you have any questions on how it works, don't hesitate to let me know!

If this answer is acceptable, please remember to click 'Accept' or give positive feedback when prompted. If you need further assistance before this is deemed acceptable, please don't hesitate to ask for more detail. Thanks again!
Eric M., Tutor
Category: Homework
Satisfied Customers: 2198
Experience: 9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
Eric M. and other Homework Specialists are ready to help you
Customer: replied 3 years ago.



Thank you so much for your help! Take care!


You need to spend $3 to view this post. Add Funds to your account and buy credits.

Related Homework Questions