{"id":105926,"date":"2022-11-09T00:52:08","date_gmt":"2022-11-09T00:52:08","guid":{"rendered":"https:\/\/papersspot.com\/blog\/2022\/11\/09\/objective-to-practice-thinking-working-with-swifts-version-of-enumerated-data-types\/"},"modified":"2022-11-09T00:52:08","modified_gmt":"2022-11-09T00:52:08","slug":"objective-to-practice-thinking-working-with-swifts-version-of-enumerated-data-types","status":"publish","type":"post","link":"https:\/\/papersspot.com\/blog\/2022\/11\/09\/objective-to-practice-thinking-working-with-swifts-version-of-enumerated-data-types\/","title":{"rendered":"Objective: To practice thinking working with Swift\u2019s version of enumerated Data Types,"},"content":{"rendered":"<p>Objective: To practice thinking working with Swift\u2019s version of enumerated Data Types, practice creating and working with them, as well as writing regular and mutating functions for enums.<\/p>\n<p> 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.\u00a0\u00a0<\/p>\n<p> We can create Enums that run calculations that are dependent on their current state. Different calculations can be performed for different states.\u00a0 We can access the current state inside an Enum by using the self reference (similar to the \u201cthis\u201d reference in Java).<\/p>\n<p> In this activity, you will be creating a Temperature Converter Enum with two operating modes: Fahrenheit and Celsius.\u00a0 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.\u00a0 In Celsius mode, you will supply temperatures in Fahrenheit and the converter will print out what the temperature is in Celsius.\u00a0\u00a0<\/p>\n<p> Directions:<\/p>\n<p> Create and open a new playground file named TempConverter (or MacOS Commandline project)<\/p>\n<p> Make sure you place a title block with requisite info at the top of the file!<\/p>\n<p> Define an Enum named TempConverter with two possible cases (operating modes):<\/p>\n<p> Fahrenheit<\/p>\n<p> Celsius<\/p>\n<p> Define an initializer method for the Enum: init(in scale: String)<\/p>\n<p> Initializers are like constructors from C++\/Java.\u00a0 They create the instance. They have no return values.<\/p>\n<p> The scale String will determine what the initial Operating mode is.<\/p>\n<p> Use the first letter of the String to determine which mode to assign to the self reference <\/p>\n<p> Define a convert(for temp: Double) method.<\/p>\n<p> This method only needs to print out what the conversion result is (no return)<\/p>\n<p> The method should run a switch on the self reference to determine which conversion to perform<\/p>\n<p> If we are already in Celsius, then convert the temp reference from Fahrenheit to Celsius and print the result<\/p>\n<p> If we are already in Fahrenheit, then convert the temp reference from Celsius to Fahrenheit and print the result<\/p>\n<p> Define a mutating function switchScale():<\/p>\n<p> Remember: Because Enums are value types, any function that changes internal state must \u201copt-in\u201d into mutating behavior by using mutating \u00a0keyword<\/p>\n<p> This method should switch on self:<\/p>\n<p> If we\u2019re already in Fahrenheit, switch to Celsius<\/p>\n<p> If we\u2019re already in Celsius, switch to Fahrenheit<\/p>\n<p> Finally, demonstrate that your temperature converter works.\u00a0<\/p>\n<p> Create an instance of the tempConverter using the defined initializer, something like this:\u00a0<\/p>\n<p> var myConverter\u00a0 = TempConverter(in: \u201cCelsius\u201d)<\/p>\n<p> Call the convert method and convert a few well-known temperature values from one scale to the other (Boiling point, freezing point, etc)<\/p>\n<p> Call the switchScale() method and then run the same conversions in the reverse direction<\/p>\n<p> Make sure the numbers come out correctly!<\/p>\n<p> 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<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objective: To practice thinking working with Swift\u2019s 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.\u00a0\u00a0 We [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10],"class_list":["post-105926","post","type-post","status-publish","format-standard","hentry","category-research-paper-writing","tag-writing"],"_links":{"self":[{"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/posts\/105926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/comments?post=105926"}],"version-history":[{"count":0,"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/posts\/105926\/revisions"}],"wp:attachment":[{"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/media?parent=105926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/categories?post=105926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/papersspot.com\/blog\/wp-json\/wp\/v2\/tags?post=105926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}