Giter VIP home page Giter VIP logo

ios-enumerations-lab's Introduction

Enumerations lab

Fork and clone this repo. On your fork, answer and commit the follow questions. When you are finished, submit the link to your repo on Canvas.

Question 1 √

a) Define an enumeration called iOSDeviceType with member values iPhone, iPad, iWatch. Create a variable called myDevice and assign it one member value.

b) Adjust your code above so that iPhone and iPad have associated values of type String which represents the model number, eg: iPhone("6 Plus"). Use a switch case and let syntax to print out the model number of each device.

enum iOSDeviceType: String {
    case iPhone = "6 Plus"
    case iPad = "11in Pro"
    case iWatch = "Series 4"
}

let devices = iOSDeviceType.init(rawValue: "Series 4")
var myDevice = String()

switch devices! {
case .iPhone:
    myDevice = devices!.rawValue
case .iPad:
    myDevice = devices!.rawValue
case .iWatch:
    myDevice = devices!.rawValue
}

print("\(myDevice)")

Question 2

a) Write an enum called Shape and give it cases for triangle, rectangle, square, pentagon, and hexagon.

b) Write a method inside Shape that returns how many sides the shape has. Create a variable called myFavoritePolygon and assign it to one of the shapes above, then print out how many sides it has.

c) Re-write Shape so that each case has an associated value of type Int that will represent the length of the sides (assume the shapes are regular polygons so all the sides are the same length) and write a method inside that returns the perimeter of the shape.

enum Shape {
        
        case triangle
        case rectangle
        case square(side: Double)
        case pentagon
        case hexagon
        
        func lengthOf() -> Double {
            
            switch self {
            //case .triangle:
                    //(height * width) / 2
            //case .rectangle:
                    //(height * width) * 2
            case .square(side: let side):
                    return side * 3
            //case .pentagon:
            default:
                return 0.00
            }
            
        }
        
    }

let myFavoritePolygon = Shape.square(side: 4)
print(myFavoritePolygon.lengthOf())

Question 3 √

Write an enum called OperatingSystem and give it cases for windows, mac, and linux. Create an array of 10 OperatingSystem objects where each one is set to a random operating system. Then, iterate through the array and print out a message depending on the operating system.

enum OperatingSystem {
    case windows
    case mac
    case linux
}

let randomOS = [OperatingSystem.mac, OperatingSystem.windows, OperatingSystem.linux, OperatingSystem.mac, OperatingSystem.windows, OperatingSystem.linux, OperatingSystem.mac, OperatingSystem.windows, OperatingSystem.linux, OperatingSystem.mac]

for OS in randomOS {
    switch OS {
    case .mac:
        print("this is a mac")
    case .windows:
        print("this is a pc")
    case .linux:
        print("this is a linux")
    }
}

Question 4 √

You are working on a game in which your character is exploring a grid-like map. You get the original location of the character and the steps he will take.

  • A step .up will increase the y coordinate by 1.
  • A step .down will decrease the y coordinate by 1.
  • A step .right will increase the x coordinate by 1.
  • A step .left will decrease the x coordinate by 1.
  • Print the final location of the character after all the steps have been taken.
enum Direction {
    case up
    case down
    case left
    case right
}

var location = (x: 0, y: 0)
var steps: [Direction] = [.up, .up, .left, .down, .left]

for direction in steps {
    print("current location x: \(location.x) y: \(location.y)")
    print("i'm about to go \(direction)")
    
    switch direction {
        
        case .up:
            location.y += 1
        case .down:
            location.y -= 1
        case .right:
            location.x += 1
        case .left:
            location.x -= 1
    }
}

print("final location x: \(location.x) y: \(location.y)")

Question 5

a) Define an enumeration named HandShape with three members: .rock, .paper, .scissors.

b) Define an enumeration named MatchResult with three members: .win, .draw, .lose.

c) Write a function called match that takes two HandShapes and returns a MatchResult. It should return the outcome for the first player (the one with the first hand shape).

Hint: Rock beats scissors, paper beats rock, scissor beats paper

Question 6

a) You are given a CoinType enumeration which describes different coin values. Print the total value of the coins in the array moneyArray which contains tuples of type (quantity, CoinType).

enum CoinType: Int {
    case penny = 1
    case nickle = 5
    case dime = 10
    case quarter = 25
}

var moneyArray:[(Int,CoinType)] = [(10,.penny),
                                   (15,.nickle),
                                   (3,.quarter),
                                   (20,.penny),
                                   (3,.dime),
                                   (7,.quarter)]

// your code here

b) Write a method in the CoinType enum that returns an Int representing how many coins of that type you need to have a dollar. Then, create an instance of CoinType set to .nickle and use your method to print out how many nickels you need to have to make a dollar.

Question 7

a) Write an enum called DayOfWeek to represent the days of the week with a raw value of type String.

b) Given the array poorlyFormattedDays, write code that will produce an array of enums that match the days.

let poorlyFormattedDays = ["MONDAY", "wednesday", "Sunday", "monday", "Tuesday", "WEDNESDAY", "thursday", "SATURDAY", "tuesday", "FRIDAy", "Wednesday", "Monday", "Friday", "sunday"]

c) Write a method in DayOfWeek called isWeekend that determines whether a day is part of the weekend or not and write code to calculate how many week days appear in poorlyFormattedDays.

Question 8

a) Create an enum called MetroLine with cases for the colors of the metro train lines. Create an instance of MetroLine.

b) Modify your enum so that each case has an associated value of either Character or Int that will represent the train on that line. Create a new instance of MetroLine and give it an appropriate train letter or number.

c) Write code that prints the train letter or number of your instance of MetroLine.

Question 9

a) Think of your own example of something that can be modeled as an enum and write it. Remember that enums allow you to create instances of a defined list of cases.

b) Add a method to your enum.... try to have the method make sense.

ios-enumerations-lab's People

Contributors

aglegaspi avatar davidlawrencer avatar benstone1 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.