Exercice java orienté objet corrigé | Exam-Lib
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice
Welcome to our Education website, plz like our page facebook to support us. Thank You and wish you good navigation

Exercice java orienté objet corrigé

abdelouafiNov 24, 2017

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      617
      Likes Received:
      12
      Trophy Points:
      18
      Joined
      Sep 13, 2016
      Exercice java orienté objet corrigé: Une classe pour modéliser un segment de droite.

      Exercice 1:
      upload_2017-11-24_19-59-4.png
      upload_2017-11-24_19-59-58.png
      upload_2017-11-24_20-1-24.png
       
      Loading...
      Exercice 1:
      La classe Segment:
      Code:
      package segment;
      
      public class Segment {
          private int extr1, extr2;
      
          public Segment(int e1, int e2) {
              extr1 = e1;
              extr2 = e2;
              ordonne();
          }
      
          private void ordonne() {
              if (extr1 > extr2){
                  int tampon;
      
                  tampon = extr1;
                  extr1 = extr2;
                  extr2 = tampon;
              }
          }
      
          public void setExtr1(int a) {
              extr1 = a;
              ordonne();
          }
      
          public int getExtr1() {
              return extr1;
          }
      
          public void setExtr2(int a) {
              extr2 = a;
              ordonne();
          }
      
          public int getExtr2() {
              return extr2;
          }
      
          public int longueur() {
              return extr2 - extr1;
          }
      
          public boolean appartient(int x) {
              return (x >= extr1) && (x <= extr2) ;
          }
      
          public String toString() {
              return "segment [" + extr1 + ", " + extr2 + "]";
          }
      }

      Code:
      package segment;
      
      class TestSegment  {
          public static void main(String[] arg) {
              Segment s = new Segment(Integer.parseInt(arg[0]),
                      Integer.parseInt(arg[1]));
              int point;
              System.out.println("Longueur du " + s + " : " + s.longueur());
              point = Integer.parseInt(arg[2]);
              if (s.appartient(point))
                  System.out.println(point + " appartient au " + s);
              else
                  System.out.println(point + " n'appartient pas au " + s);
          }
      }

Share This Page

Share