Objective: To practice thinking working with Swift’s version of enumerated Data Types, practice creating and working with them, as well as writing regular and mutating functions for enums.
Summary: In Swift, Enums are powerful first class data types that can perform many operations typically reserved for classes and objects in other more traditional languages.
We can create Enums that run calculations that are dependent on their current state. Different calculations can be performed for different states. We can access the current state inside an Enum by using the self reference (similar to the “this” reference in Java).
In this activity, you will be creating a Temperature Converter Enum with two operating modes: Fahrenheit and Celsius. You will be defining the Enumerator as well as the methods that will operate on it. In Fahrenheit mode, you will supply temperatures in Celsius and the converter will print out what the temperature is in Fahrenheit. In Celsius mode, you will supply temperatures in Fahrenheit and the converter will print out what the temperature is in Celsius.
Directions:
Create and open a new playground file named TempConverter (or MacOS Commandline project)
Make sure you place a title block with requisite info at the top of the file!
Define an Enum named TempConverter with two possible cases (operating modes):
Fahrenheit
Celsius
Define an initializer method for the Enum: init(in scale: String)
Initializers are like constructors from C++/Java. They create the instance. They have no return values.
The scale String will determine what the initial Operating mode is.
Use the first letter of the String to determine which mode to assign to the self reference
Define a convert(for temp: Double) method.
This method only needs to print out what the conversion result is (no return)
The method should run a switch on the self reference to determine which conversion to perform
If we are already in Celsius, then convert the temp reference from Fahrenheit to Celsius and print the result
If we are already in Fahrenheit, then convert the temp reference from Celsius to Fahrenheit and print the result
Define a mutating function switchScale():
Remember: Because Enums are value types, any function that changes internal state must “opt-in” into mutating behavior by using mutating keyword
This method should switch on self:
If we’re already in Fahrenheit, switch to Celsius
If we’re already in Celsius, switch to Fahrenheit
Finally, demonstrate that your temperature converter works.
Create an instance of the tempConverter using the defined initializer, something like this:
var myConverter = TempConverter(in: “Celsius”)
Call the convert method and convert a few well-known temperature values from one scale to the other (Boiling point, freezing point, etc)
Call the switchScale() method and then run the same conversions in the reverse direction
Make sure the numbers come out correctly!
Submittals: a pdf, jpg, jpeg, or png file that demonstrates your challenge code running AND project source code (compressed to .zip file ) and uploaded to the appropriate canvas link