Let your heart guide you. It whispers so listen closely. -Littlefoot's Mother
In this lab, you'll implement a few simple classes using enums.
This lab is all about classes and enums! In this lab, you'll implement a few simple classes using enums. As usual, a set of tests have been written for you. If you follow the directions in this lab correctly, the tests will pass.
Get started by opening up ClassesNenums.xcworkspace
in Xcode. Go ahead and run the tests by Test from the Product menu, or pressing Command+U. Right now, you'll see that the tests fail, but if you finish this lab, the tests should pass.
Let's go!
- First, open up
Bird.swift
in Xcode's code editor. You'll be making a lot of changes in this file. - To complete this lab, you'll need to implement three enums. The first one should be called
Speed
. This enum is used for comparison and has raw values, so it should inherit fromInt
. It should have three cases:Slow
,Medium
, andFast
. Create this enum now, inBird.swift
. - Implement a method for your
Speed
enum calledisFaster(thanSpeed:)
. This enum should take aSpeed
paramater and return true if the enum value thatisFaster(thanSpeed:)
is called on is faster than the parameter. For example:Speed.Slow.isFaster(thanSpeed: Speed.Medium)
should returnfalse
.Speed.Faster.isFaster(thanSpeed: Speed.Medium
should returntrue
.- In the case of a tie, the value that
isFather(thanSpeed:)
is called on should be considered "faster" (i.e., the method should returntrue
). SoSpeed.Faster.isFaster(thanSpeed: Speed.Faster)
should returntrue
.
- Next, create an enum called
Diet
. (This enum does not need to do any comparisons, so it does not need to have any raw values associated with it.) The cases forDiet
should beMeatEater
andVegetarian
. - Then create an enum called
Sex
. The cases of this enum should beMale
andFemale
. - Now it's time to create a new class called
Trex
. Declare that class inBird.swift
. - Add a property called
speed
to yourTrex
class. This property should be of typeSpeed
and should initially be set toSpeed.Fast
. - Add a property called
diet
to yourTrex
class. This property should be of typeDiet
and should be set toDiet.MeatEater
. - Add a property called
name
of typeString
toTrex
. - Add a property called
sex
of typeSex
toTrex
. - Add a property called
isAlive
of typeBool
toTrex
. This should initially be set totrue
. - Create an initializer for
Trex
. This initializer should take two parameters,name
andsex
, and assign those parameters to the corresponding properties. - Create a method called
speak()
. This method should return the string ""ROAAAWWWWRRRRR!!!!!!". - Create a method called
isFaster(thanTrex:)
. This method should take one parameter of typeTrex
. It should returntrue
if the object the method is called on is faster than the parameter. - Create a method called
eat(otherTrex:)
. This method should take one parameter of typeTrex
. If the object the method is called on is faster than the parameter, it should "eat" the otherTrex
(by setting the otherTrex
'sisAlive
property tofalse
).
That's it! Try running the tests again. This time, they should pass. If they don't, take a look at these directions again and make sure you implemented everything properly, and keeping hammering away until the tests do pass!