Mr. Meinzen - Java Handouts (second 9 weeks)

"Success is the ability to go from one failure to another with no loss of enthusiasm." Winston Churchill

Java Handout: Program Card class - Sample Code


public class Card
	// instance variables - add comments & methods to the code below
	private int     	suit;		
	private int     	value;			
	private int     	rank;			
	private ImageIcon	faceImg;		
	private ImageIcon	backImg;			

	* Constructor for objects of class Card
	public Card(int r)
		// initialize instance variables
		this.rank = r;			
		suit      = r/13;		
		value     = 2 + r % 13;	

	* Blank card constructor
	public Card()

	public ImageIcon getFaceImage()

	public int getRank()

	public int getValue()

	public int getSuit()

	* The toString() method returns the English name of the card's value and suit
	public String toString()
		String temp = "The card is a ";
		if (value == 14) { temp = temp + "Ace of ";}
		if (value == 13) { temp = temp + "King of ";}
		if (value <= 10) { temp = temp + value + " of ";}

		if (suit == 3)   { temp += "clubs";}

		return temp;
}	// end Card class

Java Handout: Program Deck class - Assignment


Program the Deck class.


The following specifications are required. Do NOT forget to test every method to make sure your program works!

  • Declare an array of Cards called "myDeck"

  • Declare an integer that will serve as a counter of the number of cards that have been dealt

  • Define a constructor method that will:

    • instantiate the array to hold 52 (empty) slots for the Cards

    • contain a loop to instantiate 52 Cards and assign the Card objects to the corresponding index (slots) in the array

  • Define the method to deal one card from the deck (advanced accessor method for arrays as discussed in class)

  • Define the method to sort by rank (using bubble sort algorithm discussed in class)

  • Define the method to shuffle (using randomization algorithm discussed in class)

Java Handout: Program HighCardGame - Assignment


Based upon the previous SwingTemplate, you will write an application that plays a High Card game. The program will have the following look and specifications:


First card value: Second card value: winner is:


  • ( ) details:
    • Have a layout manager (2 rows by 3 columns)
    • Have exactly 1 Deck object
    • Have 2 Card objects
    • Have 3 JButtons objects (2 for card images and 1 for the "play" button)
    • Have 3 JTextfields (first two display the value of each player's card and the third to display the player's name (or number) whose card has the highest value)
  • High Card game is played with the following rules :
    • The game starts with a deck and 2 cards face down on the table (screen).
    • When the "play" button is pressed, two cards are dealt from the deck and their face images are displayed on each button.
    • The value of each card is displayed in textfields below each card and the card with the highest value is declared the "winner" in the 3rd textfield under the play button.

Java Handout: Program TenCard application - Assignment


Each student is to create a copy of their HighCard game called and make the following modifications:

  1. Has 1 Deck object.

  2. Has 10 Card objects (5 representing what will be the first player's cards on the table/screen and 5 representing what will be the second player's cards on the table/screen.

  3. Has 10 buttons on the screen (5 in first row, 5 in second row) that duplicate everything that the first two buttons in did.

  4. Has two extra buttons on the bottom row:

    1. The first button is to sort the deck

    2. The second button is to shuffle the deck.

  5. Has two textfields on the bottom row (one for each player) that will be used later in PokeGamer to explain what each player's hand type is (i.e. "Player 1 has a royal flush").

  6. Comment each method correctly.

  7. Be prepared to show your teacher your working program at the start of class within 1 week

  8. Note: TenCard is NOT a card game since there are no "players" (just cards that represent the players' hands that are displayed on the table/screen). TenCard will allow the student to focus on the graphical (GUI) elements in preparation for the PokerGame (or any other card game that requires more than 2 cards).

Honors Java Programming (regular Java may also try this for bonus points)

  1. Include another button that "flips" all of the cards so that the all buttons will show only the back of the cards' images

  2. Use two arrays of 5 buttons and two arrays of 5 cardsOnTable (i.e. 4 arrays) rather than 10 different button variables with 10 different cards on the table.

Java Handout: Program BasicPlayer class - Assignment


The BasicPlayer class will form the foundation for a more advanced Player that will be assigned later for the poker game (or other card game). The following variables & methods will be required in the class:

  • declare an array cards called "myHand"

  • declare an integer to keep track of the number of cards currently in the player's hand

  • (optional) declare a name and an ImageIcon for the player

  • declare a constructor method that instantiates the hand to hold 5 (empty) slots for the players cards

  • declare a method to set a card in the player's hand (advanced mutator method for arrays)

  • declare a method to get a card from the player's hand (advanced accessor method for arrays)

  • declare a method to sort the player's hand by rank

  • declare a method to sort the player's hand by suit

  • declare a method to sort the player's hand by value

  • (optional) declare the accessor and mutator methods for the name and image of the BasicPlayer

At this point, the BasicPlayer class should look very have already written the variables and methods in another previous class....

Java Handout: Program PokerGame application - Assignment


The student is to create a copy of their TenCard application, call the new class and make the following modifications:

  • Keep the 10 buttons on the screen (5 in first row, 5 in second row) that do everything that did

  • The PokerGame must have two PokerPlayer objects (player1 and player2)

  • One button on the bottom called "Deal" that will:

    • a) deal 5 cards to each player
    • b) ask each player the type of hand that they have (& display the result in each player's textfield...see below)
    • c) get the 5 cards out of each player's hands, onto the table, and display the images on the respective buttons.
  • Two (or Three) text fields on the bottom:

    • One textfield which shows player1's hand type (i.e. "player 1 has a three of kind");

    • One textfield which shows player2's hand type (i.e. "player 2 has two pair");

    • (Optional) One textfield which shows which player had the better hand (i.e. "Player 1 wins");

  • Comment each method correctly.

  • Be prepared to show your teacher your working program at the start of class.

