Arkanium77

Triangle

Oct 11th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package trianglemain;
  2.  
  3. class Triangle{
  4.     protected double a,b,c;
  5.     Triangle(double a,double b,double c)throws SidesException,NegativeException{
  6.         if(a<=0||b<=0||c<=0)throw new NegativeException();
  7.         if(test(a,b,c))throw new SidesException();
  8.         this.a=a;
  9.         this.b=b;
  10.         this.c=c;
  11.     }
  12.     Triangle(double ab,double c)throws SidesException,NegativeException{
  13.         if(ab<=0||c<=0)throw new NegativeException();
  14.         if(test(ab,ab,c))throw new SidesException();
  15.         this.a=ab;
  16.         this.b=ab;
  17.         this.c=c;
  18.     }
  19.     Triangle(double a)throws SidesException,NegativeException{
  20.         if(a<=0)throw new NegativeException();
  21.         if(test(a,a,a))throw new SidesException();
  22.         this.a=a;
  23.         this.b=a;
  24.         this.c=a;
  25.     }
  26.     Triangle(Triangle t){
  27.         /*А вот здесь исключения не нужны. Если уж человек умудрится
  28.         создать объект с вырождеными значениями, значит оно ему надо*/
  29.         this.a=t.a;
  30.         this.b=t.b;
  31.         this.c=t.c;
  32.  
  33.     }
  34.     Triangle()throws SidesException,NegativeException{
  35.         this(1);
  36.     }
  37.     static boolean test(double q,double w,double e){
  38.         if(q+w>e&&q+e>w&&e+w>q) return false;
  39.         return true;
  40.     }
  41.     double p(){//Периметр
  42.         return a+b+c;
  43.     }
  44.     double s(){//Площадь
  45.         double P=p()/2;
  46.         return Math.sqrt(P*(P-a)*(P-b)*(P-c));
  47.     }
  48.     double rr(){//Радиус описанной окружности
  49.         return (a*b*c)/(4*s());
  50.     }
  51.     double r(){//Радиус вписанной окружности
  52.         return ((2*s())/(a+b+c));
  53.     }
  54.     static boolean equals(Triangle one, Triangle two){ //Сравнить треугольники
  55.         if (one.a==two.a){
  56.             if(one.b==two.b){
  57.                 if(one.c==two.c){
  58.                     return true;
  59.                 }
  60.             }
  61.             if(one.b==two.c){
  62.                 if(one.c==two.b){
  63.                     return true;
  64.                 }
  65.             }
  66.         }
  67.         if (one.b==two.a){
  68.             if(one.c==two.b){
  69.                 if(one.a==two.c){
  70.                     return true;
  71.                 }
  72.             }
  73.             if(one.c==two.c){
  74.                 if(one.a==two.b){
  75.                     return true;
  76.                 }
  77.             }
  78.         }
  79.         if (one.c==two.a){
  80.             if(one.a==two.b){
  81.                 if(one.b==two.c){
  82.                     return true;
  83.                 }
  84.             }
  85.             if(one.a==two.c){
  86.                 if(one.b==two.b){
  87.                     return true;
  88.                 }
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.     Triangle copy()throws SidesException,NegativeException{
  94.         return new Triangle(a,b,c);
  95.     }
  96.     String get(){
  97.         return "a="+a+" b="+b+" c="+c;
  98.     }
  99.     public String toString(){
  100.         return "треугольник со сторонами a="+a+", b="+b+", c="+c;
  101.     }
  102. }
  103. /*-------------------------------------------*/
  104. class NegativeException extends Exception{
  105.     public String toString(){
  106.         return "Длина стороны треугольника не может быть отрицательной!";
  107.     }
  108. }
  109.  
  110. class SidesException extends Exception{
  111.     public String toString(){
  112.         return "В невырожденном треугольнике сумма длин двух его сторон больше длины третьей стороны";
  113.     }
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment