Advertisement
techno-

Ej 3 DS final

Oct 4th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package Triangle;
  2.  
  3. import java.util.Objects;
  4.  
  5.  
  6. public record Triangle(int angle0, int angle1, int angle2){
  7.     /**
  8.      * Constructs a Triangle.Triangle object given its three internal angles
  9.      * It is the canonical constructor .
  10.      * @param angle0 Angle 0
  11.      * @param angle1 Angle 1
  12.      * @param angle2 Angle 2
  13.      * @throws IllegalArgumentException if the angles do not sum 180 degrees
  14.      */
  15.  
  16.     public Triangle{
  17.         if (angle0 + angle1 + angle2 != 180) {
  18.             throw new IllegalArgumentException("Los ángulos deben sumar 180\n");
  19.         }
  20.     }
  21.  
  22.     /**
  23.      * Copy constructor . Constructs a Triangle.Triangle using another Triangle.Triangle .
  24.      * @param t The Triangle.Triangle object to copy .
  25.      */
  26.     public Triangle (Triangle t){
  27.         this(t.angle0,t.angle1,t.angle2);
  28.     }
  29.  
  30.     /**
  31.      * Tests if a triangle is right .
  32.      * A right triangle has one of its angles measuring 90 degrees .
  33.      * @return True if it is right , false otherwise
  34.      */
  35.  
  36.     public boolean isRight(){
  37.         if(angle0 == 90 || angle1 == 90 || angle2 == 90){
  38.             return true;
  39.         }else return false;
  40.     }
  41.     /**
  42.      * Tests if a triangle is acute .
  43.      * A triangle is acute if all angles measure less than 90 degrees .
  44.      * @return True if it is acute , false otherwise
  45.      */
  46.     public boolean isAcute () {
  47.         if(angle0 <90 && angle1 <90 && angle2 <90){
  48.             return true;
  49.         }else return false;
  50.     }
  51.     /**
  52.      * Tests if a triangle is obtuse .
  53.      * A triangle is obtuse if it has one angle measuring more than 90 degrees .
  54.      * @return True if it is obtuse , false otherwise
  55.      */
  56.     public boolean isObtuse () {
  57.         if(angle0 >90 || angle1 >90 || angle2 >90){
  58.             return true;
  59.         }else return false;
  60.     }
  61.     /**
  62.      * Tests if a triangle is equilateral .
  63.      * A triangle is equilateral if all the angles are the same .
  64.      * @return True if it is equilateral , false otherwise
  65.      */
  66.  
  67.     public boolean isEquilateral () {
  68.         if(angle0 == angle1 && angle1 == angle2){
  69.             return true;
  70.         }else return false;
  71.     }
  72.     /**
  73.      * Tests if a triangle is isosceles .
  74.      * A triangle is isosceles if it has two angles of the same measure .
  75.      * @return True if it is isosceles , false otherwise
  76.      */
  77.  
  78.     public boolean isIsosceles () {
  79.         if(angle0 == angle1 && angle0 != angle2 || angle0 == angle2 && angle0 != angle1 || angle1 == angle2 && angle1 != angle0){
  80.             return true;
  81.         }else return false;
  82.     }
  83.     /**
  84.      * Tests if a triangle is scalene .
  85.      * A triangle is scalene if it has all angles of different measure .
  86.      * @return True if it is scalene , false otherwise
  87.      */
  88.  
  89.     public boolean isScalene () {
  90.         if(angle0 != angle1 && angle0 != angle2 && angle1 != angle2){
  91.             return true;
  92.         }else return false;
  93.     }
  94.     /**
  95.      * Tests if two triangles are equal .
  96.      * Two triangles are equal if their angles are the same ,
  97.      * regardless of the order .
  98.      * @param o The reference object with which to compare .
  99.      * @return True if they are equal , false otherwise .
  100.      */
  101.  
  102.     @Override
  103.     public boolean equals (Object o) {
  104.         if (o == null) {
  105.             return false;
  106.         } else if (getClass() != o.getClass()) {
  107.             return false;
  108.         } else {
  109.             Triangle aux = ((Triangle) o);
  110.             if (angle0 == aux.angle0 && (angle1 == aux.angle1 || angle1 == aux.angle2)) {
  111.                 return true;
  112.             } else if (angle1 == aux.angle0 && (angle0 == aux.angle1 || angle0 == aux.angle2)) {
  113.                 return true;
  114.             } else if (angle2 == aux.angle0 && (angle0 == aux.angle1 || angle0 == aux.angle2)) {
  115.                 return true;
  116.             } else return false;
  117.         }
  118.     }
  119.     /**
  120.      * Hashcode function whose functioning is consistent with equals .
  121.      * Two triangles have the same hashcode if their angles are the same ,
  122.      * regardless of the order .
  123.      * @return A value that represents the hashcode of the triangle .
  124.      */
  125.  
  126.     @Override
  127.     public int hashCode() {
  128.         int a= angle0;
  129.         int b= angle1;
  130.         int c= angle2;
  131.         int aux;
  132.  
  133.         if(a>b){
  134.             aux = a;
  135.             a=b;
  136.             b=aux;
  137.         }if(a>c){
  138.             aux = a;
  139.             a=c;
  140.             c=aux;
  141.         }if(b>c){
  142.             aux = b;
  143.             b=c;
  144.             c=aux;
  145.         }
  146.         return Objects.hash(a,b,c);
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement