Polymorphism
Polymorphism
Monster
MuppetMonster
VampireBat
Test Harness
Classwork
In a role-playing game there will be various items for players to collect and use. It is up to you whether you are working on items for a fantasy,
piracy, crime fighting, or other sort of game.
Part A
Write a class GameItem, representing an object in a role playing game that
players may encounter. Every GameItem has a
String name, an int value, and a boolean playerHas. (In this case, value could
be a negative, indicating that it is worse than useless.)
Add methods pickup(), use(), and putdown(). Each checks playerHas to see if it
can act successfully -- we can only pick up things we don't have, and
only use or put down things we do have. Each should print a message
like "You have picked up the bag of magic beans" (using the name) if
they are successful and a different message explaining failure
otherwise. If pickup is successful, playerHas becomes true, if
putdown is successful, playerHas becomes false.
Also write an equals method for this class. GameItems can only be equal to other
GameItems, and only if they have the same name and value (but playerHas doesn't
matter).
Don't forget to use .equals and == appropriately.
Part B
Write two classes that inherit from GameItem (things in a game
that someone might pick up and use - be as wacky as you like).
To at least one of these, add a new instance variable appropriate to the class.
In each class, override at least one of pickup() and use() and
putdown(). Print an appropriate message in each and set playerHas
to true or
false to reflect if we have picked up / put down the
item. Always check playerHas first to see if it is possible at
all, but if there is another instance var, success should rely on that
as well.
For instance, a BagOfGold class might have its use() check the value,
and if the value is 0, report that we threw away the empty bag (so
playerHas became false), and otherwise use() might report that we spent
some gold and decrease the value.
Or a Rock class might have a variable weight, and pickup() might only set
playerHas to true if it is light enough -- if it is too heavy, we can't pick it up.
You could also have the other subclass make different choices about
picking up/putting down, for instance a SuperStickyGlueLump might have putdown never set playerHas to false, and
print a message saying it is stuck to the player forever.Add equals methods for
each of the other classes, which require for equality
that the other object be of the subclass type (not just GameItems).
Check that the equals you are overriding says they're the same (to
handle the inherited instance vars) and
check any added instance variable as well.
Part C
In a separate class, in a main method, create an array of GameItems allItems, which should have at least
5 items. Make sure you have at least one for each of your classes.
Also make one extra GameItem questItem outside of the array. Set up questItem
so that it is equals to exactly one of
the items in the array and has playerHas false.
(Not another pointer to the same
object that is in the array, a different
object but with instance vars set so that your equals method will return true!)
Loop through
the array and call pickup and use for each item, and then call putdown
unless the item is equals to questItem.
At the end, print both the questItem we used to compare and the one
equals to the questItem
that we found in
the array. (for the first, playerHas should be false, for the
second, it should be true)
[EC] Also keep (don't put down) an item if it is the most
valuable item we have seen so far.
(It is possible for the questItem to be the most valuable item.)
Also print the final most valuable item at the end.
[EC] Each time we find a new most valuable item, go back and put down
the previous most valuable item (but we would never put down the
questItem).