1. Write a java class called Pokemon that has a name (e.g. “Pikachu”) and a number (e.g. 25). Implement a constructor that takes both values as parameters. Implement accessors for both values. Implement a toString() that returns a string in the format “Name(number)”, e.g. “Pikachu(25)”. Implement an equals(Object) method (two Pokémon are considered equal if they have the same number). Implement a hashCode() method that returns the number. (remember the equals and hashCode contract). 2. Create a java class, PokemonComparator, that implements Comparator and compares two Pokémon in reverse order by number. For example, if Pokemon A's number is 25 and B's number is 50, compare(A, B) would return a number > 0 because B should come first. Hint: what if you subtracted the numbers? 3. Write a new class, PokemonMain, with a main method that: a. Creates a binary tree with your favorite Pokémon at the root and a new PokemonComparator. b. Insert at least 10-15 more Pokémon in any order. c. Stringify the binary tree and print the string. d. They should print in reverse order by number (from largest to smallest). 4. Update your Pokémon class to implement Comparable so that the Pokémon are arranged in order by number (from smallest to largest). For example, if Pokemon A's number if 25 and B's number is 50, A.compareTo(B) would return a number < 0 because A should come first.
1. Write a java class called Pokemon that has a name (e.g. “Pikachu”) and a number (e.g. 25).
Implement a constructor that takes both values as parameters.
Implement accessors for both values.
Implement a toString() that returns a string in the format “Name(number)”, e.g. “Pikachu(25)”.
Implement an equals(Object) method (two Pokémon are considered equal if they have the same number).
Implement a hashCode() method that returns the number. (remember the equals and hashCode contract).
2. Create a java class, PokemonComparator, that implements Comparator and compares two Pokémon in reverse order by number. For example, if Pokemon A's number is 25 and B's number is 50, compare(A, B) would return a number > 0 because B should come first. Hint: what if you subtracted the numbers?
3. Write a new class, PokemonMain, with a main method that:
a. Creates a binary tree with your favorite Pokémon at the root and a new PokemonComparator.
b. Insert at least 10-15 more Pokémon in any order.
c. Stringify the binary tree and print the string.
d. They should print in reverse order by number (from largest to smallest).
4. Update your Pokémon class to implement Comparable so that the Pokémon are arranged in order by number (from smallest to largest). For example, if Pokemon A's number if 25 and B's number is 50, A.compareTo(B) would return a number < 0 because A should come first.
Trending now
This is a popular solution!
Step by step
Solved in 9 steps with 7 images