Day 04
Day 04
Day 04
Today we will continue to go deeper into OOP, you will keep using all the notions from the last time and
static keyword
namespace
Exercise 01 (3pts)
Create a new class Mars : it will have an attribute id for which you will create a getter (getId) but no setter.
You must create your class so that the first instance of Mars has an id of 0, the second an id of 1…
Example :
cat example.php
<?php
include_once("Mars.php");
$rocks = new Mars();
$lite = new Mars();
$snack = new Mars();
echo $rocks->getId() . "\n";
echo $lite->getId() . "\n";
echo $snack->getId() . "\n";)
php example.php
0
1
2
Exercise 02 (2pts)
Turn in: ex_04/Astronaut.php
The name must be passed during the creation of the Astronaut, it is mandatory, his snack will be initialized to 0 and his destination to null.
The id must be unique and be incremented for each new Astronaut being created (i.e the first Astronaut of our program must have id 0, then the second one will have
id 1 etc…)
You must also add a getter for id ( getId ) but no setter, and a getter for destination ( getDestination ) without setter.
Example :
cat example.php
<?php
include_once("Astronaut.php");
$mutta = new Astronaut("Mutta");
$hibito = new Astronaut("Hibito");
echo $mutta->getId() . "\n";
echo $hibito->getId() . "\n";)
php example.php
Mutta ready for launch !
Hibito ready for launch !
0
1
Exercise 03 (3pts)
Turn in: ex_03/Mars.php
Use your class Mars from the first exercise without changing it.
To differentiate the two Mars, you’ll put the first one (the one from exercise 1) in a namespace called chocolate and the second one in a namespace called
planet .
You should turn in only one file Mars.php, all your code must be in this file.
You will also add a private attribute size to this new Mars class, with a getter and a setter (getSize and setSize).
It must be possible to specify the size of the planet when creating one thanks to its constructor, but it shouldn’t be mandatory.
Exercise 04 (4pts)
Restrictions: In this exercise every time we ask for [Name] to be displayed it should be replaced by the name of the Astronaut for example:
If it is not already done, add a getter for his snacks ( getSnacks ), no setter should be created.
Because some people can’t guess obvious things (Obivously I won’t say who…), you will also need to store our Astronaut's new destination to his attribute destination
(see Exercise 2).
Moreover, if our Astronaut received a snack, you will also need to increment his attribute snacks by one.
Finally, during its destruction, if a destination has been selected it will display:
[Name]: I may have done nothing, but I have [x] Mars to eat at least !
Where [x] should be replaced by the number of snacks possessed by our Astronaut.
Exercise 05 (2pts)
Turn in: ex_05/Phobos.php
Use your classes Mars from previous exercises without changing it.
This class must be in a namespace moon itself defined in the namespace planet .
Your class Phobos will have a private attribute named mars with a getter (getMars) but no setter.
No planet given.
Example :
cat example.php
<?php
include_once("Phobos.php");
$titi = new planetMars();
$toto = new planetMars();
new planetmoonPhobos($titi);
new planetmoonPhobos($toto);
php example.php
Phobos placed in orbit.
Phobos placed in orbit.
Exercise 06 (5pts)
Turn in: ex_06/Phobos.php
Create a new class Team that will represent a team of astronauts. This class must take a name as parameter during its construction that will be the name of team.
You will create an array which will contain all of your Astronauts and a getter (getAstronauts). You will also create a getter (getName) but no setter for this attribute.
You will also create a few methods to manipulate our team. The first one shall be add , it will take a parameter that shall be equal to an Astronaut. If the parameter
given is not an astronaut you must display:
Otherwise, you shall display nothing but the Astronaut should be added to the team.
You will also implement a method remove that will take an astronaut as parameter and remove him from the team.
This function will never display anything. You will create a method countMembers that will return the number of Astronauts currently in our team.
Obviously, you will display on mission if the Astronaut is currently on mission, otherwise you will display on standby .
Example :
cat example.php
<?php
include_once("Team.php");
include_once("Mars.php");
$spaceBro->add($mutta);
$spaceBro->add($hibito);
$spaceBro->add($serika);
$spaceBro->add(3);
$mutta->doActions($titi);
$spaceBro->showMembers();
$spaceBro->remove($hibito);
php example.php
Mutta ready for launch !
Hibito ready for launch !
Serika ready for launch !
SpaceBrothers: Sorry, you are not part of the team.
3
Mutta: started a mission !
SpaceBrothers: Mutta on mission, Hibito on standby, Serika on standby.
2
Serika: I may have done nothing, but I have 0 Mars to eat at least !
Hibito: I may have done nothing, but I have 0 Mars to eat at least !
Mutta: Mission aborted !
Exercise 07 (1pts)
Add a new method to your Team, doActions , the goal of this method is to call every doActions of the Team’s Astronauts with the parameter received.
Only once.
In the case of a chocolate received, we will admit that they share the chocolate between themselves but still count it as a full chocolate.
Moreover, you will need to modify your Astronaut class, now that our Astronauts have more experience, they can also go on a mission to Phobos.