Exercise - Computed Properties and Property Observers The Rectangle struct below has two properties, one for width and one for height. Add a computed property that computes the area of the rectangle (i.e. width * height). Create an instance of Rectangle and print the area property. struct Rectangle { 7 var width: Int 8 var height: Int 9 10 } 11 12 In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust to match it. Add a didSet to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property even though it already has the right value, you will end up with an infinite loop of each property setting the other. Create an instance of Height and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly. 18 struct Height { 19 var heightInInches: Double 20 21 var heightInCentimeters: Double 22 init(heightInInches: Double) { self.heightInInches = heightInInches self.heightInCentimeters = heightInInches*2.54 23 24 25 26 } 27 init(heightInCentimeters: Double) { self.heightInCentimeters = heightInCentimeters self.heightInInches = heightInCentimeters/2.54 28 29 30

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

How do I solve this using Swift code? 7 of 10

Exercise - Computed Properties and Property Observers
The Rectangle struct below has two properties, one for width and one for height. Add a computed property that computes the area of the rectangle (i.e. width * height).
Create an instance of Rectangle and print the area property.
struct Rectangle {
var width: Int
6
7
8.
var height: Int
9
10
}
11
12
In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust to
match it. Add a didSet to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property
even though it already has the right value, you will end up with an infinite loop of each property setting the other.
Create an instance of Height and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly.
struct Height {
var heightInInches: Double
18
19
20
21
var heightInCentimeters: Double
22
init (heightInInches: Double) {
self.heightInInches = heightInInches
self.heightInCentimeters =
23
24
25
heightInInches*2.54
26
}
27
init(heightInCentimeters: Double) {
self.heightInCentimeters = heightInCentimeters
self.heightInInches = heightInCentimeters/2.54
28
29
30
Transcribed Image Text:Exercise - Computed Properties and Property Observers The Rectangle struct below has two properties, one for width and one for height. Add a computed property that computes the area of the rectangle (i.e. width * height). Create an instance of Rectangle and print the area property. struct Rectangle { var width: Int 6 7 8. var height: Int 9 10 } 11 12 In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust to match it. Add a didSet to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property even though it already has the right value, you will end up with an infinite loop of each property setting the other. Create an instance of Height and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly. struct Height { var heightInInches: Double 18 19 20 21 var heightInCentimeters: Double 22 init (heightInInches: Double) { self.heightInInches = heightInInches self.heightInCentimeters = 23 24 25 heightInInches*2.54 26 } 27 init(heightInCentimeters: Double) { self.heightInCentimeters = heightInCentimeters self.heightInInches = heightInCentimeters/2.54 28 29 30
In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust to
match it. Add a didSet to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property
even though it already has the right value, you will end up with an infinite loop of each property setting the other.
Create an instance of Height and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly.
struct Height {
var heightInInches: Double
var heightInCentimeters: Double
init(heightInInches: Double) {
self.heightInInches =
self.heightInCentimeters = heightInInches*2.54
heightInInches
}
init(heightInCentimeters: Double) {
self.heightInCentimeters
self.heightInInches = heightInCentimeters/2.54
heightInCentimeters
%D
}
}
Previous | page 7 of 10 | Next: App Exercise - Mile Times and Congratulations
Transcribed Image Text:In the Height struct below, height is represented in both inches and centimeters. However, if heightInInches is changed, heightInCentimeters should also adjust to match it. Add a didSet to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property even though it already has the right value, you will end up with an infinite loop of each property setting the other. Create an instance of Height and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly. struct Height { var heightInInches: Double var heightInCentimeters: Double init(heightInInches: Double) { self.heightInInches = self.heightInCentimeters = heightInInches*2.54 heightInInches } init(heightInCentimeters: Double) { self.heightInCentimeters self.heightInInches = heightInCentimeters/2.54 heightInCentimeters %D } } Previous | page 7 of 10 | Next: App Exercise - Mile Times and Congratulations
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY