$20
Problem Description
Having now solved the Pokemon health crisis, word of your programming ability quickly spreads throughout the land like a cool seed in The Game of Life. One day, you receive an email:
Hello there! Welcome to the world of POKEMON! My name is OAK! People call me the POKEMON PROF!
This world is inhabited by creatures called POKEMON! For some people, POKEMON are pets. Others use them for fights. Myself…I study POKEMON as a profession.
You soon learn that Oak is a professor at “Georgia Tech University”, which, he assures you, is a well known school. He wants you to help him write software to catalog and organize Pokemon so he can better research them. At first you’re skeptical, but once he promises you meal swipes, you promptly get to programming.
Solution Description
For this assignment, create the following files:
• PokemonSpecies.java
• AbstractDex.java
• Pokedex.java
PokemonSpecies Class:
In the world of Pokemon - all species of Pokemon (the equivalent of animals in this world) have been assigned a number for ease of reference and sorting. This representation of a PokemonSpecies, therefore, should implement the Comparable interface. A PokemonSpecies is “greater” when it has a higher speciesNumber; a PokemonSpecies is “lesser” when it has a lower speciesNumber.
Variables:
• speciesNumber, an int that represents the specific species of a given PokemonSpecies. Once set, this should not be changeable outside this class.
• speciesName, a String holding the name of the species. Once set, this should not be changeable outside this class.
• speciesNickname a String holding the nickname of the PokemonSpecies
• description a String holding a description of the PokemonSpecies.
Constructors:
• PokemonSpecies(String speciesName, int speciesNumber, String speciesNickname, String description)
– Sets speciesName, speciesNumber, speciesNickname, and description
• PokemonSpecies(String speciesName, int speciesNumber)
– Sets speciesName and speciesNumber. speciesNickname and description should be set to null.
Methods:
• int compareTo(PokemonSpecies other)
• boolean equals(Object other) PokemonSpecies are considered equal if they have the same speciesName and speciesNumber.
• int hashCode() Write a proper hashCode for PokemonSpecies that considers the proper fields (Hint: Look at equals)
• String toString() Should return “[speciesNumber]: [speciesName], the [speciesNickname] Pokemon.
[speciesDescription].”
• Getters for all fields.
• Setters for speciesNickname, description.
AbstractDex Class:
This class should be generic. The generic type should be bounded such that you can perform searching and sorting operations on a List of any generic type that can be sorted. We will refer to the generic as T but you can name it what you want as long as you follow the proper conventions. Because this simply serves as a base of reusable code for any type of dex, it should be abstract.
Instance Variables:
• ArrayList<T entries An ArrayList of objects that are comparable to each other.
• boolean sorted A boolean value storing whether or not entries have already been sorted by the dex.
Constructors:
• AbstractDex() Initializes entries to an empty ArrayList Methods:
• void insertionSort() sorts entries using the insertionSort() algorithm taught in class. Sets sorted to true
• void selectionSort() sorts entries using the selectionSort() algorithm taught in class. Sets sorted to true.
• int binarySearch(T element) searches entries for the passed in element using the binary search algorithm taught in class, returning its index if found or -1 if not found or sorted is false. DO NOT USE JAVA’S BUILT IN indexOf() METHOD.
• void addToDex(T newElement) If there is not already an equivalent element in entries, as determined by compareTo(), add newElement to the end of entries and set sorted to false.
• boolean equals(Object other) Write a proper equals method for AbstractDex. Two AbstractDex objects are considered equal if they have the same entries. For comparison, you can use ArrayList’s equals() method.
• int hashCode() Write a proper hashCode for a AbstractDex, using only the proper fields. (Hint: look at equals). You can use ArrayList’s hashCode() method.
• String toString() Should loop through each entry and call it’s toString(). Separate the outputs with newline characters. An example of what this should look like can be found in the sample tests.
• Getters for entries and sorted.
• Setter for entries.
Pokedex Class:
Is a concrete subclass of AbstractDex which holds PokemonSpecies only (Hint: how can we extend AbstractDex to only allow PokemonSpecies) Constructors:
• Pokedex() initializes entries to an empty ArrayList
• Pokedex(ArrayList<PokemonSpecies initialEntries) initializes entries with the elements in the passed in List.
Testing your code:
If you want to test out your own code before submitting we recommend creating a separate java file and implementing a main method there. You can then create new animals and test your methods. Below is some sample testing code. These tests are not comprehensive!
Pokedex myPokedex = new Pokedex();
PokemonSpecies carl = new PokemonSpecies("Carl", 1, "Head TA",
"Its Mixtapes are said to be too hot for the average listener"); System.out.println(carl);
// 1: Carl, the Head TA Pokemon. Its Mixtapes are said to be too hot for the average listener.
PokemonSpecies karl = new PokemonSpecies("Karl", 2, "Spooky TA",
"It is said Karl can be found lurking in Klaus during spooktober");
PokemonSpecies qarl = new PokemonSpecies("Qarl", 3, "Checkstyle TA",
"Qarl likes to remind students to checkstyle their work");
System.out.println(qarl.compareTo(karl));
// positive number myPokedex.addToDex(karl); myPokedex.addToDex(qarl); myPokedex.addToDex(carl); myPokedex.insertionSort();
System.out.println(myPokedex.binarySearch(carl));
// 0
System.out.println(myPokedex.binarySearch(karl));
// 1
System.out.println(myPokedex.binarySearch(qarl));
// 2
Pokedex secondPokedex = new Pokedex();
secondPokedex.addToDex(qarl); secondPokedex.addToDex(carl); secondPokedex.addToDex(karl); secondPokedex.selectionSort();
System.out.println(myPokedex.binarySearch(carl));
// 0
System.out.println(myPokedex.binarySearch(karl));
// 1
System.out.println(myPokedex.binarySearch(qarl));
// 2
System.out.println(myPokedex.equals(secondPokedex));
// true
System.out.println(myPokedex);
/*
1: Carl, the Head TA Pokemon. Its Mixtapes are said to be too hot for the average listener.
2: Karl, the Spooky TA Pokemon. It is said Karl can be found lurking in Klaus during spooktober.
3: Qarl, the Checkstyle TA Pokemon. Qarl likes to remind students to checkstyle their work. */
Feel free to create your own tests in the main method! Try to write tests for the remaining methods in the file!