Question
Scenario A theme park has a number of rides modelled by two classes: Ride and RollerCoaster. On a given ride each car has a capacity,
Scenario
A theme park has a number of rides modelled by two classes: Ride and RollerCoaster. On a given ride each car has a capacity, and rides may be operational or not. A RollerCoaster is a ride that can have several cars.
The class ThemePark holds a list of all the rides in the park.
- a.Launch BlueJ, open the project TMA03_Q1 in the TMA03Download folder you unzipped earlier and then immediately save the project as TMA03_Q1_SolXX, in the TMA03Download folder, where XX represents your own initials.
The provided project contains the completed class ThemePark as well as the partially completed class Ride. You should make no changes to ThemePark.
In this part you will complete the class Ride which has been provided for you. Ride already has the following private fields as well as getter and setter methods for these and a toString method:
- name, the name of the ride, which is a String.
- carCapacity, of type int, which holds the capacity of a single car on the ride.
- operational, of type boolean, which is true if the ride is running and otherwise false.
i.Add a public constructor to the class Ride with the modifier and header public Ride(String aName, int aCarCapacity)
The constructor uses its two parameters to set the values of the corresponding fields.
The field operational should be set to false.
- ii.Add an equals method to Ride that overrides the Object equals method and returns true if the ride name and car capacity of the argument are the same as this object's and false otherwise. For the purpose of this method, objects of a subclass of Ride should not be considered equal.
- iii.Add a hashCode method to Ride that returns the sum of the ride name's length and car capacity.
- b.In this part you will add a new class RollerCoaster which should extend Ride.
- i.Begin by adding the new class called RollerCoaster as a subclass of Ride. Retain but edit the class comment to include your own name and the date and a description of the class RollerCoaster. Remove the sample field, constructor and method and add a private int field numberOfCars representing the number of cars the roller coaster ride has.
- ii. Write a public constructor for RollerCoaster with the signature RollerCoaster(String, int, int) to initialise the instance variables using its parameters. The third parameter will be used to set the number of cars the roller coaster has.
- iii. Write a public method getCapacity in RollerCoaster that returns the total capacity for the roller coaster, which is the ride carCapacity multiplied by the numberOfCars.
- iv. Write a toString method in RollerCoaster that overrides that in Ride and returns in addition to the superclass description of the ride the total capacity of the ride, in the following format:
has car capacity and is . The total capacity of the ride is .
The total capacity needs to be replaced by the appropriate value for the current object.
Note that there is a newline character between the two sentences.
The first sentence should be provided by the Ride class toString method.
- v.Add an equals method to RollerCoaster which should override equals in Ride and return true if the ride name and capacity are the same. It should allow instances of subclasses to be considered equal.
- vi.Override the inherited hashCode method so that it returns a hash code using the Objects.hash method, to which you should pass the ride name and total capacity in that order.
- c.You have been provided with the class ThemePark, which you should not have made any changes to.
ThemePark maintains a list of rides in an ArrayList called rides.
Rides are added to rides using a method addRide.
Are there any examples of overloading, overriding or polymorphism in class ThemePark? Explain your answer.
- d.Go back and check that you have provided multiline comments for your classes, constructors, and methods. Use all of the comment annotations illustrated in the book at this stage of your study and be sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.
Step by Step Solution
3.41 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
Here is the code implementation for the Ride and RollerCoaster classes as per the requirements provided I will also provide explanations for each part ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started