Success is not final, failure is not fatal: it is the courage to continue that counts. -Winston Churchill
This quiz will give you practice working with both classes and structs.
In the last few lessons, you've learned a lot about classes and structs in Swift. Now, challenge yourself with a brief quiz to test everything you've learned.
Given this class that represents a giant:
class Giant {
var name: String
var weight: Double
let homePlanet: String
init(name: String, weight: Double, homePlanet: String) {
self.name = name
self.weight = weight
self.homePlanet = homePlanet
}
}
And this code that instantiates an instance of Giant
:
let fred = Giant(name: "Fred", weight: 340.0, homePlanet: "Earth")
Will these three lines of code run? If not, why not?
fred.name = "Brick"
fred.weight = 999.2
fred.homePlanet = "Mars"
---> NO, BECAUSE HOME PLANET IS A LET
Can you fix the class definition above so that it does work?
---> var homePlanet: String
Take a look at this struct that represents an alien:
struct Alien {
var name: String
var height: Double
var homePlanet: String
}
And this line of code that instantiates an Alien
:
let bilbo = Alien(name: "Bilbo", height: 1.67, homePlanet: "Venus")
Will these three lines of code run? If so, why not?
bilbo.name = "Jake"
bilbo.height = 1.42
bilbo.homePlanet = "Saturn"
---> NO, BECAUSE let bilbo
Can you change the declaration of bilbo
so that the above three lines of code do work?
---> var bilbo...
Consider this bit of code that uses the Giant
class:
let edgar = Giant(name: "Edgar", weight: 520.0, homePlanet: "Earth")
let jason = edgar
jason.name = "Jason"
What will the value of edgar.name
be after those three lines of code are run? What will the value of jason.name
be? Why?
---> Jason, Jason
Given this bit of code that uses the Alien
struct:
var charles = Alien(name: "Charles", height: 2.25, homePlanet: "Pluto")
var charlesFromJupiter = charles
charlesFromJupiter.homePlanet = "Jupiter"
What will the value of charles.homePlanet
be after the above code run? What about the value of charlesFromJupiter.homePlanet
? Why?
---> Pluto, Jupiter
Here's a struct that represents a bank account:
struct BankAccount {
var owner: String
var balance: Double
func deposit(_ amount: Double) {
balance += amount
}
func withdraw(_ amount: Double) {
balance -= amount
}
}
Does this code work? Why or why not?
---> NOPE, IMMUTABLE
Can you fix the BankAccount
struct so it does work?
---> mutating func
Given this bit of code (which incorporates any fixes you made in Question 8):
var joeAccount = BankAccount(owner: "Joe", balance: 100.0)
var joeOtherAccount = joeAccount
joeAccount.withdraw(50.0)
What will the value of joeAccount.balance
be after the above code runs? What about the value of joeOtherAccount.balance
? Why?
---> joeAccount= 50, joeOtherAccount = 100. BECAUSE A COPY IS CREATED
Here's a class that can track songs in a music library:
class MusicLibrary {
var tracks: [String]
init() {
tracks = []
}
func add(track: String) {
tracks.append(track)
}
}
Given this bit of code that uses MusicLibrary
:
let library1 = MusicLibrary()
library1.add(track: "Michelle")
library1.add(track: "Voodoo Child")
let library2 = library1
library2.add(track: "Come As You Are")
After this code runs, what are the contents of library1.tracks
? What about the contents of library2.tracks
? Why?
---> SAME, ("MICHELLE", "VOODOO CHILD", "COME AS YOU ARE") -> COS REFERENCE TYPE