View the step-by-step solution to:

Java Homework: Structural Recursion - A train. Write a set of classes that model a train - starting with an abstract class called TrainCar as follows:...

Java Homework: Structural Recursion - A train. Write a set of classes that model a train - starting with an abstract class called TrainCar as follows:


public abstract class TrainCar{

protected double itsDistanceFromHome;

protected TrainCar isNextConnectedCar;


public TrainCar(TrainCar nextCar){

itsNextConnectedCar = nextCar;

itsDistanceFromHome = 0.0;

public abstract void advance(double howFar);

public abstract Boolean isMemberOfValidTrain();

}


Using TrainCar, extend it into Engine, BoxCar, and Caboose with their own constructors and implementation of the advance and isMemberOfValidTrain methods. Then write a class Train that only knows about the Engine. A valid train consists of an engine followed by some number of BoxCars ending with a Caboose. Calling isMemberOfValidTrain on the Engine should return true if this actually is the setup and false if it is not. Calling advance(double) on the engine should advance the entire train.

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question