In this exercise you will be implementing a simulation of encounters. This will familiarise you with the basics of multiple dispatch, Julia's main paradigm.
For this exercise, all function definitions should be a single-line declaration with no if/else logic.In general, encounters involve one entity a meeting another entity b and reacting to it.
encounter(a, b) = "$(name(a)) meets $(name(b)) and $(meets(a, b))."At first, we will simulate what happens when cats and dogs meet.
First add an abstract type Pet.
Then define types Cat and Dog as subtypes of Pet.
Each has a single field name.
Implement a function name() which returns the name of a pet.
julia> fido = Dog("Fido")
Dog("Fido")
julia> name(fido)
"Fido"Implement meets() methods for the following encounters:
julia> ginger = Cat("Ginger")
Cat("Ginger")
julia> meets(ginger, fido)
"hisses"Implement the encounter() function with one method, to report what happens when two named entities meet.
julia> encounter(ginger, fido)
"Ginger meets Fido and hisses."What happens if they encounter a different pet on their walk, like a horse?
julia> bella = Horse("Bella")
Horse("Bella")
julia> encounter(fido, bella)
"Fido meets Bella and is cautious."There are many other things that pets may encounter that aren't pets: cars, humans, plants, natural disasters, asteroids… What happens then?
julia> colnago = Bicycle("Colnago")
Bicycle("Colnago")
julia> encounter(ginger, colnago)
"Ginger meets Colnago and runs away."There are many other encounters that could occur in our simulation. A car meets a dog, two electrons encounter each other and interact…
It is impossible to cover all encounters in advance, therefore we will implement a generic fallback.
julia> γ1 = Photon("γ1")
Photon("γ1")
julia> γ2 = Photon("γ2")
Photon("γ2")
julia> encounter(γ1, γ2)
"γ1 meets γ2 and nothing happens."
# This is true, photons just pass through each other.In this exercise you will be implementing a simulation of encounters. This will familiarise you with the basics of multiple dispatch, Julia's main paradigm.
For this exercise, all function definitions should be a single-line declaration with no if/else logic.In general, encounters involve one entity a meeting another entity b and reacting to it.
encounter(a, b) = "$(name(a)) meets $(name(b)) and $(meets(a, b))."At first, we will simulate what happens when cats and dogs meet.
First add an abstract type Pet.
Then define types Cat and Dog as subtypes of Pet.
Each has a single field name.
Implement a function name() which returns the name of a pet.
julia> fido = Dog("Fido")
Dog("Fido")
julia> name(fido)
"Fido"Implement meets() methods for the following encounters:
julia> ginger = Cat("Ginger")
Cat("Ginger")
julia> meets(ginger, fido)
"hisses"Implement the encounter() function with one method, to report what happens when two named entities meet.
julia> encounter(ginger, fido)
"Ginger meets Fido and hisses."What happens if they encounter a different pet on their walk, like a horse?
julia> bella = Horse("Bella")
Horse("Bella")
julia> encounter(fido, bella)
"Fido meets Bella and is cautious."There are many other things that pets may encounter that aren't pets: cars, humans, plants, natural disasters, asteroids… What happens then?
julia> colnago = Bicycle("Colnago")
Bicycle("Colnago")
julia> encounter(ginger, colnago)
"Ginger meets Colnago and runs away."There are many other encounters that could occur in our simulation. A car meets a dog, two electrons encounter each other and interact…
It is impossible to cover all encounters in advance, therefore we will implement a generic fallback.
julia> γ1 = Photon("γ1")
Photon("γ1")
julia> γ2 = Photon("γ2")
Photon("γ2")
julia> encounter(γ1, γ2)
"γ1 meets γ2 and nothing happens."
# This is true, photons just pass through each other.