My only advice is to stay aware, listen carefully, and yell for help if you need it. -Judy Blume
In this lab, you'll implement a variety of structs and create properties for them.
This lab will test what you learned about structs in the previous lesson. It will start off pretty easy, but ramp up in difficulty as it goes along.
Tests are included in this lab. When you first open StructLove.xcworkspace
, you can run the tests. They'll all fail. As you work through the lab, try running the tests each time you complete another section. By the end, all the tests will pass, and you'll have finished the lab.
Let's get started!
Make sure that you're opening the StructLove.xcworkspace file.
Let's start off easy: By implementing a simple struct called Tiger
. The Tiger
you implement in this lab is pretty basic: It has a name, and it is either hungry or not hungry, just like tigers in the real world.
- In the
Tiger.swift
file, define a struct calledTiger
. - Add two properties to your
Tiger
struct:- A
String
property calledname
- A
Bool
property calledisHungry
- A
- Create an initializer for this struct. This struct should take one parameter of type
String
calledname
, and assign that parameter to the appropriate property.isHungry
should betrue
by default. See the note below. - Implement a method called
eat()
. This method takes no parameters. It should changeisHungry
tofalse
. (Remember how to write a struct method that changes a property?)
NOTE: It's best when you're assigning a default value to a stored property, to do it at the declaration of the property, not within the initializer. It's clear to anyone reading your code (including yourself) that when you look through the various properties of a class or struct, which ones have default values as opposed to having to go to the initalizer to see if the init
function is providing a default value to certain stored properties.
With that simple task out of the way, let's switch gears a bit. For the rest of this lab, you're going to implement a few structs to represent a pizza delivery service. This isn't any ordinary pizza delivery service, though. This is an intercontinental pizza delivery service that can deliver pizzas up to 5000 kilometers away.
The first thing you have to do, though, is create a struct that can represent a coordinate somewhere on Earth. Coordinates are made up of latitude/longtitude points. These points are Double
s.
- In
Coordinate.swift
, declare a struct calledCoordinate
. - Add two stored properties:
- A
Double
calledlatitude
- Another
Double
calledlongitude
- A
- Create an initializer with two parameters. Both parameters should be of type
Double
labeled aslatitude
andlongitude
. - Add four computed properties:
- A
Bool
calledisInNorthernHemisphere
. This returnstrue
if the coordinate is in the Northern Hemisphere. (A coordinate is in the Northern Hemisphere if its latitude is positive.) - A
Bool
calledisInSouthernHemisphere
. This returnstrue
if the coordinate is in the Souther Hemisphere. (A coordinate is in the Souther Hemisphere if its latitude is negative.) - A
Bool
calledisInWesternHemisphere
. This returnstrue
if the coordinate is in the Western Hemisphere. (A coordinate is in the Western Hemisphere if its longitude is positive.) - A
Bool
calledisInEasternHemisphere
. This returnstrue
if the coordinate is in the Eastern Hemisphere. (A coordinate is in the Eastern Hemisphere if its longitude is negative.)
- A
- Create a method called
distance(to:)
. This method takes one parameter calledcoordinate
of typeCoordinate
.distance(to:)
should return the distance, in kilometers, between the two points. What are the two points? Well.. this function you're implementing is only available to instances of theCoordinate
struct. So if an instance of theCoordinate
struct calls on this particular method, we will be passing in anotherCoordinate
instance as an argument. So we're left with twoCoordinate
instances which are available to us within our implementation of this function. TheCoordinate
which called on the function--we can access thelatitude
andlongitude
stored properties by typinglatitude
orlongitude
. We can also access these stored properties by typingself.latitude
orself.longitude
. The parameter of this function is calledcoordinate
. We can access this coordinate's stored properties by typingcoordinate.latitude
orcoordinate.longitude
.- The distance between two coordinates requires some hairy math. The formula is acos(sin(latitude1) * sin(latitude2) + cos(latitude1) * cos(latitude2) * cos(longitude1-longitude2)) * 6371000 / 1000.
acos()
andsin()
are both functions you can call in Swift. latitude
andlongitude
need to be converted to radians. Aradians
property has been created for you to do this, so you can calllatitude.radians
to get the latitude in radians.
- The distance between two coordinates requires some hairy math. The formula is acos(sin(latitude1) * sin(latitude2) + cos(latitude1) * cos(latitude2) * cos(longitude1-longitude2)) * 6371000 / 1000.
Next, you have to create a struct to represent a pizza delivery service.
- In
PizzaDeliveryService.swift
, declare a struct calledPizzaDeliveryService
. - Your
PizzaDeliveryService
struct should have two properties:- A
Coordinate
calledlocation
that represents where the pizza delivery service is located. - An
Int
calledpizzasAvailable
. This should be a variable. It represents the service's inventory.
- A
- Create an initializer that takes one parameter, a
Coordinate
calledlocation
, and assigns it to thelocation
property. It should also make sure thepizzasAvailable
property is initially set to10
. - Create a method called
isInRange(to:)
. This method takes one parameter, aCoordinate
calleddestination
, and returnstrue
if the destination is no more than 5000 kilometers from the pizza delivery service. - Create a method called
deliverPizza(to:)
. This method takes one parameter, aCoordinate
calleddestination
. If the destination is in range of the pizza delivery service, it subtracts one from the number of pizzas available and returnstrue
. Otherwise, it simply returnsfalse
. If there are no pizzas available, it also returnsfalse
.
Finally, you have to create a Person
struct, so someone can eat all these pizzas.
- In
Person.swift
, declare a new struct calledPerson
. - Add four properties to your
Person
struct:- A
String
calledfirstName
- A
String
calledlastName
- A
Coordinate
calledhome
- A
Bool
calledisHungry
- A
- Create an initializer that takes three parameters: a
String
calledfirstName
, aString
calledlastName
, and aCoordinate
calledhome
. These parameters should be assigned to the appropriate properties.isHungry
should initially be set totrue
. - Create a method called
eatPizza(from:)
. This method takes one parameter, aPizzaDeliveryService
calledservice
that represents where the pizza is being ordered from. If the person is hungry and the service is in range of the person's home, then theisHungry
property should be set tofalse
and the method should returntrue
. If the person is not hungry or the service is not in range of the person's home, the method should simply returnfalse
.
That's it! Run the tests for this project. If they all pass, you have completed the lab! If not, look back over these directions and try to find and fix what you did wrong. Good luck!
View Swift Struct Pizza Delivery Lab on Learn.co and start learning to code for free.