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: 2179
Experience:  9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
29802289
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

Resolved Question:

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.
Thanks!
Submitted: 10 months ago.
Category: Homework
Expert:  LogicPro replied 10 months ago.
Hi,
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 http://wikisend.com or http://ge.tt and post download link here.
Expert:  Eric M. replied 10 months ago.
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.

Thanks!
Customer: replied 10 months 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.


 


}

Expert:  Eric M. replied 10 months ago.
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.

Thanks!
Customer: replied 10 months 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.

Expert:  Eric M. replied 10 months ago.
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 Railroad.java file if you've made any changes to it (or even if you haven't)?
Customer: replied 10 months ago.

Here is the link to view the railroad.java file:


 


http://wikisend.com/download/918158/Railroad.java

Expert:  Eric M. replied 10 months ago.
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 10 months 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?

Expert:  Eric M. replied 10 months ago.
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 10 months 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.

Expert:  Eric M. replied 10 months ago.
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: 2179
Experience: 9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
Eric M. and 7 other Homework Specialists are ready to help you
Customer: replied 10 months ago.

Sir,


 


Thank you so much for your help! Take care!

Expert:  Eric M. replied 10 months ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3121
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3035
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/lanis/2009-4-1_233717_phput9xef_c1pm.jpg Lani S.'s Avatar

    Lani S.

    Tutor

    Satisfied Customers:

    2457
    Registered Nurse, Internet Researcher, Private Tutor
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2341
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1975
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1873
    Bachelors Degree and CPA with Accounting work experience