Question

In: Computer Science

Using Java, define a train class and write code to demonstrate its functionality The train should...

Using Java, define a train class and write code to demonstrate its functionality

The train should run between Boston and New York, stopping at different, different station along the way.

Provide output to indicate the train’s current status.

Solutions

Expert Solution

/**************************************Train.java*******************************/

import java.util.LinkedList;

public class Train {

   //A Linked list store the stations
   private LinkedList<String> stations;
   private String currentStation;
   private String lastStation;

   public Train() {

      
       stations = new LinkedList<String>();
       /*
       * modify the station names between Boston and New York
       */
       stations.add("Boston");
       stations.add("Staion A");
       stations.add("Staion B");
       stations.add("Staion C");
       stations.add("Staion D");
       stations.add("Staion E");
       stations.add("New York");
       currentStation = stations.getFirst();//get the first station
       lastStation = stations.getLast();//get the last station
       stations.poll();//train start run from first station
   }

   public LinkedList<String> getStations() {
       return stations;
   }

   public String getCurrentStation() {
       return currentStation;
   }

   public String getLastStation() {
       return lastStation;
   }

   public void run() {

       if (!stations.isEmpty()) {
           currentStation = stations.poll();
       } else {

           System.out.println("You have reached at: " + lastStation);
       }
   }

   public static void main(String[] args) {

       Train train = new Train();

       System.out.println("Train at: " + train.getCurrentStation());
       System.out.println("Train's last station: " + train.getLastStation());

       train.run();
       train.run();

       System.out.println("Now train at: " + train.getCurrentStation());

       train.run();
       train.run();

       System.out.println("Now train at: " + train.getCurrentStation());
      
       train.run();
       train.run();
       train.run();
      
       System.out.println("Now train at: " + train.getCurrentStation());
      

   }

}
/*******************************output*****************/

Train at: Boston
Train's last station: New York
Now train at: Staion B
Now train at: Staion D
You have reached at: New York
Now train at: New York

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
Code in C++. Using ONE of the following themes to create a class and demonstrate the...
Code in C++. Using ONE of the following themes to create a class and demonstrate the idea. Use this to create a solution (two ADTs and a driver program) to demonstrate the following concepts. Create a one page summary to describe where you are using each of these concepts. Themes:(PICK ONE) a house has a door a semester has a holiday break a cell phone has a shatterproof case a chair has a cushion Concepts to include: composition separating interface...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT