20.6 Program 3: Pokemon cards
Objectives
- Using classes, inheritance
- Writing getters, setters, and constructors
- Overriding toString()
Description
Using single inheritance, we have a general class of TradingCard which contains string fields for name and image. A two argument constructor initializes the fields and the class has getters (no setters) for both fields. In addition, there is a toString() method that must return the name.
A subclass of TradingCard is GamingCard. This class has a category and game string fields, which are initialized by the constructor only. GamingCard has getters for both fields, no setter methods for this class. (FYI: another type of Trading Card would be a Sports Card, and more specifically, a Baseball trading card. But we aren’t going to write code for those.)
Finally, extending this the GamingCard is a PokemonCard which has fields associated with Pokemon cards. All fields have both getters and setters. Those unfamiliar with the Pokemon game can read this article. Here are examples of Pokemon characters:
Name | Type | Pokedex | Ability |
---|---|---|---|
Charmeleon | Fire | 004 | Blaze |
Beedrill | Bug/Poison | 015 | Swarm or Sniper |
Clefairy | Normal | 035 | Friend Guard |
Hint: The “super” keyword is useful in terms of inheritance.
Instructions
You will submit four files:
- TradingCard.java
- GamingCard.java
- PokemonCard.java
- Program3.java
For the class Program3.java just copy and paste the code in the example below to create the class. This is also a good way to test your code by checking to see if your output is the same as the examples.
Example use in a test program:
public static void main(String[] args) {TradingCard tc = new TradingCard("General", "gen.img");GamingCard gc = new GamingCard("Game Name", "gc.img", "gaming", "Pokemon");PokemonCard charmeleon = new PokemonCard("Charmeleon", "p004.img", "gaming", "Pokemon", "fire", 4, "blaze");PokemonCard beedrill = new PokemonCard("Beedrill", "p015.img", "gaming", "Pokemon", "bug/poison", 15, "swarm or sniper");PokemonCard clefairy = new PokemonCard("Clefairy", "p035.img", "gaming", "Pokemon", "normal", 35, "friend guard");TradingCard[ ] cards = new TradingCard[5];cards[0] = tc;cards[1] = gc;cards[2] = charmeleon;cards[3] = beedrill;cards[4] = clefairy;for ( TradingCard card : cards ) System.out.println(card);}
The output is:
GeneralGame Name: Pokemon characterCharmeleon: Pokemon character, 4, a fire-type with blaze ability.Beedrill: Pokemon character, 15, a bug/poison-type with swarm or sniper ability.Clefairy: Pokemon character, 35, a normal-type with friend guard ability.
Things to consider:
- Each class should be able to be instantiated.
- Each class should have a
toString()
method that overrides its superclass’stoString()
method. - None of the classes uses System.out.println. You must use
toString()
to return a string. A test program can print the string that’s returned from thetoString()
method.