Created
March 26, 2024 23:16
-
-
Save dmitriyvolk/46a6a4454c4a75566a2affed38a421db to your computer and use it in GitHub Desktop.
Generics: RedRover 2024 Spring Lecture 16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package animals; | |
public class Animal { | |
private final String name; | |
private final double weight; | |
private final double canLiftWeight; | |
public Animal(String name, double weight, double canLiftWeight) { | |
this.name = name; | |
this.weight = weight; | |
this.canLiftWeight = canLiftWeight; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getWeight() { | |
return weight; | |
} | |
public double getCanLiftWeight() { | |
return canLiftWeight; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package animals; | |
import comparators.GenericComparator; | |
public class Animals { | |
private Animals() {} | |
public static final Animal TIGER = new Animal("Тигр", 550, 1100); | |
public static final Animal ELEPHANT = new Animal("Слон", 4000, 6800); | |
public static final Animal BEAR = new Animal("Медведь", 500, 500); | |
public static final Animal ANT = new Animal("Муравей", 0.000003, 0.000060); | |
public static final Animal DUNG_BEETLE = new Animal("Навозный Жук", 0.002, 2.282); | |
public static final GenericComparator<Animal> HEAVIEST = new GenericComparator<Animal>() { | |
@Override | |
public int compare(Animal a, Animal b) { | |
if (a.getWeight() > b.getWeight()) return 1; | |
if (a.getWeight() < b.getWeight()) return -1; | |
return 0; | |
} | |
}; | |
public static final GenericComparator<Animal> STRONGEST = new GenericComparator<Animal>() { | |
@Override | |
public int compare(Animal a, Animal b) { | |
double relativeWeightA = a.getCanLiftWeight() / a.getWeight(); | |
double relativeWeightB = b.getCanLiftWeight() / b.getWeight(); | |
if (relativeWeightA > relativeWeightB) return 1; | |
if (relativeWeightA < relativeWeightB) return -1; | |
return 0; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package animals; | |
import comparators.Comparators; | |
import static animals.Animals.*; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println(Comparators.findBest( | |
List.of(TIGER, ELEPHANT, BEAR, ANT, DUNG_BEETLE), | |
STRONGEST | |
).getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comparators; | |
import java.util.List; | |
public class Comparators { | |
public static final GenericComparator<Integer> MORE = new GenericComparator<Integer>() { | |
@Override | |
public int compare(Integer a, Integer b) { | |
if (a > b) return 1; | |
if (a < b) return -1; | |
return 0; | |
} | |
}; | |
public static final GenericComparator<Integer> ABS_MORE = new GenericComparator<Integer>() { | |
@Override | |
public int compare(Integer a, Integer b) { | |
return MORE.compare(Math.abs(a), Math.abs(b)); | |
} | |
}; | |
public static final GenericComparator<String> LONGEST_STRING = new GenericComparator<String>() { | |
@Override | |
public int compare(String a, String b) { | |
if (a.length() > b.length()) return 1; | |
if (a.length() < b.length()) return -1; | |
return 0; | |
} | |
}; | |
public static <T> T findBest(List<T> elements, GenericComparator<T> comparator) { | |
if (elements.size() == 0) return null; | |
T champion = elements.get(0); | |
for (int i = 1; i < elements.size(); i++) { | |
if (comparator.compare(elements.get(i), champion) > 0) { | |
champion = elements.get(i); | |
} | |
} | |
return champion; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comparators; | |
public interface GenericComparator<T> { | |
int compare(T a, T b); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comparators; | |
public class IntegerLess implements GenericComparator<Integer> { | |
@Override | |
public int compare(Integer a, Integer b) { | |
if (a < b) return 1; | |
if (a > b) return -1; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment