Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java Assignment Description This assignment is an opportunity to review objects, inheritance and polymorphism. Write code that creates the following hierarchy. Train Monorail Highspeed

in java image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Assignment Description This assignment is an opportunity to review objects, inheritance and polymorphism. Write code that creates the following hierarchy. Train Monorail Highspeed Lightrail *** Please place all classes in one Java file. *** (This makes my life and the grader's life easier!) Your code will perform actions on these objects and display information proving you have properly used inheritance and polymorphism. Use the provided file Trains.txt to test your code. Specifications Read these specifications to get an overview of assignment. When writing code, first create the classes. 1. Add this assignment to your CS1450 project 2. Create a new Java class called LastName FirstName Assignment2 (in Eclipse: File -> New > Class) 3. Create the above hierarchy of classes in the LastName FirstName Assignment2.java file 4. Follow "CS1450 Programming Assignments Policy" 5. Write a test program (i.c. main) that: a. Opens the test file Trains.txt for reading, b. Creates a polymorphic array to store the trains. The 1" value in the file indicates how big to make the array. Use a regular array. DO NOT use an array list. c. For each line in the file: i. Read the type (highspeed, monorail, etc.), top speed, and name of the train. ii. Create the specific train object (the type tells you what kind of object to create). iii. Place the train object into the polymorphic trains array. d. AFTER all lines in the file have been read and all train d. AFTER all lines in the file have been read and all train objects are in the array: i. Iterate through the trains array and for each train display its: 1. Type, name, top speed, and benefit in a table 2. See output section for an example table 6. Test file information: a. Run your code on the provided file Trains.txt b. This file is an example so DO NOT assume that your code should work for only 8 trains in the order specified in the file. Number of trains highspeed 267 Shanghai Maglev lightrail 50 Vancouver Sky Train monorail 40 Walt Disney World Monorail Details for each train cog 9 Pikes Peak Cog See (d) below for details about highspeed 200 Eurostar e320 the format for these lines. monorail 45 Seattle Center Monorail lightrail 55 Denver RTD Lightrail cog 4.6 Mount Washington Cog c. 1. line is an integer value representing the number of trains in the file. d. Remaining lines contain details for each train. You must assume the format is as follows: Type Top Speed Name highspeed 267 Shanghai Maglev Classes Train Class Description Class that represents a generic train. o Superclass in the hierarchy. Private Data Fields o type - String representing the type of train (highspeed, monorail, etc.) o name - String representing the train's name o topSpeed - double representing the top speed the train can achieve o topSpeed - double representing the top speed the train can achieve Public Methods o Constructor: public Train(String type, String name, double topSpeed) - Creates a train by initializing all private data fields with incoming values for type, name, topSpeed o Getters . One for each private data field-type, name, topSpeed o Setters - No setters are needed (this is being done on purpose so do not include setters) public String benefit Returns a string representing the benefit of this train. The benefits of each train is shown in table below. Highspeed, Monorail, Lightrail, Cog Train Subelasses Description o Each class represents a specific train. Each of these classes must be a subclass of Train. Private Data Fields o None Public Methods o Constructors Incoming values are only name and topSpeed. Inside each constructor the type of train is known, so type is not pass in. . That is, when the code is in the Highspeed constructor, it knows that the type of train it is creating is a Highspeed train. public Highspeed(String name, double topSpeed) o Each subclass must override the benefit method in the Train class. @Override public String benefit Use the following benefits for each specific train: @Override public String benefit . Use the following benefits for each specific train: Train Type Benefit highspeed Travels at speeds between 125 and 267 mph monorail Minimal footprint and quicter lichtrail Tigher turning aus cog C anclimb grades up to 48% Must Dos and Tips Must Do: Use an array to store the trains not an ArrayList. Type, name, and topSpeed must be defined only in the superclass Train O These are common for all trains so we define them only in superclass Must Not: Do not include setters in the Train superclass. Tip: Build the classes first Before writing the code in main, build as much of the classes as possible . You need to have at least a skeleton of each class created before you can write the code to create trains in main. Tip: Reading from files When reading from a file, use the methods on the Scanner class to obtain the different pieces of data in the file. DO NOT read the entire line and then parse the line in your code. The Scanner methods will do the parsing for you. To review, read about the difference between the Scanner methods next and nextLine Output If your program is using inheritance and polymorphism correctly, your output will look like the following when running against the test file Trains.txt Output - Example Type Name Top Speed Benefit Do not include setters in the Train superclass. Tip: Build the classes first Before writing the code in main, build as much of the classes as possible You need to have at least a skeleton of each class created before you can write the code to create trains in main. Tip: Reading from files When reading from a file, use the methods on the Scanner class to obtain the different pieces of data in the file. DO NOT read the entire line and then parse the line in your code. The Scanner methods will do the parsing for you. To review, read about the difference between the Scanner methods next and nextLine Output If your program is using inheritance and polymorphism correctly, your output will look like the following when running against the test file Trains.txt Output - Example Type Name Top Speed Benefit Highspeed Shanghai Maglev 267.00 Travels speeds between 125 and 267 mph Lightrail Vancouver SkyTrain 50.00 Tighter turning radius Monorail Walt Disney Monorail 40.00 Minimal footprint and quieter Cog Pikes Peak Cog 9.00 C an climb grades up to 48% Highspeed Eurostar e320 200.00 Travels speeds between 125 and 267 mph Monorail Seattle Monorail 45.00 Minimal footprint and quieter Lightrail Denver Lightrail 55.00 Tighter turning radius Cog Mount Washington Cog 4.60 climb grades up to 48% Can

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions