RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

lect09b
Fields vs locals

We'll look at the same problem two ways; one of the solutions will involve helper methods.

Exercise: add getters to

 1  /** A solution to the Card problem.
 2   * @see http://www.radford.edu/itec120/2009spring-ibarland/Homeworks/hw05.html
 3   * @author Ian Barland
 4   * @version 2009.Mar.19
 5   */
 6  public class Card
 7  {
 8      private String rank;  // "Queen" or "4", e.g.
 9      private String suit;  // "spades" or "diamonds", e.g.
10      
11      
12      /** Card constructor.
13       * @param shorthand A string of length two or three.  E.g. "4D", "QH", "10S".
14       */
15      public Card( String shorthand )
16      {
17          // First, initialize the field 'rank'.
18          char rankChar = shorthand.charAt(0);
19          if (rankChar == 'A') 
20          {
21              this.rank = "Ace";
22          }
23          else if (rankChar == 'K') 
24          {
25              this.rank = "King";
26          }
27          else if (rankChar == 'Q') 
28          {
29              this.rank = "Queen";
30          }
31          else if (rankChar == 'J') 
32          {
33              this.rank = "Jack";
34          }
35          else if (rankChar == '1') 
36          {
37              this.rank = "10";
38          }
39          else 
40          {
41              this.rank = Character.toString(rankChar);
42          }
43      
44          // Now, initialize the field `suit`.
45          // Note that `shorthand` might have two characters *or* three
46          // eg "10D". 
47          char suitChar = shorthand.charAt( shorthand.length()-1 );
48          if (suitChar == 'S') 
49          {
50              this.suit = "spade";
51          }
52          else if (suitChar == 'H') 
53          {
54              this.suit = "heart";
55          }
56          else if (suitChar == 'D') 
57          {
58              this.suit = "diamond";
59          }
60          else if (suitChar == 'C') 
61          {
62              this.suit = "club";
63          }
64          else 
65          {
66              // Defensive programming:
67              // We don't need to do any of this, but it might save
68              // us significant headache.
69              System.err.println( "Uh-oh -- Card constructor: unrecognized suit??" );
70              this.suit = "Card constructor: unrecognized suit??";
71          }
72              
73     }  // End of Card constructor.
74      
75         
76      /** Return an English description of this card.
77       * @return an English description of this card,
78       *  perhaps "4 of diamonds" or "Queen of hearts".
79       */
80      public String getDescription() 
81      {
82          return this.rank + " of " + this.suit + "s";
83      }
84  
85  }

Alternate method: Rather than do all the work in the constructor, we'll just have the constructor store the shorthand description, and we'll have the getter methods getRankName and getSuitName do the work. Note how they will have local variables.

In either of these two approaches, once we have getter methods, how do we best write getDescription? We use those getters as helper methods!

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Mar.26 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme