- Create custom classes in an inheritance chain.
- Add properties to a subclass.
- Write initializers that call the superclass's initializer.
- Write new methods available to a subclass.
- Override methods inherited from a superclass.
Open the swift-vehicle.xcworkspace
file.
Create a new file to hold a custom class named Vehicle
.
It should have five properties:
- an immutable
String
namedname
, - an immutable
Double
namedweight
, - an immutable
Double
namedmaxSpeed
, - a mutable
Double
namedspeed
, and - a mutable
Double
namedheading
.
Top-tip: Theheading
property is measured in angular degrees,0
to360
. Changes to this property should always keep it within this range.
Set the mutable properties to zero as a default.
Write a designated initializer that covers the three immutable properties.
Write the following six methods, none of which take any arguments nor provide a return:
-
goFast()
which sets the vehicle'sspeed
equal to itsmaxSpeed
. -
halt()
which sets the vehicle'sspeed
to zero. -
accelerate()
which increases thespeed
by one tenth (1/10th) of the vehicle'smaxSpeed
. This should not increase thespeed
beyond themaxSpeed
. -
decelerate()
which decreases thespeed
by one tenth (1/10th) of the vehicle'smaxSpeed
. This should not decrease thespeed
below zero. -
turnRight()
which increases theheading
by 90 angular degrees and cuts thespeed
in half (the physics of turning has a momentum cost). This method should have no effect on a stationary vehicle (a vehicle can only turn if it is moving).
Top-tip: Remember to keep theheading
's value between0
and360
. -
turnLeft()
which has the effect of decreasing theheading
by 90 angular degrees. Similarly, this should cut thespeed
in half and should have no effect on a stationary vehicle.
Top-tip: The modulus operator won't perform the math that you want if you simply feed it-90
. You're going to have to help it figure out how to simulate a left turn.
Create a new file for a custom class named Car
that inherits from the Vehicle
class.
The Car
class should add three new properties:
- an immutable
String
namedtransmission
, - an immutable
Int
namedcylinders
, and - an immutable
Double
namedmilesPerGallon
.
Write a designated initializer that takes six arguments total, the three arguments for the superclass's initializer (name
, weight
, and maxSpeed
), and three arguments for these new properties (transmission
, cylinders
, and milesPerGallon
). The initializer should set all three new properties to their associated arguments, and call the superclass's initializer, passing it the correct arguments that it requires.
-
Write a new method named
drive()
which takes no arguments and provides no return. This method should simply call theaccelerate()
method inherited from the superclass. -
Write a new method named
brake()
which take no arguments and provides no return. This method should simply call thedecelerate()
method inherited from the superclass.
Create a new file for a custom class named RaceCar
that inherits from the Car
class.
The RaceCar
class should add two new properties:
- an immutable
String
nameddriver
, and - a mutable array of type
String
namedsponsors
.
The designated initializer should accept eight (8) arguments total, six for the superclass's initializer, and two for the new properties. It should set these two new properties to the arguments, and pass the other six along to the superclass's initializer.
Race cars have powerful engines and high performance brakes.
-
Override the
accelerate()
method that is inherited from theVehicle
class. It should increase thespeed
by one fifth (1/5th) of themaxSpeed
. However, it should not increase thespeed
beyond themaxSpeed
. -
Override the
decelerate()
method that is also inherited from theVehicle
class. It should decrease thespeed
by one fifth (1/5th) of themaxSpeed
. However, it should not decrease thespeed
below zero.
Race cars also have professional drivers who can navigate sharp turns without losing as much speed. We'll refer to this ability as "drifting".
-
Write a new method named
driftRight()
which takes no arguments and provides no return. If the race car is in motion, it should increase the heading by 90 angular degrees, but only decrease thespeed
by one quarter (1/4) of its current value (instead of one half). -
Write a new method named
driftLeft()
which takes no arguments and provides no return. If the race car is in motion, it should have the effect of decreasing the heading by 90 angular degrees (handle this the same way you handled the heading inturnLeft()
), but only decrease thespeed
by one quarter (1/4) of its current value (instead of one half).
These new methods should not affect turnRight()
or turnLeft()
; they are separate abilities.
Create a new file for a custom class named Plane
that also inherits from Vehicle
.
The Plane
class should add three new properties:
- an immutable
Double
namedmaxAltitude
, - a mutable
Double
namedaltitude
, and - a calculated
Bool
namedinFlight
.
Set altitude
's initial value to zero.
Program inFlight
to return True
if both the speed
and the altitude
are greater than zero.
Write a designated initializer that takes four arguments, three for the superclass's properties and one for maxAltitude
.
Write new aviation methods which take no arguments nor provide any returns:
-
takeoff()
which only affects a plane that is not in flight. This method should set thespeed
andaltitude
properties to one tenth (1/10th) of their maximums. -
land()
which sets thespeed
andaltitude
properties to zero. -
climb()
which increases thealtitude
by one tenth (1/10th) of the plane'smaxAltitude
and causes the plane to decelerate. This method should not increase thealtitude
beyond themaxAltitude
and can only be performed when the plane is in flight. -
dive()
which decreases thealtitude
by one tenth (1/10th) of the plane'smaxAltitude
and causes the plane to accelerate. This method should not decrease thealtitude
below zero and can only be performed if the plane is not already on the ground. However, if the plane's speed is zero, it is still in the air but has "stalled" and is still able to perform a dive in order to convert altitude into speed. -
bankRight()
which increases theheading
by 45 angular degrees but only decreases thespeed
by one tenth (1/10th) of its current value. This method can only be performed by a plane that is in flight. -
bankLeft()
which has the effect of decreasing theheading
by 45 angular degrees, but only decreases thespeed
by one tenth (1/10th) of its current value. This method can only be performed by a plane this is in flight.
Create a new file for a custom class named Jet
that inherits from Plane
.
The Jet
class does not add any new properties.
Jets have stronger engines than regular planes and are more aerodynamic. This allows them to climb and dive at better rates.
-
Override the
climb()
method to increase the jet'saltitude
by one fifth (1/5) of themaxAltitude
(instead of one tenth). Similar to the superclass's implementation, this method should decelerate the plane, should not increase thealtitude
beyond themaxAltitude
, and can only be performed by a jet that is in flight. -
Override the
dive()
method to decrease the jet'saltitude
by one fifth (1/5) of themaxAltitude
(instead of one tenth). Similar to the superclass's implementation, this method should accelerate the plane, should not decrease thealtitude
below zero, and can only be performed by a jet that is in flight.
Jets (i.e. military jets) have a special ability called "using the afterburner" that allows them to reach incredible speeds.
- Write a new method named
afterburner()
which takes no arguments and provides no return. This method should set thespeed
to twice the value of the jet'smaxSpeed
but only if the jet is in flight and already traveling at its maximum speed.
//Flat-fact: The SR-71 Blackbird is the fastest non-experimental aircraft in the world and can reach a speed of Mach 3.3 which is in excess of 2200 miles per hour.
Create a few instances of your new vehicular classes in the AppDelegate
's application:didfinishLaunchingWithOptions:
method. See what methods and properties are available to your instances at different places in the inheritance chain.
View Vehicle on Learn.co and start learning to code for free.