Java III
Home Up Java I Java II Java III Java IV Java V Java VI Java VII Code

 

In the previous lesson, we learned enough about Java to construct an interface that contains the constants necessary to play a game of Color Belot.  The goal of today's lesson is to implement a "Card" class.  The "Card" class provides the scaffolding necessary to work with individual Belot cards.  The code is located in a file named "Card.java".

The first line in the file reads "class Card implements BelotConstants".  The Java keyword "class" indicates that a class is about to be defined, in this case a class named "Card".  The Java keyword "implements" indicates that the class is going to implement an interface, in this case the interface named "BelotConstants".  Since the BelotConstants interface merely defines constants, there is nothing further that class "Card" must do.   By saying that class Card "implements" BelotConstants, all of the constants in this file now become available to class "Card".  In order for the "BelotConstants" interface to be visible to the "Card" class, both files must appear in the same directory.  Would you like to learn more about classes?

Classes are composed of two major components: methods and data fields.  To best understand a class, it is usually a good idea to first look at its data fields.   Since from a stylistic standpoint, data fields should be the last thing in a class, they appear just before the closing right brace of the class.  Data fields are merely data types.  For the "Card" class, there are four data fields: "suit", "rank", "value", and "sortCode".  The "suit" field indicates the suit of the card; the legal values (e.g. "CLUBS") are constants from the "BelotConstants" interface.  The "rank" field indicated the rank of the card; the legal values (e.g. "EIGHTS") are constants from the "BelotConstants" interface.  The "value" field indicates how many points the card is worth in play; the legal values are once again constants from the "BelotConstants" interface.  Finally, "sortCode" is a code used to sort the cards in a player's hand.  We will see how this field is used more specifically in the next lesson.  Notice that by convention, data fields begin with a lowercase letter.  However, if the name of the data field is a multiple word (e.g. "sortCode"), the first letter of all words following the first word are capitalized.

Locate the first method defined in the class, a constructor named "Card".  A constructor must have the same name as the class.  In this case, the constructor takes four arguments, and the values of these arguments will be assigned to the data fields of the class "Card".  This particular class has only one constructor, but in general, multiple constructors can be provided for a class as long as each constructor uses a different set of arguments.  When the constructor is called, a new instance of the class is created and the body of the constructor is executed.  Would you like to learn more about constructors?

Locate the method named "comesAfter".  This particular method is "public", meaning that it can be called by anyone.  The method is a function that must return a "boolean" value.  The method has two parameters: "otherCard" and "trump".  The method can be called by an instance of the "Card" class.  When the method is called, it is passed a value for "otherCard" (another instance of the Card class) and "trump".  The method returns "true" or "false", based on whether the instance of "Card" that calls the method should be displayed to the left or to the right of "otherCard".  Would you like to learn more about methods?

The logic of "comesAfter" is interesting.  In the first test, if the "suit" of the calling instance of the "Card" class is trump and the "suit" of "otherCard" is not trump, then a false is returned since we want to display trump cards to the left of non-trump cards.  (There is no logical need to do this, but it makes it easier for the human player to understand her hand.)  In the second test, if the "suit" of the calling instance "Card" is not trump, but the suit of "otherCard" is trump, then a true is returned.   In the third test, if the two suits are not the same, but the "suit" of the calling instance "Card" is higher (the BelotConstants interface defines clubs to be the lowest, followed by diamonds, followed by hearts, followed by spades) than the "suit" of "otherCard", a "true" is returned.  The fourth test is similar to the third.  With the "else if" construct, if the fifth test is made, the suits of the two cards must be the same.   In the fifth test, if the "rank" of the "otherCard" is an eight, a "false" is returned, since anything else in a suit is higher than an eight.  The sixth test is similar to the fifth.  In the seventh and eighth tests,  the values of the two cards are compared.  Since neither card can be an eight, the values of the two cards are guaranteed to be unique, so the final determination can be made.  Would you like to learn more about conditional statements or boolean operators?

Locate the method named "isHigher".  The method determines which of the two cards wins the trick, given that the instance of the calling "Card" is led and that "otherCard" is played second.  This method does not introduce any new Java constructs, but it is illustrative to look at the method carefully to understand how it determines which card has won the trick.

Locate the method named "print".  This method is a procedure, rather than a function, due to the fact that its return type is "void".  The purpose of this procedure is to print out a readable representation of the card using the System.out.println() procedure.

Locate the method named "updateValue".  This method is also a procedure.  The procedure is called because nines and jacks have different scoring values based on whether they belong to a suit that is trump or non-trump.  Take a careful look at the procedure.  Can you understand its logic?

Locate the four accessor methods named "getValue", "getSuit", "getRank", and "getSortCode".  It is good OOP style to provide accessor methods for each data field in a class.  These methods are "public" functions and merely return the value of a given field.  To be consistent, this tutorial names an accessor method "get" followed by the name of the data field that the method returns (e.g. "getSortCode").