Sun Microsystems

Wake Tech

Your Guide to Java

Samples
Syllabus
Assignments
Instructors
Introduction
Samples Index
 

CSC 148 Java Programming
Sample Solutions
Chapter Eight

// Exercise 8.3 Solution

// Poker.java

// Program deals a Poker "hand"

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class Poker extends Applet

implements ActionListener {

private Card deck[], hand[];

private int currentCard;

private Button dealButton, shuffleButton;

private TextArea displayCard;

private String faces[], suits[], output;

private int numbers[];

public void init()

{

String f[] = { "Ace", "Deuce", "Three", "Four",

"Five", "Six", "Seven", "Eight",

"Nine", "Ten", "Jack", "Queen",

"King" };

String s[] = { "Hearts", "Diamonds",

"Clubs", "Spades" };

numbers = new int [ 13 ];

faces = f;

suits = s;

hand = new Card[ 5 ];

deck = new Card[ 52 ];

currentCard = -1;

for ( int i = 0; i < deck.length; i++ )

deck[ i ] = new Card( faces[ i % 13 ],

suits[ i / 13 ] );

dealButton = new Button( "Deal hand" );

shuffleButton = new Button( "Shuffle deck" );

dealButton.addActionListener( this );

shuffleButton.addActionListener( this );

displayCard = new TextArea( 7, 20 );

displayCard.setEditable( false );

add( dealButton );

add( shuffleButton);

add( displayCard );

}

public void actionPerformed( ActionEvent event )

{

if ( event.getSource() == dealButton ) {

displayCard.setText( "" ); // clear text area

showStatus( "" ); // clear status bar

output = "";

for ( int n = 0; n < hand.length; n++ ) {

Card temp = dealCard();

if ( temp != null ) {

hand[ n ] = temp;

displayCard.append( hand[ n ].toString() + "\n" );

}

}

totalHand(); // calculates contents of the hand

pair();

threeOfAKind();

fourOfAKind();

straight();

flush();

}

else if ( event.getSource() == shuffleButton ) {

shuffle();

displayCard.setText( "DECK IS SHUFFLED\n" );

}

}

public void shuffle()

{

currentCard = -1;

for ( int i = 0; i < deck.length; i++ ) {

int j = ( int ) ( Math.random() * 52 );

Card temp = deck[ i ];

deck[ i ] = deck[ j ];

deck[ j ] = temp;

}

dealButton.setEnabled( true );

}

public Card dealCard()

{

if ( currentCard > 50 ) {

dealButton.setEnabled( false );

showStatus( "" ); // clear for last two cards

return null;

}

else if ( ++currentCard < deck.length )

return deck[ currentCard ];

return null; // not functional only for compiler

}

private void totalHand()

{

for ( int x = 0; x < faces.length; x++ )

numbers[ x ] = 0;

for ( int h = 0; h < hand.length; h++ )

for ( int f = 0; f < faces.length; f++ )

if ( hand[ h ].getFace().equals( faces[ f ] ) )

++numbers[ f ];

}

public void pair()

{

for ( int k = 0; k < faces.length; k++ )

if ( numbers[ k ] == 2 )

output += ( "Pair of " + faces[ k ] + "'s " );

showStatus( output );

}

public void threeOfAKind()

{

for ( int k = 0; k < faces.length; k++ )

if ( numbers[ k ] == 3 ) {

output += ( "Three " + faces[ k ] + "'s" );

break;

}

showStatus( output );

}

public void fourOfAKind()

{

for ( int k = 0; k < faces.length; k++ )

if ( numbers[ k ] == 4 )

output += ( "Four " + faces[ k ] + "'s" );

showStatus( output );

}

public void flush()

{

String theSuit = hand[ 0 ].getSuit();

for ( int s = 1; s < hand.length; s++ )

if ( hand[ s ].getSuit().compareTo( theSuit ) != 0 )

return; // not a flush

output += ( "flush in " + theSuit );

showStatus( output );

}

public void straight()

{

int locations[] = new int[ 5 ], z = 0;

for ( int y = 0; y < numbers.length; y++ )

if ( numbers[ y ] == 1 )

locations[ z++ ] = y;

bubbleSort( locations );

int faceValue = locations[ 0 ];

for ( int m = 1; m < locations.length; m++ ) {

if ( faceValue != locations[ m ] - 1 )

return; // not a straight

else

faceValue = locations[ m ];

}

output += "straight ";

showStatus( output );

}

private void bubbleSort( int values[] )

{

for ( int pass = 1; pass < values.length; pass++ )

for ( int comp = 0; comp < values.length - 1; comp++ )

if ( values[ comp ] > values[ comp + 1 ] ) {

int temp = values[ comp ];

values[ comp ] = values[ comp + 1 ];

values[ comp + 1 ] = values[ comp ];

}

}

}

