Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1.  
  2. public class Moteur {
  3.     // variables d'instance
  4.     private int puissance;
  5.     private char carburant;
  6.  
  7.     //getter/setter
  8.  
  9.     public int getPuissance() {
  10.         return puissance;
  11.     }
  12.  
  13.     public void setPuissance(int puissance) {
  14.         this.puissance = (puissance <= 1000 ? 1000 : puissance);
  15.     }
  16.  
  17.     public char getCarburant() {
  18.         return carburant;
  19.     }
  20.  
  21.     public void setCarburant(char carburant) {
  22.         this.carburant = (carburant != 'E' && carburant != 'D' && carburant != 'T' ? 'E' : carburant);
  23.         //E = essence, D = Diesel, si le carburant est différent de E et D alors il est assigné à E par défaut
  24.     }
  25.  
  26.     // Constructeurs
  27.     public Moteur() {
  28.         setCarburant('E');
  29.         setPuissance(1000);
  30.     }
  31.  
  32.     public Moteur(int puissance, char carburant) {
  33.         this.setPuissance(puissance);
  34.         this.setCarburant(carburant);
  35.     }
  36.  
  37.     //conversion to string
  38.  
  39.     @Override
  40.     public String toString() {
  41.         return "Moteur{" +
  42.                 "puissance=" + puissance +
  43.                 ", carburant=" + carburant +
  44.                 '}';
  45.     }
  46.  
  47.     //test unitaire
  48.     public static void main(String[] args) {
  49.         Moteur m1= new Moteur(1200, 'D');
  50.         Moteur m2= new Moteur( 1000, 'E');
  51.  
  52.         System.out.println(m1);
  53.         System.out.println(m2);
  54.  
  55.     }
  56. }
  57.  
  58.  
  59. public class Voiture {
  60.     // variables de classe
  61.     private static int dernier_num_serie = 1;
  62.     // variables d'instance
  63.     private int num_serie;
  64.     private String marque;
  65.     //composition;
  66.     private Moteur moteur = new Moteur();
  67.  
  68.     //getter/setter
  69.  
  70.     public int getDernier_num_serie() {
  71.         return dernier_num_serie;
  72.     }
  73.  
  74.     public int getNum_serie() {
  75.         return num_serie;
  76.     }
  77.  
  78.     public String getMarque() {
  79.         return marque;
  80.     }
  81.  
  82.  
  83.     private static void setDernier_num_serie() {
  84.         Voiture.dernier_num_serie++; // on incrémente le numéro de série
  85.     }
  86.     private void setNum_serie() {
  87.         this.num_serie = dernier_num_serie; // on fait correspondre le numéro de série au numéro de série précedemment incrémenté
  88.     }
  89.     private void setMarque(String marque) {
  90.         this.marque = ( marque==null || marque.trim().equals("")
  91.                 ? "FIAT"
  92.                 : marque.toUpperCase()
  93.         );
  94.     }
  95.     //Constructeurs
  96.     public Voiture(String marque, int puissance, char carburant) {
  97.         this.setMarque(marque);
  98.         this.moteur = new Moteur(puissance, carburant);
  99.  
  100.         this.setNum_serie();
  101.     }
  102.     public Voiture() {
  103.         this(null, 1000, 'E');
  104.     }
  105.  
  106.     public Voiture(String marque, Moteur moteur){
  107.         if(moteur!=null)
  108.             this.moteur = new Moteur(moteur.getPuissance(), moteur.getCarburant());
  109.         this.setNum_serie();
  110.  
  111.         this.setMarque(marque);
  112.     }
  113.  
  114.     @Override
  115.     public String toString() {
  116.         return "Voiture{" +
  117.                 "num_serie=" + num_serie +
  118.                 ", marque='" + marque + '\'' +
  119.                 ", moteur=" + moteur +
  120.                 '}';
  121.     }
  122.     public static void main(String[] args) {
  123.         Voiture v1 = new Voiture("",new Moteur());
  124.         Voiture v2 = new Voiture("Renaud",new Moteur());
  125.         Voiture v3 = new Voiture("Hot Wheels",12000,'T');
  126.  
  127.         System.out.println(v1);
  128.         System.out.println(v2);
  129.         System.out.println(v3);
  130.     }
  131. }
  132.  
  133.  
  134. public class Personne {
  135.     // variables de classe
  136.     private static int dernier_matricule = 1;
  137.     // variables d'instance
  138.     private int matricule;
  139.     private String nom;
  140.     private String prenom;
  141.  
  142.     //getter
  143.  
  144.     public static int getDernier_matricule() {
  145.         return dernier_matricule;
  146.     }
  147.  
  148.     public int getMatricule() {
  149.         return matricule;
  150.     }
  151.  
  152.     public String getNom() {
  153.         return nom;
  154.     }
  155.  
  156.     public String getPrenom() {
  157.         return prenom;
  158.     }
  159.  
  160.     //setter
  161.  
  162.     public static void setDernier_matricule(int dernier_matricule) {
  163.         Personne.dernier_matricule++; // on incrémente le numéro de série
  164.     }
  165.  
  166.     public void setMatricule(int matricule) {
  167.         this.matricule = dernier_matricule;
  168.     }
  169.  
  170.     public void setNom(String nom) {
  171.         this.nom = nom;
  172.     }
  173.  
  174.     public void setPrenom(String prenom) {
  175.         this.prenom = prenom;
  176.     }
  177. }
  178.  
  179.  
  180.  
  181. public class Complex {
  182.     private float realnum;
  183.     private float complexnum;
  184.  
  185.     //getter
  186.  
  187.     public float getRealnum() {
  188.         return realnum;
  189.     }
  190.  
  191.     public float getComplexnum() {
  192.         return complexnum;
  193.     }
  194.  
  195.     //setter
  196.  
  197.     public void setRealnum(float realnum) {
  198.         this.realnum = realnum;
  199.     }
  200.  
  201.     public void setComplexnum(float complexnum) {
  202.         this.complexnum = complexnum;
  203.     }
  204.     //fonction addition
  205.     public Complex addition(Complex c2){
  206.         float reel = getRealnum() + c2.getRealnum();
  207.         float complex = getComplexnum() + c2.getComplexnum();
  208.         return new Complex(complex, reel);
  209.     }
  210.     //fonction soustraction
  211.     public Complex soustraction(Complex c2){
  212.         float reel = getRealnum() - c2.getRealnum();
  213.         float complex = getComplexnum() - c2.getComplexnum();
  214.         return new Complex(complex, reel);
  215.     }
  216.     //fonction multiplication
  217.     public Complex multiplication(Complex c2){
  218.         float reel = (getRealnum() * c2.getRealnum());
  219.         float complex = getComplexnum() * c2.getComplexnum();
  220.         return new Complex(complex, reel);
  221.     }
  222.  
  223.  
  224.     //constructeur
  225.     public Complex() {
  226.         setComplexnum(1);
  227.         setRealnum(1);
  228.     }
  229.     public Complex(float complexnum, float realnum) {
  230.         this.setComplexnum(complexnum);
  231.         this.setRealnum(realnum);
  232.     }
  233.  
  234.     //conversion to string
  235.  
  236.  
  237.     @Override
  238.     public String toString() {
  239.         return "Complex{" +
  240.                 "a=" + realnum +
  241.                 ", b=" + complexnum +
  242.                 "i" +
  243.                 '}';
  244.     }
  245.  
  246.     //test unitaire
  247.     public static void main(String[] args) {
  248.         Complex c1= new Complex(2,3);
  249.         Complex c2= new Complex(5,8);
  250.         Complex c3= c1.addition(c2);
  251.         Complex c4= c1.soustraction(c3);
  252.  
  253.         System.out.println(c1);
  254.         System.out.println(c3);
  255.         System.out.println(c4);
  256.  
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement