In this part you will add a new class RollerCoaster which should extend Ride. i.Begin by adding the new class called Rol
Posted: Mon May 02, 2022 12:04 pm
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.
(5 marks)
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.
(5 marks)
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.
(5 marks)
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:
<class name> <ride name> has car
capacity <car capacity> and is <running/not
running>.
The total capacity of the ride is
<capacity>.
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.
(5 marks)
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.
(5 marks)
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.
(5 marks)
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.
Really stuck with this one
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.
(5 marks)
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.
(5 marks)
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.
(5 marks)
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:
<class name> <ride name> has car
capacity <car capacity> and is <running/not
running>.
The total capacity of the ride is
<capacity>.
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.
(5 marks)
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.
(5 marks)
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.
(5 marks)
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.
Really stuck with this one