Assignment No 7
Assignment No 7
struct Book {
var title: String
var author: String
var pages: Int
var price: Double
}
A Post struct has been created for you below, representing a generic social media post. Add a
mutating method on Post called like that will increment likes by one. Then create an instance
of Post and call like () on it. Print out the likes property before and after calling the method to
see whether or not the value was incremented.
struct Post {
var message: String
var likes: Int
var numberOfComments: Int
}
Workout Functions
20. A RunningWorkout struct has been created for you below. Add a method on
RunningWorkout called postWorkoutStats that prints out the details of the run. Then create an
instance of RunningWorkout and call postWorkoutStats ().
struct RunningWorkout {
var distance: Double
var time: Double
var elevation: Double
}
A Steps struct has been created for you below, representing the day's step-tracking data. It has
the goal number of steps for the day and the number of steps taken so far. Create a method on
Steps called takeStep that increments the value of steps by one. Then create an instance of
Steps and call takeStep (). Print the value of the instance's steps property before and after the
method call.
struct Steps {
var steps: Int
var goal: Int
}
Computed Properties and Property Observers
21. 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
var height: Int
}
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 = heightInInches
self. heightInCentimeters = heightInInches*2.54
}
struct RunningWorkout {
var distance: Double
var time: Double
var elevation: Double
}
In other app exercises, you've provided encouraging messages to the user based on how many
steps they've completed. A great place to check whether or not you should display something
to the user is in a property observer. In the Steps struct below, add a willSet to the steps property
that will check if the new value is equal to goal, and if it is, prints a congratulatory message.
Create an instance of Steps where steps are 9999 and goal is 10000, then call takeStep () and
see if your message is printed to the console.
struct Steps {
var steps: Int
var goal: Int
mutating func takeStep() {
steps += 1
}
}
struct User {
var userName: String
var email: String
var age: Int
}
There are other properties and actions associated with a User struct that might be good
candidates for a type property or method. One might be a method for logging in. Go back and
create a type method called logIn (user:) where user is of type User. In the body of the method,
assign the passed in user to the currentUser property, and print out a statement using the user's
userName saying that the user has logged in. Below, call the logIn (user:) method and pass in
a different User instance than what you assigned to currentUser above. Observe the printout in
the console.
Type Properties and Methods
24. In another exercise, you added a computed property representing the average mile time
from a run. However, you may want to have a calculator of sorts that users can use before their
run to find out what mile time they need to average in order to run a given distance in a given
time. In this case it might be helpful to have a type method on RunningWorkout that can be
accessed without having an instance of RunningWorkout. Add to RunningWorkout a type
method mileTimeFor (distance: time:) where distance and time are both of type Double. This
method should have a return value of type Double. The body of the method should calculate
the average mile time needed to cover the passed in distance in the passed in time. Assume that
distance is in meters and that one mile is 1600 meters. Call the method from outside of the
struct and print the result to ensure that it works properly.
struct RunningWorkout {
var distance: Double
var time: Double
var elevation: Double
}
It may be helpful to have a few type properties on RunningWorkout representing unit
conversions (i.e. meters to mile, feet to meters, etc.). Go back and add a type property for
meterInFeet and assign it 3.28084. Then add a type property for mileInMeters and assign it
1600.0. Print both of these values.
********************