Date:Summary of Chapter 14: Objects of Objects
The chapter applies the basics of Object-Oriented Programming principles as covered in the Think Java book to write design codes that simulates a game played using deck, cards and hands. It describes in details the attributes and objects from the crazy eights classic game.
The visualization, construction and documentation of the instances and inheritance of classes aims to represent decks of cards that discard and shuffle cards through ArrayList collection and CardCollection. Deck and Hand are inherited from the public class CardCollection. When declaring the cards from the ArrayList, the subclass card is declared in angle brackets (<>) like
this.cards = new ArrayList<Card> ();
to specify its source as ArrayList. The constructor views argument as string and assigns it to an instance variable and such as label, adds an element to the collection and initialize cards when the public ArrayList is null. When a collection is empty or there is need to shift card, the constructor can as well move the card using the
public class Card popCard(int i) {
return card.remove();
}
int i = size () 1;
removes a card from a collection it is invoked and shifts it using wrapper classes.
This wrapper methods evaluate storage locations and detect possible relationships and interactions away from those belonging to any class but rather as defined in the primitive data types before shifting the cards.
It is not possible to access elements of an ArrayList using double square brackets ([]) operator. Instead set and get wrappers can be defined and used using dot(.) notation since the variables belong to the same object.
public Card getCard(int i) {
return cards.get(i);
}
As at this point there is CardCollection object from which Deck and Hand are define as new class with the same instance variables and methods.
The inheritance is defined as
public class Deck extends CardCollection {
public Deck (String label) {
Therefore, cardCollection is regarded as the superclass while the Deck is one of the subclasses of CardCollection. With java classes may only extend one superclass and when the superclass is not specified, classes automatically inherit from java.lang.Object. which provides default equals and toStrings. Unlike all other public attributes and methods, special methods that initialize the instance of variables are not inherited. When super (keyword referring to superclass) is used like a method, it invokes the constructor to superclass i.e the CardCollection which initializes the attributes label and cards. The empty Arraylist is then populated with card objects by the Deck constructor. To represent the Hand classes, the same approach for Deck class is used since Hand is a subclass of CardCollection just like Deck. The sample below shows the definition used.
public class Hand extends CardCollection {
public Hand(String label) {
super(label);
}
public void display() {
System.out.println(getLabel() + ": ");
for (int i = 0; i < size(); i++) {
System.out.println(getCard(i));
}
System.out.println();
}
}
To be able to use the design in any related (card) game, related data in an object must be treated as one unit under a process which encapsulates the strategy. To implement the strategy with the player in the crazy eights game, the development assigns two private attributes (a name and a hand). The constructor saved the name as an instance variable with the string data type. Since the name and hand belong to the same object, they are separated with this dot notation (this.) to distinguish the instance variable and parameters defined before method definition.
The definition below has been used to initialize player.
public class Player {
private String name;
private Hand hand;
public Player(String name) {
this.name = name;
this.hand = new Hand(name);
}
Either top-down or bottom-up program development approaches, can be used to code the algorithm for the players. The bottom-up development consolidates lowest elements in array to form the deck to be shuffled while top-down approach is vice versa. Whichever approached used, the play invokes two helper methods: searchForMatch and drawForMatch. The searchForMatch looks in the participants possession for a card that tallies with the earlier played card. The for iteration looks for the first card that should be playing and returns it. If null is return, the search repeats until the card is found under the while loop. The search basically uses the eight cards to draw a card. When card is matched, it returns the card otherwise the card is included in the players hand and draw continues. At the end of the game, each participant is provided with a score and penalty points for the cards left in possession of the player.
The game cannot be complete without Boolean condition that tastes when the game is over and terminate it or shuffle the discard pile.
The templates of objects that have been used to specify what attributes the objects have and what methods can operate them have utilized two relationships: composition (HAS-A) where instances of one classes belong to objects of another class e.g Eights contains reference to two hands and two players and inheritance (IS-A) where one class extends to another class like every instance of Hand is a CardCollection as well.
Â
Request Removal
If you are the original author of this essay and no longer wish to have it published on the thesishelpers.org website, please click below to request its removal:
- Summary of Chapter 14 of the Think Java Book
- Research Proposal on Network Firewall Optimisation
- Research Paper on the Tringl Solution
- Dissertation Example: The Development of an IT Disaster Recovery Plan
- Hackers and Aviation Sector - Research Paper Example
- Evolution of Computerized Accounting Information Systems - Essay Example
- Analysis of Amazon Website - A Research Paper Examplpe