class Card {

private String face;

private String suit;

public Card( String f, String s )

{

face = f;

suit = s;

}

public String getFace() { return face; }

public String getSuit() { return suit; }

public String toString() { return face + " of " + suit; }

}

 

 

// Exercise 8.4 Solution

// Poker2.java

// Program deals two Poker "hands"

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class Poker2 extends Applet

implements ActionListener {

private Card deck[], hand[], hand2[];

private int currentCard;

private Button dealButton, shuffleButton;

private TextArea displayCard, displayCard2;

private String faces[], suits[], output;

private int numbers[], numbers2[];

private int highValue, highValue2, group, group2;

private boolean straightHand, straightHand2;

private final int ONEPAIR = 2;

private final int TWOPAIR = 4;

private final int THREEKIND = 6;

private final int STRAIGHT = 8;

private final int FLUSH = 10;

private final int FOURKIND = 12;

private final int STRAIGHTFLUSH = 14;

public void init()

{

String f[] = { "Ace", "Deuce", "Three", "Four",

"Five", "Six", "Seven", "Eight",

"Nine", "Ten", "Jack", "Queen",

"King" };

String s[] = { "Hearts", "Diamonds",

"Clubs", "Spades" };

faces = f;

suits = s;

numbers = new int[ 13 ];

numbers2 = new int[ 13 ];

hand = new Card[ 5 ];

hand2 = new Card[ 5 ];

deck = new Card[ 52 ];

currentCard = -1;

for ( int i = 0; i < deck.length; i++ )

deck[ i ] = new Card( faces[ i % 13 ],

suits[ i / 13 ] );

dealButton = new Button( "Deal hand" );

shuffleButton = new Button( "Shuffle deck" );

dealButton.addActionListener( this );

shuffleButton.addActionListener( this );

displayCard = new TextArea( 8, 20 );

displayCard2 = new TextArea( 8, 20 );

displayCard.setEditable( false );

add( dealButton );

add( shuffleButton);

add( displayCard );

add( displayCard2 );

}

public void actionPerformed( ActionEvent e )

{

if ( e.getSource() == dealButton ) {

int h1 = 0, h2 = 0;

group = group2 = 0;

highValue = highValue2 = 0;

displayCard.setText( "" ); // clear text area

displayCard2.setText( "" );

showStatus( "" ); // clear status bar

straightHand = straightHand2 = false;

for ( int n = 1; n <= 10; n++ ) {

Card temp = dealCard();

if ( temp != null ) {

if ( n % 2 == 0 ) {

hand[ h1 ] = temp;

displayCard.append( hand[ h1 ] + "\n" );

++h1;

}

else {

hand2[ h2 ] = temp;

displayCard2.append( hand2[ h2 ] + "\n" );

++h2;

}

}

}

totalHand(); // calculates contents of the hand

pair();

threeOfAKind();

fourOfAKind();

straight();

flush();

if ( group > group2 )

showStatus( "top hand is better" );

else if ( group < group2 )

showStatus( "bottom hand is better" );

else {

// test for the highest card

// this solution does not test for

// the next highest card. You may

// want to add this capability.

if ( highValue > highValue2 )

showStatus( "top hand is better" );

else if ( highValue < highValue2 )

showStatus( "bottom hand is better" );

else

showStatus( "they are equal" );

}

}

else if ( e.getSource() == shuffleButton ) {

shuffle();

displayCard.setText( "DECK IS SHUFFLED\n" );

displayCard2.setText( "" );

}

}

public void shuffle()

{

currentCard = -1;

for ( int i = 0; i < deck.length; i++ ) {

int j = ( int ) ( Math.random() * 52 );

Card temp = deck[ i ];

deck[ i ] = deck[ j ];

deck[ j ] = temp;

}

dealButton.setEnabled( true );

}

public Card dealCard()

{

if ( currentCard > 50 ) {

dealButton.setEnabled( false );

showStatus( "" );

return null;

}

else if ( ++currentCard < deck.length )

return deck[ currentCard ];

return null; // not used logically; compiler only

}

private void totalHand()

{

for ( int x = 0; x < faces.length; x++ )

numbers[ x ] = numbers2[ x ] = 0;

for ( int h = 0; h < hand.length; h++ )

for ( int f = 0; f < faces.length; f++ ) {

if ( hand[ h ].getFace().equals( faces[ f ] ) ) {

++numbers[ f ];

if ( f == 0 )

highValue = 15; // special value for Ace

if ( f > highValue )

highValue = f;

}

if ( hand2[ h ].getFace().equals( faces[ f ] ) ) {

++numbers2[ f ];

if ( f == 0 )

highValue2 = 15; // special value for Ace

if ( f > highValue2 )

highValue2 = f;

}

}

}

public void pair()

{

int c = 0, c2 = 0, high = 0, high2 = 0;

for ( int k = 0; k < faces.length; k++ ) {

if ( numbers[ k ] == 2 ) {

if ( k == 0 )

high = 15; // special value for ace

if ( high < k )

high = k;

c++;

}

if ( numbers2[ k ] == 2 ) {

if ( k == 0 )

high2 = 15; // special value for ace

if ( high2 < k )

high2 = k;

c2++;

}

}

if ( c != 0 && c2 != 0 && c == c2 ) {

if ( c == 1 && c2 == 1 ) {

group = ONEPAIR;

group2 = ONEPAIR;

}

else { // two pairs

group = TWOPAIR;

group2 = TWOPAIR;

}

if ( high > high2 )

group++;

else if ( high < high2 )

group2++;

}

else if ( c > c2 && c == 1 )

group = ONEPAIR;

else if ( c > c2 && c == 2 ) {

group = TWOPAIR;

if ( c2 == 1 )

group2 = ONEPAIR;

}

else if ( c < c2 && c2 == 1 )

group2 = ONEPAIR;

else if ( c < c2 && c2 == 2 ) {

group2 = TWOPAIR;

if ( c == 1 )

group = ONEPAIR;

}

}

public void threeOfAKind()

{

int high = 0, high2 = 0;

boolean flag = false, flag2 = false;

for ( int k = 0; k < faces.length; k++ ) {

if ( numbers[ k ] == 3 ) {

group = THREEKIND;

flag = true;

if ( k == 0 )

high = 15; // special value for ace

if ( k > high )

high = k;

}

if ( numbers2[ k ] == 3 ) {

group2 = THREEKIND;

flag2 = true;

if ( k == 0 )

high2 = 15; // special value for ace

if ( k > high2 )

high2 = k;

}

}

if ( flag == true && flag2 == true )

if ( high > high2 )

group++;

else if ( high < high2 )

group2++;

}

public void fourOfAKind()

{

int high = 0, high2 = 0;

boolean flag = false, flag2 = false;

for ( int k = 0; k < faces.length; k++ ) {

if ( numbers[ k ] == 4 ) {

group = FOURKIND;

flag = true;

if ( k == 0 )

high = 15; // special value for ace

if ( k > high )

high = k;

}

if ( numbers2[ k ] == 4 ) {

group2 = FOURKIND;

flag2 = true;

if ( k == 0 )

high2 = 15; // special value for ace

if ( k > high2 )

high2 = k;

}

}

if ( flag == true && flag2 == true )

if ( high > high2 )

group++;

else if ( high < high2 )

group2++;

}

public void flush()

{

String theSuit = hand[ 0 ].getSuit();

boolean flag1 = true, flag2 = true;

int high = 0, high2 = 0;

for ( int s = 1; s < hand.length && flag1 == true; s++ )

if ( hand[ s ].getSuit().compareTo( theSuit ) != 0 )

flag1 = false; // not a flush

for ( int s = 1; s < hand.length && flag2 == true; s++ )

if ( hand2[ s ].getSuit().compareTo( theSuit ) != 0 )

flag2 = false; // not a flush

if ( flag1 == true ) {

group = FLUSH;

if ( straightHand == true )

group = STRAIGHTFLUSH;

}

if ( flag2 == true ) {

group2 = FLUSH;

if ( straightHand2 == true )

group2 = STRAIGHTFLUSH;

}

}

public void straight()

{

int locations[] = new int[ 5 ],

locations2[] = new int[ 5 ], z = 0, z2 = 0;

for ( int y = 0; y < numbers.length; y++ ) {

if ( numbers[ y ] == 1 )

locations[ z++ ] = y;

if ( numbers[ y ] == 1 )

locations2[ z2++ ] = y;

}

bubbleSort( locations );

bubbleSort( locations2 );

int faceValue = locations[ 0 ];

boolean flag1 = true, flag2 = true;

for ( int m = 1; m < locations.length && flag1 == true; m++ ) {

if ( faceValue != locations[ m ] - 1 )

flag1 = false; // not a straight

else

faceValue = locations[ m ];

}

faceValue = locations2[ 0 ];

for ( int m = 1; m < locations.length && flag2 == true; m++ ) {

if ( faceValue != locations2[ m ] - 1 )

flag2 = false; // not a straight

else

faceValue = locations2[ m ];

}

if ( flag1 == true ) {

straightHand = true;

group = STRAIGHT;

}

if ( flag2 == true ) {

straightHand2 = true;

group2 = STRAIGHT;

}

}

private void bubbleSort( int values[] )

{

for ( int pass = 1; pass < values.length; pass++ )

for ( int comp = 0; comp < values.length - 1; comp++ )

if ( values[ comp ] > values[ comp + 1 ] ) {

int temp = values[ comp ];

values[ comp ] = values[ comp + 1 ];

values[ comp + 1 ] = values[ comp ];

}

}

}

