StarWars Task
StarWars Task
A starship has a name. If it protects a planet, the planet is also known. It has 3
properties: its armour (natural number), its shield (natural number), and the
number of guardsmen on the ship. There are 3 types of ship: the destroyer, the
transport, and the ironclad. The power of a ship depends on its type and its
properties:
• The destroyer’ power is given as the half of its shield.
• The transport’s power means the number of guardsmen on it.
• The ironclad’ power means its armour.
1. Find the ship with the greates power in the Solar System!
2. List all the unprotected planets!
3. Give the total shield of the ships guarding a given planet!
Star Wars 2nd component of p.MaxPower()
+ planets
Starship if planet ≠ null then error endif *
# name : string planet := p Planet
# armour : int { getter p.Protect(this)
} - name : string
+ Planet( string) return |ships|
# shield : int * prot 0..1
# guards : int + ShipCount() : int ships.Add(s)
- ships # planet
+ Starship(string,int,int,int) + Protect(s:Starship)
+ Protect(p:Planet) + Leave(s:Starship) ships.Remove(s)
+ Leave() + TotalShield() : real {query}
+ Power() : real {virtual, query} + MaxPower() : bool×real×Starship {query}
if planet = null then error endif
planet.Leave(this) return MAX e.Power()
e∊ships
planet := null true
return ∑ e.GetShield()
e∊ships