Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

Class Example | Swift & IOS Tutorials

Posted on December 12, 2018 by Yılmaz Cemalettin

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:

Swift
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()

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme