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 a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Using Java, write code as follows: Create an interface called Items. It should define at least...
Using Java, write code as follows: Create an interface called Items. It should define at least the following methods: public void add( Object item ) - adds a single object to the collection. public Object get( int index ) - returns an item at a specific index. public int size() - returns the number of items stored in the collection. public void addAll( Object[] items ) - adds all of the elements in the array to the collection. Write a...
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...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
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...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Write a Java code to represent a 1. Date class. As date class is composed of...
Write a Java code to represent a 1. Date class. As date class is composed of three attributes, namely month, year and day; so the class contains three Data Members, and one method called displayDate() which will print these data members. Test the Date class using main class named DateDemo. Create two objects of date class. Initialize the data fields in Date class using the objects, invoke the method displyaDate(). Date month : String year: int day : int displayDate():...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT