Skip to content

Instantly share code, notes, and snippets.

@BENALI31
Created February 12, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BENALI31/e4dcf1c38ecc08348a089948a76a3dd5 to your computer and use it in GitHub Desktop.
Save BENALI31/e4dcf1c38ecc08348a089948a76a3dd5 to your computer and use it in GitHub Desktop.
tp java
public class CompteurNChiffres
{
// Attributs
private int km;
private int limite;
// M�thodes
// Constructeur
public CompteurNChiffres(int n)
{
this.limite = this.calculeLimite(n);
this.km = 0;
}
public void afficher()
{
// Conversion d'un entier en chaine --> ""+this.km
System.out.println(String.valueOf(this.km));
}
public void incrementer()
{
this.km ++;
if (this.km> this.limite)
this.km= 0;
}
private int calculeLimite (int n)
{
int i = 1;
// Calcul de la limite : Puissance de 10...
for (int j = 0; j<n; j++)
{
i = i*10;
}
return (i-1);
}
}
public class Main
{
// Programme principal permettant de tester le compteur
public static void main(String a[])
{
CompteurNChiffres c = new CompteurNChiffres(2);
for (int i=0; i<25; i++)
c.incrementer();
c.afficher(); // 25
for (int i=0; i<75; i++)
c.incrementer();
c.afficher(); // 0
for (int i=0; i<34; i++)
c.incrementer();
c.afficher(); // 34
}
}
public class Main {
public static void main (String[]a){
Compteur3chiffres cpt ;
cpt = new Compteur3chiffres();
cpt.afficher();
for(int i=0;i<750;i++){
cpt.incrementer();
}
cpt.afficher();
}
}
public_class Compteur3chiffres{
private int km;
public Compteur3chiffres(){
this.km=0;
}
public void afficher(){
System.Out.println("le nombre de kilometres parcourus"+this.km);
}
public void incrementer (){
this.km=this.km+1;
if (this.km>999){
this.km=0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment