09. “Card” completion

Okay, let’s finish off the “Card” class. We’ll start by creating the correct code for our “printCardVal” method. Replace whatever is already in┬áthere with the following:

if ((cardRank >= 2) && (cardRank <= 14)) {
  return cardName + " of " + suitName + "s";
} else if (cardRank > 14) {
  return suitColour + " Joker";
} else {
  return "Card does not exist";
}

There’s nothing too complicated about this code, you can see we’re checking the cardRank variable to separate regular cards from Jokers and non-existent┬ácards. We’re then returning a String that represents the way you would say the card’s name (“10 of Hearts” for instance.)

Once you’ve done that, your Card class is just about complete, but no programmer should be content with their code until it’s properly formatted!

Indenting
In order to indent our code and keep it consistent you’ll be glad to know that Eclipse provides an automatic indenting function. Simply right-click anywhere in your code and select:

Source -> Format

Commenting
The next important step is to comment your code. Since you may be new to Java I suggest commenting this class very thoroughly, making sure you understand every single line completely. Even if that takes some serious time I can assure you it’ll be of benefit as we plough on through this project. If you’re finding anything particularly hard to understand then feel free to ask questions below.

Javadoc
For those with a really keen interest in Java and becoming proper Java programmers you should probably learn about Javadoc. Javadoc is a way of generating documentation for your classes directly from the comments you add. This is a very useful feature if you intend on building reusable classes and projects that have documentation associated with them. Some day I may create a tutorial for this but for now you’ll have to look it up yourself.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>