class Card {

private String face;

private String suit;

public Card( String f, String s )

{

face = f;

suit = s;

}

public String getFace() { return face; }

public String getSuit() { return suit; }

public String toString() { return face + " of " + suit; }

}

 

 

// Exercise 8.7 Solution

// CompareStrings.java

// Program compares two strings

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class CompareStrings extends Applet

implements ActionListener {

private Label prompt1, prompt2;

private TextField input1, input2;

public void init()

{

prompt1 = new Label( "Enter first string:" );

prompt2 = new Label( "Enter second string:" );

input1 = new TextField( 20 );

input2 = new TextField( 20 );

input2.addActionListener( this );

add( prompt1 );

add( input1 );

add( prompt2 );

add( input2 );

}

public void actionPerformed( ActionEvent e )

{

String first = input1.getText();

String second = input2.getText();

int value = first.compareTo( second );

if ( value == 0 )

showStatus( first + " == " + second );

else if ( value > 0 )

showStatus( first + " > " + second );

else

showStatus( first + " < " + second );

}

}

 

 

// Exercise 8.8 Solution

// Compare.java

// Program compares two strings

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class Compare extends Applet

implements ActionListener {

private Label prompt1, prompt2,

promptNum, promptIndex;

private TextField input1, input2,

inputNum, inputIndex;

public void init()

{

prompt1 = new Label( "Enter first string:" );

prompt2 = new Label( "Enter second string:" );

promptNum = new Label( "Enter number of characters:" );

promptIndex = new Label( "Enter starting index:" );

input1 = new TextField( 20 );

input2 = new TextField( 20 );

inputNum = new TextField( 4 );

inputIndex = new TextField( 4 );

inputIndex.addActionListener( this );

add( prompt1 );

add( input1 );

add( prompt2 );

add( input2 );

add( promptNum );

add( inputNum );

add( promptIndex );

add( inputIndex );

}

public void actionPerformed( ActionEvent e )

{

String first = input1.getText();

String second = input2.getText();

int number = Integer.parseInt( inputNum.getText() );

int index = Integer.parseInt( inputIndex.getText() );

if ( first.regionMatches( true, index, second, 0, number ) )

showStatus( "The strings are equivalent." );

else

showStatus( "The strings are NOT equivalent." );

}

}

 

// Exercise 8.12 Solution

// Phone.java

// Program separates phone area

// code from the other seven digits

import java.applet.Applet;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class Phone extends Applet

implements ActionListener {

private TextField input;

private Label prompt;

public void init()

{

input = new TextField( 12 );

input.addActionListener( this );

prompt = new Label( "Enter phone number:" );

add( prompt );

add( input );

}

public void actionPerformed( ActionEvent e )

{

int x = 0;

StringTokenizer t = new StringTokenizer( input.getText() );

String tokens[] = new String[ t.countTokens() + 1 ], s = "";

tokens[ x++ ] = t.nextToken( "()" ); // area code

tokens[ x++ ] = t.nextToken( " )-" ); // exchange

tokens[ x ] = t.nextToken(); // line

for ( int i = 0; i < tokens.length; i++ )

s += tokens[ i ];

int areaCode = Integer.parseInt( tokens[ 0 ] );

long number = new Long( tokens[ 1 ] + tokens[ 2 ] ).longValue();

showStatus( s + " -> " + areaCode + " " + number );

}

}