Java Handout: Program PokerPlayer class - Assignment


Modify the BasicPlayer class to include the following private boolean methods:

  1. royalFlush()

  2. fourOfKind()

  3. straightFlush()

  4. fullHouse()

  5. flush()

  6. straight()

  7. threeOfKind()

  8. twoPair()

  9. pair()

  10. highCard()

  • Your teacher will write one of the above methods in class as a reference to be used for the logic needed for the other nine methods.

  • You will also need to include a final method with a signature of:

    • public String getHandType()

  • The purpose of this method is for the PokerPlayer to be able to inform the PokerGame exactly what type of hand the player calculates that they have (assuming 5 cards are in the player's hand). The PokerGame will call this method and put the returned String into the textfield for the appropriate PokerPlayer (player1 or player2).

  1. Determine the final value of the variable.


    A) B) C)
    int x = 4; 
    x *= 3;
    int x = 10/3; 
    if (x < 3)
        x = 10;
    int x = -5; 
    while (x < 7)
        x ++;
    x = 
    x = 
    x = 
    D) E) F)
    int x = 17; 
    x = x % 3;
    int x = 5; 
    if (x > 6);
        x = 7;
    int x = 13; 
    for (int j=1; j < 4; j++)
        x = j-x;
    x = 
    x = 
    x = 
  2. Identify the following parts (i.e. variables, classes, constructors, etc.) in the statement:

    • Ship canoe = new Ship();




    new Ship()

    Ship canoe

  3. Write a for loop to print out the numbers 2 to 25 [Honors should be able to write a while loop to do the same]





  4. Circle the 10 syntax errors in the following code:


    public class Ship()
         private int     sailors;
         private boolean scurvy == true;
         public   getsailors()
              return sailors
         public void setSailors[]
              sailors = -5;
         public   void   main(String arg[])
              System.out.println("# of sailors = " + number);
    {   // Ship;
  5. Write the five questions when designing a class:

  6. Write the three questions when designing a method:

  7. Write the three steps for loop control:

  8. Rewrite the following declarations using Standard Naming Conventions

    • public void GetBoats()
    • public class DECK
    • private int TopCard;
    • public int Setcard(Card C)
    • private boolean donewithLoop
  9. Given the following class, write the methods to set the paper color and get the paper color

     public class PaperDolls
          private Color paper;
    	  /** Write the method to get the color of paper.
      	  *   You DON'T have to use all lines
     	   /** Write the method to set the paper color */
  10. Given the following method, what would the scores array contain after myMethod() was called:


    public void myMethod() 	  	
      	int j = 0;	
      	int scores[] = new int[4];		
      	scores[0] = 50; scores[1] = 22; scores[2] = 76; scores[3] =43;	
      	for (int j=0; j < 3; j++) 	  	
      	    if (scores[j] < scores[j+1]) 		  		
      	        int temp      = scores[j]; 	
      	        scores[j]     = scores[j+1]; 	
      	        scores[j+1]   = temp; 	
      	    } // if 	  	  	
      	} // for 	  	  	  	
    } // myMethod()


    At the start of the for loop, the content of the scores array is:












    At the end of the method, the content of the scores array will be: