Java for Black Jack

Have you ever wished you could learn a new skill with one reading? Especially a skill that could pay you over $100,000 a year? Wish no longer. With his latest title, Java for Black Jack, famed teacher U.Q. Magnusson will get you going in the dominant programming language of the Internet, Java.

Within this compact, easy-to-read Micro-Manual, you will learn the Java programming language by writing and running a Java-based simulation of the card game Black Jack. U.Q. presents the logic of this simple game as a fully functioning, completely coded Java application. His breezy, easy-to-understand narrative is enhanced with pithy, in-depth discussions. All of his snippets are formatted especially for the E-Book, and many are annotated with embedded explanations. A comprehensive glossary and complete source code round out the experience. He even includes a feature not often found in programming texts, explicit instructions on how to compile and run your own Java program. And he does all of this in less than 60 pages!

There is no bull in Java for Black Jack. From the very first words, you will absorb the principles of Java, including classes, objects, methods, attributes, inheritance, exceptions, and expressions. Because you will be having fun while you do so, you will retain more of this knowledge. And when you are finished with Java for Black Jack, you will know all that you need.

———————————————€€€€€€€€€€€——————————————

Features

Java for Black Jack has five separate sections, each detailing Java from a very different, but equally important perspective.

1. Black Jack Simulation in Java: This is a complete Java-based simulation of the casino card game. All of the examples are formatted especially for the E-Book:

2. Basics of Java Programming: These are the typical passages which you will see over and over again. Many of them include embedded explanations:

3. The Java Development Kit: U.Q. will give you directions to download the JDK, install it, and use it to run your Java programs. Command-line examples are explicit and precise:

4. Java Jargon: U.Q. includes a glossary of buzzwords corresponding to the italicized words in this book.

5. Code for Black Jack: This is the complete source code for our program.

Believe it. Java for Black Jack is your quickest route to Java competency.

———————————————€€€€€€€€€€€——————————————

Available From

———————————————€€€€€€€€€€€——————————————

Sample Chapter

Swipe the page from right to left.

PLAYER

Before the round begins, each player is dealt two cards from the deck to start his hand.

On his subsequent play, he may either request more cards from the deck, called a “hit”, or stick with his current hand, called a “stay”.

Without his hand, the Player could not play – ergo, the Player would not exist. Fittingly, the Player’s most important member variables are his unique identifier, his hand, and a reference to the game deck from which he populates his hand.

The Player’s constructor method is responsible for initializing these member variables. It passes in a String for his uid_ identifier and his reference to the game’s Deck. Then it creates an empty hand as a Stack of Cards, and starts it with two Card objects dealt from the top of that game Deck.

Once a Player has been instantiated and initialized, his only purpose is to play. Just like real-life, each Player plays his entire game in one loop. With each iteration of this loop, he assesses his current hand, and decides to either Hit (pull another card from the deck) or Stay (stick with his current hand).

The next block of code represents this central decision as a switch statement between his potential Call choices. A switch is like an extended if-then choice, except that it is based upon the value of one variable. In this case, that variable is the player’s Call, represented as an enum. If the player hits the keyboard character ‘H’ (upper or lower case), then he is making a Hit. Otherwise, he is making a Stay.

Note the variable c above. In the ASCII world, that one-character keyboard datum c would be an integer retrieved with a system call. However, this simple logic requires more code than you would expect. This is because it requires you to handle the exception.

What is an exception? An exception is a condition which steps outside the normal boundaries of the environment. In other words, it is exceptional. A better term for this, IMHO, would be “error”. Truth is, the Exception class is Java’s way of letting you recover gracefully from your own boneheaded mistakes.

By contrast, other programming languages let you proceed willy-nilly with no regard to your personal safety. You can divide by zero, read the 11th element of a 10-element array, allocate all the memory on your network, and even flush your entire masterpiece into /dev/null. There are literally no safeguards against your carelessness.

Java is different. Because it assumes your program will be running on all sorts of unknown computers scattered all over the world, it imposes its own safeguards. The primary safeguard is the Exception class. It is its own discrete class in the Java language, and its subclasses will pop up again and again. In fact, you can define your own Exception, so long as you subclass it properly from the base class Exception.

When an exceptional condition is met in run-time, the computer will throw an Exception object. You must program to catch this object. You can create as elaborate a catch statement as you wish, coding for every type of Exception to be thrown, even including a finally block to execute at the end. However, you have to know which classes and methods will throw which Exceptions, and you have to wrap all these potentially lethal calls in a try block.

In the snippet below, the code to retrieve one character from the keyboard will be “System.in.read()”. It is called twice, once for the character key, and again for the Enter key. If there is a problem with either, it will throw an IOException object which you have to catch.

Note how the try block is wrapped inside the while loop. (More on while loops later.) Because it tests at the top for a true condition, it replays itself forever. If the System.in.read() method throws an IOException, then the “continue” statement in the catch block takes you back to the top. If not, then the “break” statement gets you out. Bottom line: you keep trying until it works.

To summarize, the makeCall() method uses a System.in.read() method to read a keystroke from the keyboard, which causes the method to return either a Hit or Stay, which sends the flow of control back to the beginning or out to the end.

The flowchart below demonstrates this. As you study it, make note: This encapsulates the Player’s only purpose in the span of this program.

———————————————€€€€€€€€€€€——————————————

 
Java | SQL | C | HTML | Blog | Download | Contact Us
Copyright © 2012 UberQueue LLC.