Getting Started with Java

1. Java is a language and a platform. The language is partly patterned after the C and C++ languages to shorten the learning curve for C/C++ developers. The platform consists of a virtual machine and associated execution environment.

2. A virtual machine is a software-based processor that presents its own instruction set.

3. The purpose of the Java compiler is to translate source code into instructions (and associated data) that are executed by the virtual machine.

4. The answer is true: a classfile's instructions are commonly referred to as bytecode.

5. When the virtual machine's interpreter learns that a sequence of bytecode instructions is being executed repeatedly, it informs the virtual machine's Just In Time (JIT) compiler to compile these instructions into native code.

6. The Java platform promotes portability by providing an abstraction over the underlying platform. As a result, the same bytecode runs unchanged on Windows-based, Linux-based, Mac OS X-based, and other platforms.

7. The Java platform promotes security by providing a secure environment in which code executes. It accomplishes this task in part by using a bytecode verifier to make sure that the classfile's bytecode is valid.

8. The answer is false: Java SE is the platform for developing applications and applets.

9. The JRE implements the Java SE platform and makes it possible to run Java programs.

10. The difference between the public and private JREs is that the public JRE exists apart from the JDK, whereas the private JRE is a component of the JDK that makes it possible to run Java programs independently of whether or not the public JRE is installed.

11. The JDK provides development tools (including a compiler) for developing Java programs. It also provides a private JRE for running these programs.

12. The JDK's javac tool is used to compile Java source code.

13. The JDK's java tool is used to run Java applications.

14. The purpose of the JDK's jar tool is to create new JAR files, update existing JAR files, and extract files from existing JAR files.

15. Standard I/O is a mechanism consisting of Standard Input, Standard Output, and Standard Error that makes it possible to read text from different sources (keyboard or file), write nonerror text to different destinations (screen or file), and write error text to different definitions (screen or file).

16. An IDE is a development framework consisting of a project manager for managing a project's files, a text editor for entering and editing source code, a debugger for locating bugs, and other features.

17. Two popular IDEs are NetBeans and Eclipse.

18. Pseudocode is a compact and informal high-level description of the problem domain.

19. You would use the jar tool along with its t (table of contents) and f (JAR file's name) options to list FourOfAKind.jar's table of contents; for example, jar tf FourOfAKind.jar.

20. Listing 1 presents the FourOfAKind application's refactored FourOfAKind class that was called for in Chapter 1.

Listing 1. Letting the human player pick up the top card from the discard pile or the deck

* <code>FourOfAKind</code> implements a card game that is played between two

* players: one human player and the computer. You play this game with a

* standard 52-card deck and attempt to beat the computer by being the first

* player to put down four cards that have the same rank (four aces, for

* @author Jeff Friesen

public class FourOfAKind {

* Human player

final static int HUMAN = 0;

* Computer player

final static int COMPUTER = 1;

* Application entry point.

* @param args array of command-line arguments passed to this method

public static void main(String[] args) {

System.out.println("Welcome to Four of a Kind!"); Deck deck = new Deck(); // Deck automatically shuffled DiscardPile discardPile = new DiscardPile(); Card hCard; Card cCard;

hCard =; cCard =; if (hCard.rank() != cCard.rank())

break; deck.putBack(hCard); deck.putBack(cCard);

deck.shuffle(); // prevent pathological case where every successive // pair of cards have the same rank int curPlayer = HUMAN;

if (cCard.rank().ordinal() > hCard.rank().ordinal())

curPlayer = COMPUTER; deck.putBack(hCard); hCard = null; deck.putBack(cCard); cCard = null;

Card[] hCards = new Card[4]; Card[] cCards = new Card[4]; if (curPlayer == HUMAN)

hCards[i] cCards[i]

if (curPlayer {

showHeldCards(hCards); if (discardPile.topCard() != null)


System.out.println("Discard pile top card: " +

discardPile.topCard()); System.out.println();

choice = prompt("Which card do you want to throw away (A, "C, D)? ");

switch (choice) {




choice =





choice =





choice =





choice =


dest = prompt("Pick up top card from deck or discard pile " + "(D, P)? ");

card = (dest == 'D') ? : discardPile.getTopCard();

discardPile.setTopCard(hCards[choice-'A']); hCards[choice-'A'] = card; card = null;

if (isFourOfAKind(hCards)) {


System.out.println("Human wins!");


putDown("Human's cards:", hCards);


putDown("Computer's cards:", cCards);

return; // Exit application by returning from main()

curPlayer = COMPUTER;

int choice = leastDesirableCard(cCards); discardPile.setTopCard(cCards[choice]); cCards[choice] =;

if (isFourOfAKind(cCards)) {


System.out.println("Computer wins!");


putDown("Computer's cards:", cCards);

return; // Exit application by returning from main()

curPlayer = HUMAN;

while (discardPile.topCard() != null)

deck.putBack(discardPile.getTopCard()); deck.shuffle();

* Determine if the <code>Card</code> objects passed to this method all

* have the same rank.

* @param cards array of <code>Card</code> objects passed to this method

* @return true if all <code>Card</code> objects have the same rank;

* otherwise, false

static boolean isFourOfAKind(Card[] cards) {

for (int i = 1; i < cards.length; i++) if (cards[i].rank() != cards[0].rank()) return false; return true;

* Identify one of the <code>Card</code> objects that is passed to this

* method as the least desirable <code>Card</code> object to hold onto.

* @param cards array of <code>Card</code> objects passed to this method

* @return 0-based rank (ace is 0, king is 13) of least desirable card

static int leastDesirableCard(Card[] cards) {

rankCounts[cards[i].rank().ordinal()]++; int minCount = Integer.MAX_VALUE; int minIndex = -1;

if (rankCounts[i] < minCount && rankCounts[i] != 0) {

minCount = rankCounts[i]; minIndex = i;

Prompt the human player to enter a character. @param msg message to be displayed to human player return i;

return 0; // Needed to satisfy compiler (should never be executed)

* @return integer value of character entered by user.

static int prompt(String msg) {


// Erase all subsequent characters including terminating \n newline // so that they do not affect a subsequent call to prompt(). while ( != '\n'); return ch;

catch ( ioe) {

return 0;

* Display a message followed by all cards held by player. This output

* simulates putting down held cards.

* @param msg message to be displayed to human player

* @param cards array of <code>Card</code> objects to be identified

static void putDown(String msg, Card[] cards) {

System.out.println(msg); for (int i = 0; i < cards.length; i++) System.out.println(cards[i]);

* Identify the cards being held via their <code>Card</code> objects on

* separate lines. Prefix each line with an uppercase letter starting with

* @param cards array of <code>Card</code> objects to be identified

static void showHeldCards(Card[] cards) {

System.out.println(); System.out.println("Held cards:"); for (int i = 0; i < cards.length; i++) if (cards[i] != null)

System.out.println((char) ('A'+i) + System.out.println();

Was this article helpful?

0 0

Post a comment