Homework 4
In this homework you will extend the Diet trait from the lab by omnivores and make the Ecosystem.jl package pass automated quality checks. Start from your package from the lab, including the Diet trait with Herbivore and Carnivore.
How to submit?
Zip the whole package folder Ecosystem.jl and upload it to BRUTE. The package has to include at least the following files:
├── Project.toml
├── src
│ └── Ecosystem.jl
└── test
├── diet.jl # testsets for the Diet trait
└── runtests.jl # includes diet.jl and runs AquaThe test/runtests.jl file can look like this:
using Test
using Aqua
using Ecosystem
include("diet.jl")
@testset "Aqua" begin
Aqua.test_all(Ecosystem)
endOmnivores
Homework
Add a new trait value
Omnivore <: Diet. An omnivore eatsplants with
size > 0(like a herbivore),animals whose species is a
Herbivore(like a carnivore).
Add a new species
Bear <: AnimalSpecieswith the dietOmnivore. Use the same constructor pattern as forSheepandWolf, e.g.Bear(id; E=20.0, ΔE=4.0, pr=0.05, pf=0.3, s=randsex()), and print it as 🐻.Export
OmnivoreandBear.eatsandeat!have to work for all combinations of species, following the table:
| eats ↓ \ food → | Grass | Sheep | Wolf | Bear |
|---|---|---|---|---|
| Sheep | ✔ | |||
| Wolf | ✔ | |||
| Bear | ✔ | ✔ |
When a bear eats grass, it behaves like a sheep eating grass. When a bear eats a sheep, it behaves like a wolf eating a sheep.
HINTS:
Do not modify the methods for
HerbivoreandCarnivore. Can you reuse them forOmnivore?Do carnivores eat bears? Check that your rule for carnivores does not need to change.
Tests and quality checks
Homework
Write
test/diet.jlwith testsets that check the whole table above usingeats, and check thata
Beareating fully grownGrasssets itssizeto0,a
Beareating aSheepremoves the sheep from theWorld.
Add Aqua.jl as a test dependency of the package and make
Aqua.test_all(Ecosystem)pass.
HINTS:
Test dependencies go to
[extras]and[targets]inProject.toml(or totest/Project.toml), see the bachelor course.Aqua requires
[compat]entries for all dependencies, including julia and the test dependencies (Aqua,Test).Aqua checks method ambiguities. If you get one for
eatsoreat!, which methods are equally specific?]testhas to pass in a fresh Julia session.
Show solution
Nothing to see here.