Swift & IOS Application Development Tutorials
Example 1:
Introduction the Swift and Xcode Playground
Using playgrounds, implement the classes shown in the diagram below.
Width and height will be in meters.
For this example;
* Create StudyRoom and ClassRoom instances.
* Classroom instance will also initialize an equipment dictionary. This [String: Double] dictionary will hold the name and the price for each equipment ( as an example; computer: 3500, projector: 2000 etc.) There should be at least 5 equipments in a classroom.
* Print meaningful descriptions for each instance. All properties should be visible in the description. Area information should be included in the description.
* Classroom instances should also calculate and display the total cost of the equipments present in the class.
* calculateArea() functions should calculate the area of the room in square meters.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
//Cemalettin Yılmaz import UIKit //Defining the main class which is Room class Room { //Defining variables let Number: Int let Width : Double let Height : Double // Initiliza variables init (Number : Int, Width: Double, Height: Double){ self.Number = Number self.Width = Width self.Height = Height } //Defining print description function func printDescription(){ print("This is a room with NO = \(Number) and Width is \(Width) meter and Heigth is \(Height) meter") } } //Defining sub class (ClassRoom) of room class class ClassRoom : Room{ // Defining Equipments array let Equipment : [String: Double] = ["Computer": 3499.99, "Projector": 1995.95, "Whiteboard": 259.90, "Board Pen": 10.00, "Speakers": 190.50] override init(Number: Int, Width: Double, Height: Double) { super.init(Number: Number, Width: Width, Height: Height) } //Override function of printDescription We use the override because printDescription is also in main class override func printDescription(){ print("This is a classroom with NO = \(Number) and Width is \(Width) meter and Heigth is \(Height) meter. Area of classroom is \(self.calculateArea()) square meters.Equipments in classroom with individiual prices are \(Equipment) and total price is \(self.calculateCost())") } func calculateArea() -> Double{ //Returning area of the Class return Height*Width } func calculateCost() -> Double{ var sum :Double = 0 //For each equipments I find them cost and sum of them for element in Equipment { sum += element.value } //Returning cost of equipments return sum } } //Defining sub class (StudyRoom) of room class class StudyRoom : Room{ let maxOccupancy : Int init(maxOccupancy: Int, Number: Int, Width: Double, Height: Double) { self.maxOccupancy = maxOccupancy //I use the Super imit so that I don't need re-initlaze main class variables super.init(Number: Number, Width: Width, Height: Height) } //Override function of printDescription We use the override because printDescription is also in main class override func printDescription(){ print("This is a classroom with NO = \(Number) and Width is \(Width) meter and Heigth is \(Height) meter. Area of classroom is \(self.calculateArea()) square meters. Maximum occupancy is \(maxOccupancy)") } func calculateArea() -> Double{ return Height*Width } } //I created new instances of the room, Classroom and StudyRoom classes let room1 = Room(Number: 1, Width: 7.5, Height: 8) let classRoom1 = ClassRoom(Number: 2, Width: 10, Height: 15) let studyRoom1 = StudyRoom(maxOccupancy: 24, Number: 3, Width: 10.5, Height: 20) //Call class function which is print description of class proporties room1.printDescription() classRoom1.printDescription() studyRoom1.printDescription() |