Advertisement
xickoh

ED Ficha 1 Exer 3

Oct 10th, 2020 (edited)
1,363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. //------------------------------------UnorderedPair.java------------------------------------//
  2.  
  3. package ed_ficha01;
  4.  
  5. public class UnorderedPair<T extends Comparable> extends Pair<T> {
  6.  
  7.     public UnorderedPair() {
  8.         setFirst(null);
  9.         setSecond(null);
  10.     }
  11.  
  12.     public UnorderedPair(T firstItem, T secondItem) {
  13.         setFirst(firstItem);
  14.         setSecond(secondItem);
  15.     }
  16.  
  17.     public boolean match() {
  18.         return this.getFirst().equals(this.getSecond());
  19.     }
  20.  
  21.     public boolean equals(Object otherObject) {
  22.         if (otherObject == null) {
  23.             return false;
  24.         } else if (getClass() != otherObject.getClass()) {
  25.             return false;
  26.         } else {
  27.             UnorderedPair<T> otherPair
  28.                     = (UnorderedPair<T>) otherObject;
  29.             return (getFirst().equals(otherPair.getFirst())
  30.                     && getSecond().equals(otherPair.getSecond()))
  31.                     || (getFirst().equals(otherPair.getSecond())
  32.                     && getSecond().equals(otherPair.getFirst()));
  33.         }
  34.     }
  35. }
  36.  
  37. //------------------------------------Main.java------------------------------------//
  38.  
  39. public static void main(String[] args) {
  40.        
  41.     Person p1 = new Person("Francisco", 23);
  42.         Person p2 = new Person("Ana", 24);
  43.  
  44.         TwoTypePair<Person, Person> ttp1 = new TwoTypePair<Person, Person>(p1, p2);
  45.         TwoTypePair<Person, Person> ttp2 = new TwoTypePair<Person, Person>(p1, p2);
  46.         if (ttp1.equals(ttp2)) {
  47.             System.out.println("São iguais");
  48.         }
  49.  
  50.         Pair<Person> pair1 = new Pair<Person>(p1, p2);
  51.         Person p3 = (Person) pair1.max();
  52.         System.out.println("O mais velho é " + p3.getName());
  53.  
  54.         UnorderedPair<Person> up1 = new UnorderedPair<Person>(p1, p2);
  55.         System.out.println("O primeiro elemento do par é " + up1.getFirst().getName());
  56.         System.out.println("O segundo elemento do par é " + up1.getSecond().getName());
  57.         System.out.println("São iguais? " + up1.match());
  58.  
  59.     }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement