Question: Using Swift command line for macOS (open xcode, create a new xcode project, macOS, command line tool) or playground, practice the following exercises: Exercise: Swift

Using Swift command line for macOS (open xcode, create a new xcode project, macOS, command line tool) or playground, practice the following exercises:

Exercise: Swift Classes

  1. Create a class named Flight with 3 properties: airlineName, number of seats, number of customers with confirmed seats
  2. Create a custom initializer in the class to set its properties
  3. Create a method that returns the number of seats available after booking
  4. Create another class named Boeing as subclass of Flight and initialize it using the custom initializer in the flight class
  5. Calculate number of seats available in the Boeing flight and print its details
  6. Now, create one more class called Airbus as subclass of Flight with custom initializer. Override the calculate number of seats method to ignore 2 seats that are only available for cabin crew and rewrite it.
  7. Also, create a subclass of Airbus that again calculates number of seats using the overridden method above

Exercise: Enum

  1. Define an enum Month which consists of all the months in a year.
  2. Next, define a function NumberOfDays() which accepts the month and prints out Number of Days for that month. {Use switch case statement}

Exercise: Structures

  1. Define a structure, Cube, having 3 parameters length, breadth and height to encapsulate the data
  2. Create an area function inside struct that calculates the cubes area
  3. Instantiate cube1 with values of length, breadth and height.
  4. Assign cube1 to cube2
  5. Set length of cube2 to some other value
  6. Calculate and print area of both cubes

Exercise: Protocols

  1. Define a protocol MastersDegree, define following functions inside this protocol:

  • printGPA() - This should print the GPA

  • isSmartphonesCourseCompleted() - This should print if the student has completed smartphones course or not

  1. Now define a class Student which adopt MastersDegree protocol and provide the actual implementation

Exercise: Extensions

  1. Extending the datatype String add methods: stringLength() and concatenateString (Use another string as Smartphones)

  1. Next, create a constant named stringExtension of String initializing it to a string of your choice. Use the extended functionality in point 1 and print out the values in the console.

  1. Complete the code below:

class division {

var number: Int = 0

}

extension Division {

//Define a method to divide 2 numbers and print the result

}

let div = division()

div.dividedBy(number1: 1800, number2: 3)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!