Object oriented programs divide major responsibilities into classes. Each class is responsible for 1 part, or a set of related parts, of the overall program.

A program’s user interface is part of the overall program. It can be written using 1 or more classes with instance methods and instance variables, just like the rest of the program.

Blackjack UI

Consider the user interface for a simplified version of Blackjack. The mechanics of the game are already implemented in a Blackjack class. The Blackjack class is a client of (uses) other classes such as Card and Hand.

Example program run

Money: $1,000.00

Bet: $100

Dealer: [10C]
Player: [10D, 8H]
Hit? (y/n): n

Dealer: [10C, 7S]
Player: [10D, 8H]
Result: win

Money: $1,100.00
Play again (y/n): n

This example shows the output in US dollars. The code below uses the computer’s settings to determine the currency.

BlackjackUI class

The BlackjackUI class is responsible for handling interactions with the user. This includes:

The Class writing order should be followed, as with any other class.

Public method headers

/**
 * Constructs a blackjack game with $1,000 in player bankroll
 */
public BlackjackUI()
{ /* to be implemented */ }

/**
 * Plays a single hand of blackjack
 */
public void playHand()
{ /* to be implemented */ }

/**
 * Plays blackjack hands until the user chooses to quit
 */
public void playHandsUntilQuit()
{ /* to be implemented */ }

The public methods of BlackjackUI correspond to the functionality it offers to client classes (classes that use the BlackjackUI class).

Instance variables

private Blackjack bj; 
private Scanner fromKeyboard;
private NumberFormat nf;

The instance variables of BlackjackUI exist to support its public methods.

Implementing Blackjack is the responsiblity of the Blackjack class. BlackjackUI stores a reference to an object of type Blackjack.

BlackjackUI stores a reference to a Scanner object to accept input from the user.

BlackjackUI stores a reference to NumberFormat object to produce formatted output of currency.

Selected implementation code

public void playHand()
{
    bj.placeInitialBetAndDealCards(getValidBet());
    printHands();

    playPlayersHand();
    bj.playDealersHand();
    printHands();

    displayResult();
    bj.resolveBetsAndReset();
}

The playHand method calls a combination of Blackjack and BlackjackUI methods. The Blackjack methods are prefixed by bj. because they are run on the object of type Blackjack. The BlackjackUI methods have no prefix because they are run on the implicit parameter (the same BlackjackUI object on which playHand was run).

It is possible to prefix each call to a BlackjackUI instance method with this.. Prefixing the method calls with this. would not change the behavior.

The BlackjackUI methods are private because they are intended for use only by other methods of BlackjackUI. Implementations of these methods can be found in the Java files near the bottom of this page.

public void playHandsUntilQuit()
{
    System.out.println("Money: " + nf.format(bj.getPlayersMoney()));
    
    boolean playAgain = true;
    
    while(playAgain)
    {
        playHand();

        System.out.println("\nMoney: " + nf.format(bj.getPlayersMoney()));
        
        if(bj.getPlayersMoney() > 0)
        {
            System.out.print("Play again (y/n): ");
            playAgain = fromKeyboard.next().toLowerCase().startsWith("y");
        }
        else
            playAgain = false;
    }
}

The playHandsUntilQuit method calls the playHand method to handle playing a single hand. playHandsUntilQuit uses the NumberFormat object to produce formatted output of currency.

Completed code

The Blackjack class is a stub. The implementation is designed to facilitate demonstration of the UI. The implementation code does not match, or even resemble, what would be used for an actual Blackjack game. All method documentation has been removed to avoid confusion about what each method would do if actually implemented, vs what each method does in the stub.

If you are interested in a complete implementation of Blackjack, feel free to reach out. Blackjack might eventually be posted as a lab on this site. The project includes:

Comments

Comment on Object oriented UI