Correction du TP de l’atelier7 : « Interface »

 

interface Veh_a_moteur

{

            int PUISSFISCALE = 1;   // Par défaut, c’est static et final

            int PUISSREELLE = 2;

            public int rendPuissance(int i);          // Par défaut, c’est abstract

            public float rendConso();      

}

 

interface Veh_flottant

{

            public float rendTirantEau();

}                     

 

public class Bateau_a_moteur implements Veh_a_moteur, Veh_flottant, Comparable

{

            private int puissFiscale;         

            private int puissReelle;

            private float conso;

            private float tirantEau;

            private float longueur;

           

            public int compareTo(Object o)

            {

            if (this.longueur < ((Bateau_a_moteur)o).longueur)

            {

                        return -1;

            }

            else

            {

                        if (this.longueur == ((Bateau_a_moteur)o).longueur)

                        {

                                   return 0;

                        }

                        else

                        {

                                   return 1;

                        }

            }

 

            public Bateau_a_moteur (int pf, int pr, float c, float t, float l)

            {

                        puissFiscale = pf;       

                        puissReelle = pr;

                        conso = c;

                        tirantEau = t; 

                        longueur=l;

            }

            public int rendPuissance(int i)

            {

                        return (i==PUISSFISCALE ? puissFiscale : puissReelle);

            }

            public float rendConso()

            {

                        return conso;

            }

            public float rendTirantEau()

            {

                        return tirantEau;

            }

            public float rendLongueur()

            {

                        return longueur;

            }

            public void affich()

            {

                        System.out.println ("Puissance fiscale : " + rendPuissance(1));        //On peut

                                                           // bien sûr utiliser directement la donnée

                        System.out.println ("Puissance réelle : " + rendPuissance(2));        

                        System.out.println ("Consommation : " + conso);    

                        System.out.println ("Tirant d'eau : " + tirantEau);   

                        System.out.println ("Longueur : " + longueur);        

            }

}

 

 

public class Test_Bateau_a_moteur

{

            public static void main (String liste[])

            {

                        Bateau_a_moteur b = new Bateau_a_moteur(7,50,30F,1.5F,13F);

                        b.affich();

                        System.out.println ("\n");

                        Bateau_a_moteur b2 = new Bateau_a_moteur(7,50,30F,1.5F,11F);

                        b2.affich();

                        System.out.println ("\n");

                        int res = b.compareTo(b2);

                        String mess;

                        if (res==1)

                        {

                                   mess="Le 1er bateau est plus long que le second";

                        }

                        else

                        {

                                   if (res==0)

                                   {

                                               mess="Les 2 bateaux ont la même longueur";

                                   }

                                   else

                                   {

                                               mess="Le second bateau est plus long que le 1er";

                                   }

                        }

            }

}