Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java 8 Train Schedule 1. Objective Between the two largest cities of Russia, St. Petersburg and Moscow make daily trips of N trains. For each

Java 8

Train Schedule

1. Objective

Between the two largest cities of Russia, St. Petersburg and Moscow make daily trips of N trains. For each train, it is known its departure time from St. Petersburg and the time of arrival in Moscow. You need to find the fastest train and its speed. It is guaranteed that the fastest train is determined uniquely.

In this project you need to use:

a) Objects

b) Arrays

c) File Input

2. User Interface

In this homework, you need to use the user interface client code for working with your supplier classes. See the reference on the page.

3. Input Data Files and storage

The user submitted text file your program will read from, follows this format:

a) start with one integer on its own line represents the distance between two cities

b) next line one integer (the number of trains in the everyday schedule 1 ? N ? 100)

c) followed by a series of lines that describe every train. This description contains 3 pieces of information:

a train name - a string with a length of 1 to 50 characters. It can contain letters of the English alphabet, spaces, numbers, and dashes ("-"). Lowercase and uppercase letters in the names of trains vary.

time of departure and arrival is indicated in a 24-hour format as follows HH MM. The format specifies hours (2 digits) and minutes (2 digits) separated by a space. The journey time for each of the trains is at least one minute and does not exceed 24 hours. Pay attention to this example, if the train departs at 19:50 and arrives at 8:30, it means that it arrives the next day and the time of journey is 12 hours and 40 minutes.

Your program does not have to be responsible for files that do not match this format (in other words, if the end user gives you a filename with bad data and the program crashes, that's ok). You can create any text file you want for testing (use a program like Notepad or any other basic text editor). Here is a file I've created for you to use: trains.txt.

4. Output

Print the name of the fastest train and its speed. Speed output in kilometers per hour and round to the nearest whole by mathematical rules. Follow the output format shown in the examples.

5. Code Specification

Implement the class specifications below. To get full credit, your program's public interface must match these descriptions exactly.

Objects Youll Create

Here are UML Class Diagrams for the objects you are to create. Pay attention to the diagram notation indicating whether methods are public (+) or private (-); ask questions if you need clarification. Understanding the model is of critical importance here.

Class: Time

Class: Train

Properties

(you figure out the private data needed)

Properties

(you figure out the private data needed)

Constructor

+Time(hour : Integer, minute : Integer)

Accessors

+ getHour() : Integer

+ getMinute() : Integer

Mutators

+setHour(Integer)

+setMinute(Integer)

+ timeBetween(Time) : Time

+ minBetween(Time) : Integer

+zeroTime(Integer) : String

+toString() : String

Constructor

+Train(name : String, departure : Time, arrival: Time, distance : Integer)

Accessors

+getDeparture () : Time

+getArrival () : Time

+getDistance () : Integer

Mutators

+setDeparture(Time)

+setArrival(Time)

+setDistance(Integer)

+averageSpeed() : Integer

+travelTime() : Time

+toString() : String

1.The method zeroTime() uses for formatting the output as follows:

Hour = 8, minute = 5. Output should be 08:05

2. The method minBetween() returns the difference between two Time objects in minutes. It will be needed for sorting trains using departure time.

Class: Schedule

Properties

- trains : Train[]

- distance : Integer

Constructor

+Schedule (fileName : String)

Methods

+ fillArray(Sring)

+ fastestTrain() : Train

+ sortDeparture()

+ toString() : String

The method sortDeparture() sorts an array of trains using departure time.

6. Suggestions

I hope by this point in the quarter you appreciate the benefit of working pieces of your solution one at a time. I recommend building and testing your Time and Train classes first. Next work on reading the data (the Schedule class method). Then tackle one or two methods at a time.

If it makes sense to decompose any of these methods, do so. However, the helper methods should all be private. Only these methods listed here should be part of the public interface.

Also, when dealing with the FileNotFoundException, every method that calls the Schedule constructor needs the throws clause in its method signature (unless you want to use try/catch blocks in any client code, but that is not a requirement for this assignment).

7. Documentation and Style

Make sure to write complete Javadoc comments for each class and each public method.

Include sufficient internal documentation.

Use appropriate style (variable names, indenting, class constants, etc.) throughout the program.

Be wise in your choice of instance variables. Only the data that an object needs to know (and remember) should be an instance variable.

8. Example input file

trains.txt

650 10 Night Express 23 55 8 15 Red Arrow 22 10 7 45 Star 10 30 19 25 Comfort 11 45 20 40 Budget 5 50 18 55 Day Rocket 11 55 16 00 Super Star 12 20 16 48 Good Morning 21 20 8 45 Best Choice 20 00 7 25 Sunrise 21 30 5 40

9. User Interface client code

ScheduleApp.java

import javax.swing.*;

import java.awt.*;

import javax.swing.filechooser.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import java.io.*;

import java.awt.FileDialog;

import java.awt.event.*;

import java.awt.event.MouseListener;

import java.util.Scanner;

public class ScheduleApp extends JFrame

{

Schedule schedule;

JTextArea textArea;

public ScheduleApp()

{

super();

setTitle("Schedule Of Train");

setSize(600, 800);

setResizable(false);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocation(100, 100);

JPanel panel = new JPanel();

panel.setLayout(null);

add(panel);

//Create Menu

Font font = new Font("TimesRoman", Font.PLAIN, 12);

JMenuBar menuBar = new JMenuBar();

JMenu menuFile = new JMenu("File");

menuFile.setFont(font);

JMenuItem menuItemFileOpen = new JMenuItem("Open");

menuItemFileOpen.setFont(font);

menuItemFileOpen.addMouseListener(new MouseListener(){

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {

fileOpen();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) { }

public void mouseClicked(MouseEvent e) { }

});

menuFile.add(menuItemFileOpen);

menuFile.addSeparator();

JMenuItem menuItemExit = new JMenuItem("Exit");

menuItemExit.setFont(font);

menuItemExit.addMouseListener(new MouseListener(){

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {

System.exit(1);

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) { }

public void mouseClicked(MouseEvent e) { }

});

menuFile.add(menuItemExit);

menuBar.add(menuFile);

setJMenuBar(menuBar);

textArea = new JTextArea();

textArea.setColumns(40);

textArea.setBounds(0, 0, getWidth(),getHeight()-20);

textArea.setBackground(Color.white);

textArea.setVisible(true);

textArea.setEditable(false);

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

font = new Font("TimesRoman", Font.PLAIN, 12);

textArea.setFont(font);

panel.add(textArea);

//Add JScrollPane

JScrollPane areaScrollPane = new JScrollPane(textArea);

areaScrollPane.setBounds(10, 0, getWidth()-14,getHeight()-20);

areaScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

panel.add(areaScrollPane);

this.validate();

this.setVisible(true);

this.toFront();

}

protected void fileOpen(){

JFileChooser fileChooser = new JFileChooser();

fileChooser.setCurrentDirectory(new File("."));

fileChooser.setFileFilter(new FileNameExtensionFilter("txt files", "txt", "all files", "*"));

int returnVal = fileChooser.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION){

String fileName = fileChooser.getSelectedFile().getPath();

try

{

schedule = new Schedule(fileName);

schedule.sortDeparture();

textArea.append(schedule.toString());

textArea.append("The faster train: ");

textArea.append(schedule.fastestTrain().toString() + " ");

}

catch(IOException ex)

{

System.out.println(ex);

}

}

}

public static void main(String[] args)throws IOException{

ScheduleApp myWindow = new ScheduleApp();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions

Question

Design a health and safety policy.

Answered: 1 week ago