Exercice 3: Objet Voiture2 initialisé. Partie 2 terminée.
This commit is contained in:
parent
743a6c5c4d
commit
576b0fb369
2 changed files with 72 additions and 0 deletions
20
src/exercice3part2.java
Normal file
20
src/exercice3part2.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import partie3.Voiture2;
|
||||||
|
|
||||||
|
public class exercice3part2 {
|
||||||
|
public static void main(String[] Argv)
|
||||||
|
{
|
||||||
|
Voiture2 peugeot = new Voiture2(200);
|
||||||
|
peugeot.setVitesse(500);
|
||||||
|
peugeot.demarre();
|
||||||
|
printVoiture(peugeot);
|
||||||
|
|
||||||
|
peugeot.setVitesse(200);
|
||||||
|
peugeot.demarre();
|
||||||
|
printVoiture(peugeot);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printVoiture(Voiture2 v)
|
||||||
|
{
|
||||||
|
System.out.println("VOITURE:\tPuissance: "+v.getPuissance()+"\tVitesse: "+v.getVitesse());
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,4 +4,56 @@ public class Voiture2 {
|
||||||
int puissance;
|
int puissance;
|
||||||
int vitesse;
|
int vitesse;
|
||||||
boolean estDemarre;
|
boolean estDemarre;
|
||||||
|
|
||||||
|
public Voiture2()
|
||||||
|
{
|
||||||
|
this.puissance = 10;
|
||||||
|
this.vitesse = 0;
|
||||||
|
this.estDemarre = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Voiture2(int p)
|
||||||
|
{
|
||||||
|
this();
|
||||||
|
puissance = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPuissance()
|
||||||
|
{
|
||||||
|
return this.puissance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPuissance(int p)
|
||||||
|
{
|
||||||
|
this.puissance = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVitesse()
|
||||||
|
{
|
||||||
|
return this.vitesse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVitesse(int v)
|
||||||
|
{
|
||||||
|
if (v <= this.puissance)
|
||||||
|
{
|
||||||
|
this.vitesse = v;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Vitesse trop élevée !!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void demarre()
|
||||||
|
{
|
||||||
|
if(vitesse > 0)
|
||||||
|
{
|
||||||
|
this.estDemarre = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Ne démarre que si une vitesse est initialisée.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue