And the Tester is : Could somebody help me with this? Write a suburbanTrain clas
ID: 3789691 • Letter: A
Question
And the Tester is :
Could somebody help me with this?
Write a suburbanTrain class. A SuburbanTrain moves along 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. Il can either move toward the end direction) or towards the start direction), Write a class suburban Train 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. Suburban Train has these methods publi 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 he start or end of the track publia 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) publio int distanceTostart calculates the distance the train is om the start in m public int distanceToEnd calculates the distance the train is from the end in miles Do not use if stateme hink abo tion needs to be re membered and define instance varia les to rememher that information. what informa HINT: You kan represent the direction with an integer: +1 ifthe train is moving toward the end -1 if it is moving towards the start. HINT: Remember that each stop is 5 miles from the previous stop. So if a train has moved 3 stops along the track, its distance from the start is 15. It will probably be easiest to call the start location stop number 0.Explanation / Answer
As mentioned in hints, we will use -1 and +1 to manage negative and positive directions.
Here when train moves towards end it is +ve(+1) direction
and when train moves towards start it is -ve(-1) direction.
I have also written the tester class for testing purpose. create to separate java files for both classes.
SuburbanTrain Class:
public class SuburbanTrain {
private final int stepSize=5;
private final int trackLength=50;
private int direction=1;
private int distanceToStart=0;
private int distanceToEnd=trackLength;
public void move(int numberOfSteps) {
distanceToStart+=(direction*numberOfSteps*stepSize);
distanceToEnd+=((-1)*direction*numberOfSteps*stepSize);
}
public int turn() {
this.direction*=-1;
return 0;
}
public int distanceToStart() {
return distanceToStart;
}
public int distanceToEnd() {
return distanceToEnd;
}
}
SuburbanTrainTesterClass:
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");
}
}