Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a SuburbanTrain class. A suburbanTrain moves a track from the start to the

ID: 3789664 • Letter: W

Question

Write a SuburbanTrain class. A suburbanTrain moves a track from the start to the end. There are stops every 5 miles. The track is 50 miles long. The SuburbanTrain moves from one stop to another. It can either move toward the end (+ Direction or towards the start (- direction). Write a class SuburbanTrain that models this behavior.

SuburbanTrain class has a default constructor that sets the train at the start moving toward the end (+ direction). You can think of this as a number line with only positive numbers and zero.

SuburbanTrain has these methods:

1.Public void move (int numberOfStops) - moves the train the specified number of stops in the current direction. Assume the user is well behaved and never tries to move beyond the start or end of the track
2.Public void turn () - reverses the direction of the train. If the train was moving from start to end (+ direction), after the method executes, the train will be moving toward the start (- direction)
3.Public int distanceToStart () - calculates the distance the train is from the start in miles
4.Public int distanceToEnd () - calculates the distance the train is from the end in miles

Do not use if statements.

----------------------------------------------------------------------------------------

The tester is :

SuburbanTrainTester.java

Explanation / Answer

Hi,

Please see below the java classes. Please comment for any queries/feedbacks.

Thanks,

Anita

SuburbanTrain.java


public class SuburbanTrain {

   private int stop ;
   private String direction ;
  
   //Constructor
   public SuburbanTrain(){
       stop = 0; //Setting initial stop as start
       direction = "StartToEnd";
   }
   public int getStop() {
       return stop;
   }

   public void setStop(int stop) {
       this.stop = stop;
   }

   //to move train
   public void move (int numberOfStops) {
       stop = this.getStop() + numberOfStops;
   }

   //To turn the train
   public void turn (){  
      
       switch(this.direction){
           //Setting the direction
           case "StartToEnd": this.direction = "EndToStart";
                               this.stop = this.stop * -1;
                               break;
           case "EndToStart": this.direction = "StartToEnd";
                               this.stop = this.stop * -1;
                               break;
       }

   }
   //to calculate the distance from start
   public int distanceToStart () {
       int distance = this.getStop() * 5;
       return distance;
   }

   //to calculate the distance from end -> 50 - distanceToStart
   public int distanceToEnd (){

       int distanceFromEnd = 50 - distanceToStart();
       return distanceFromEnd;

   }
}

SuburbanTrainTester.java

public class SuburbanTrainTester
{

public static void main(String[] args)
{
SuburbanTrain train = new SuburbanTrain();
train.move(3);
train.move(5);
System.out.println("Distance to start: " + train.distanceToStart());
System.out.println("Expected: 40");
System.out.println("Distance to end: " + train.distanceToEnd());
System.out.println("Expected: 10");
  
train.turn();
train.move(4);
train.turn();
train.move(2);
System.out.println("Distance to start: " + train.distanceToStart());
System.out.println("Expected: 30");
System.out.println("Distance to end: " + train.distanceToEnd());
System.out.println("Expected: 20");
}

}

Sample output:

Distance to start: 40
Expected: 40
Distance to end: 10
Expected: 10
Distance to start: 30
Expected: 30
Distance to end: 20
Expected: 20

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote