import java.util.Scanner;
public class Assignment4 {
public static void main(String args[]) {
final int LEN = 5;
int[] hand = new int[LEN];
Scanner input = new Scanner(System.in);
//input the hand
System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
for (int i = 0; i < hand.length; i++) {
hand[i] = input.nextInt();
}
//sort the collection
bubbleSortCards(hand);
//determine players hand type
//flow of evaluation -> checking complex hands first
if (containsFullHouse(hand)) {
System.out.println("Full House!");
} else if (containsStraight(hand)) {
System.out.println("Straight!");
} else if (containsFourOfaKind(hand)) {
System.out.println("Four of a Kind!");
} else if (containsThreeOfaKind(hand)) {
System.out.println("Three of a Kind!");
} else if (containsTwoPair(hand)) {
System.out.println("Two Pair!");
} else if (containsPair(hand)) {
System.out.println("Pair!");
} else
System.out.println("High Card!");
}
//cardInSets is called with hand[] and pair to check if a pair is found.
public static boolean containsPair(int hand[]) {
int pair = 2;
return cardInSets(hand, pair) != NOT_FOUND;
}
//cardInSets is called with hand[] and pair to check if a pair is found.
//Then calls back again with fcard result to check for another pair.
public static boolean containsTwoPair(int hand[]) {
int pair = 2;
int fcard = cardInSets(hand, pair);
int scard = cardInSets(hand, pair, fcard);
return fcard!=NOT_FOUND && scard!=NOT_FOUND;
}
//cardInSets is called with hand[] and threeaKind to check if three of a kind has been found.
public static boolean containsThreeOfaKind(int hand[]) {
int threeaKind = 3;
return cardInSets(hand, threeaKind) != NOT_FOUND;
}
//hasStraight will resolve to true, unless the previous value++ is not equivalent.
//The loop ends as soon as hasStaight is false or the end of the hand is reached.
public static boolean containsStraight(int hand[]) {
boolean hasStraight = true;
int i=1;
do {
hasStraight = (hand[i] == (hand[i-1] + 1));
} while(hasStraight && ++i < hand.length);
return hasStraight;
}
//cardInSets is called with hand[] and threeKinds to check if three of a kind is found.
//Then calls back again with pair and tcard result to check for a pair of another card type.
public static boolean containsFullHouse(int hand[]) {
int threeKinds = 3;
int pair = 2;
int tcard = cardInSets(hand, threeKinds);
int pcard = cardInSets(hand, pair, tcard);
return tcard!=NOT_FOUND && pcard!=NOT_FOUND;
}
//cardInSets is called with hand[] and fouraKind to check if four of a kind is found.
public static boolean containsFourOfaKind(int hand[]) {
int fouraKind = 4;
return cardInSets(hand, fouraKind) != NOT_FOUND;
}
//When ignoreCard isn't needed, it overloads with a sentinel 0.
public static int cardInSets(int hand[], int numKinds) {
return cardInSets(hand, numKinds, 0);
}
//The method cardInSets is a generic evaluation that returns if the parameter specified
numKinds(like 2 for pair) has been found in the hand.
//The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation.
//After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair
evaluation will not be of the same card type.
public static int cardInSets(int hand[], int numKinds, int ignoreCard) {
numKinds--;
int last = hand[0];
int card=NOT_FOUND;
int numMatch = 0;
for (int i = 1; i < hand.length; i++) {
if (last == hand[i] && ignoreCard!=last) {
numMatch++;
//hasNumKinds = hasMatch==numKinds;
if (numMatch==numKinds) {
card=last;
break;
}
} else numMatch = NOT_FOUND;
last = hand[i];
}
return card;
}
//For ease of evaluation, the hand is sorted after input by bubble sort.
public static int[] bubbleSortCards(int hand[]) {
int temp;
for (int i=0; i < hand.length-1; i++) {
for (int j=1; j < hand.length-i; j++) {
if (hand[j-1] > hand[j]) {
temp = hand[j-1];
hand[j-1] = hand[j];
hand[j] = temp;
}
}
}
return hand;
}
}
Solution
import java.util.Scanner;
public class Assignment4 {
public static void main(String args[]) {
final int LEN = 5;
int[] hand = new int[LEN];
Scanner input = new Scanner(System.in);
//input the hand
System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
for (int i = 0; i < hand.length; i++) {
hand[i] = input.nextInt();
}
//sort the collection
bubbleSortCards(hand);
//determine players hand type
//flow of evaluation -> checking complex hands first
if (containsFullHouse(hand)) {
System.out.println("Full House!");
} else if (containsStraight(hand)) {
System.out.println("Straight!");
} else if (containsFourOfaKind(hand)) {
System.out.println("Four of a Kind!");
} else if (containsThreeOfaKind(hand)) {
System.out.println("Three of a Kind!");
} else if (containsTwoPair(hand)) {
System.out.println("Two Pair!");
} else if (containsPair(hand)) {
System.out.println("Pair!");
} else
System.out.println("High Card!");
}
//cardInSets is called with hand[] and pair to check if a pair is found.
public static boolean containsPair(int hand[]) {
int pair = 2;
return cardInSets(hand, pair) != NOT_FOUND;
}
//cardInSets is called with hand[] and pair to check if a pair is found.
//Then calls back again with fcard result to check for another pair.
public static boolean containsTwoPair(int hand[]) {
int pair = 2;
int fcard = cardInSets(hand, pair);
int scard = cardInSets(hand, pair, fcard);
return fcard!=NOT_FOUND && scard!=NOT_FOUND;
}
//cardInSets is called with hand[] and threeaKind to check if three of a kind has been found.
public static boolean containsThreeOfaKind(int hand[]) {
int threeaKind = 3;
return cardInSets(hand, threeaKind) != NOT_FOUND;
}
//hasStraight will resolve to true, unless the previous value++ is not equivalent.
//The loop ends as soon as hasStaight is false or the end of the hand is reached.
public static boolean containsStraight(int hand[]) {
boolean hasStraight = true;
int i=1;
do {
hasStraight = (hand[i] == (hand[i-1] + 1));
} while(hasStraight && ++i < hand.length);
return hasStraight;
}
//cardInSets is called with hand[] and threeKinds to check if three of a kind is found.
//Then calls back again with pair and tcard result to check for a pair of another card type.
public static boolean containsFullHouse(int hand[]) {
int threeKinds = 3;
int pair = 2;
int tcard = cardInSets(hand, threeKinds);
int pcard = cardInSets(hand, pair, tcard);
return tcard!=NOT_FOUND && pcard!=NOT_FOUND;
}
//cardInSets is called with hand[] and fouraKind to check if four of a kind is found.
public static boolean containsFourOfaKind(int hand[]) {
int fouraKind = 4;
return cardInSets(hand, fouraKind) != NOT_FOUND;
}
//When ignoreCard isn't needed, it overloads with a sentinel 0.
public static int cardInSets(int hand[], int numKinds) {
return cardInSets(hand, numKinds, 0);
}
//The method cardInSets is a generic evaluation that returns if the parameter specified
numKinds(like 2 for pair) has been found in the hand.
//The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation.
//After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair
evaluation will not be of the same card type.
public static int cardInSets(int hand[], int numKinds, int ignoreCard) {
numKinds--;
int last = hand[0];
int card=NOT_FOUND;
int numMatch = 0;
for (int i = 1; i < hand.length; i++) {
if (last == hand[i] && ignoreCard!=last) {
numMatch++;
//hasNumKinds = hasMatch==numKinds;
if (numMatch==numKinds) {
card=last;
break;
}
} else numMatch = NOT_FOUND;
last = hand[i];
}
return card;
}
//For ease of evaluation, the hand is sorted after input by bubble sort.
public static int[] bubbleSortCards(int hand[]) {
int temp;
for (int i=0; i < hand.length-1; i++) {
for (int j=1; j < hand.length-i; j++) {
if (hand[j-1] > hand[j]) {
temp = hand[j-1];
hand[j-1] = hand[j];
hand[j] = temp;
}
}
}
return hand;
}
}

import java.util.Scanner;public class Assignment4 {    public st.pdf

  • 1.
    import java.util.Scanner; public classAssignment4 { public static void main(String args[]) { final int LEN = 5; int[] hand = new int[LEN]; Scanner input = new Scanner(System.in); //input the hand System.out.println("Enter five numeric cards, no face cards. Use 2-9."); for (int i = 0; i < hand.length; i++) { hand[i] = input.nextInt(); } //sort the collection bubbleSortCards(hand); //determine players hand type //flow of evaluation -> checking complex hands first if (containsFullHouse(hand)) { System.out.println("Full House!"); } else if (containsStraight(hand)) { System.out.println("Straight!"); } else if (containsFourOfaKind(hand)) { System.out.println("Four of a Kind!"); } else if (containsThreeOfaKind(hand)) { System.out.println("Three of a Kind!"); } else if (containsTwoPair(hand)) { System.out.println("Two Pair!"); } else if (containsPair(hand)) { System.out.println("Pair!"); } else System.out.println("High Card!"); } //cardInSets is called with hand[] and pair to check if a pair is found. public static boolean containsPair(int hand[]) { int pair = 2;
  • 2.
    return cardInSets(hand, pair)!= NOT_FOUND; } //cardInSets is called with hand[] and pair to check if a pair is found. //Then calls back again with fcard result to check for another pair. public static boolean containsTwoPair(int hand[]) { int pair = 2; int fcard = cardInSets(hand, pair); int scard = cardInSets(hand, pair, fcard); return fcard!=NOT_FOUND && scard!=NOT_FOUND; } //cardInSets is called with hand[] and threeaKind to check if three of a kind has been found. public static boolean containsThreeOfaKind(int hand[]) { int threeaKind = 3; return cardInSets(hand, threeaKind) != NOT_FOUND; } //hasStraight will resolve to true, unless the previous value++ is not equivalent. //The loop ends as soon as hasStaight is false or the end of the hand is reached. public static boolean containsStraight(int hand[]) { boolean hasStraight = true; int i=1; do { hasStraight = (hand[i] == (hand[i-1] + 1)); } while(hasStraight && ++i < hand.length); return hasStraight; } //cardInSets is called with hand[] and threeKinds to check if three of a kind is found. //Then calls back again with pair and tcard result to check for a pair of another card type. public static boolean containsFullHouse(int hand[]) { int threeKinds = 3; int pair = 2; int tcard = cardInSets(hand, threeKinds); int pcard = cardInSets(hand, pair, tcard); return tcard!=NOT_FOUND && pcard!=NOT_FOUND; } //cardInSets is called with hand[] and fouraKind to check if four of a kind is found. public static boolean containsFourOfaKind(int hand[]) {
  • 3.
    int fouraKind =4; return cardInSets(hand, fouraKind) != NOT_FOUND; } //When ignoreCard isn't needed, it overloads with a sentinel 0. public static int cardInSets(int hand[], int numKinds) { return cardInSets(hand, numKinds, 0); } //The method cardInSets is a generic evaluation that returns if the parameter specified numKinds(like 2 for pair) has been found in the hand. //The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation. //After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair evaluation will not be of the same card type. public static int cardInSets(int hand[], int numKinds, int ignoreCard) { numKinds--; int last = hand[0]; int card=NOT_FOUND; int numMatch = 0; for (int i = 1; i < hand.length; i++) { if (last == hand[i] && ignoreCard!=last) { numMatch++; //hasNumKinds = hasMatch==numKinds; if (numMatch==numKinds) { card=last; break; } } else numMatch = NOT_FOUND; last = hand[i]; } return card; } //For ease of evaluation, the hand is sorted after input by bubble sort. public static int[] bubbleSortCards(int hand[]) { int temp; for (int i=0; i < hand.length-1; i++) {
  • 4.
    for (int j=1;j < hand.length-i; j++) { if (hand[j-1] > hand[j]) { temp = hand[j-1]; hand[j-1] = hand[j]; hand[j] = temp; } } } return hand; } } Solution import java.util.Scanner; public class Assignment4 { public static void main(String args[]) { final int LEN = 5; int[] hand = new int[LEN]; Scanner input = new Scanner(System.in); //input the hand System.out.println("Enter five numeric cards, no face cards. Use 2-9."); for (int i = 0; i < hand.length; i++) { hand[i] = input.nextInt(); } //sort the collection bubbleSortCards(hand); //determine players hand type //flow of evaluation -> checking complex hands first if (containsFullHouse(hand)) { System.out.println("Full House!"); } else if (containsStraight(hand)) { System.out.println("Straight!"); } else if (containsFourOfaKind(hand)) { System.out.println("Four of a Kind!");
  • 5.
    } else if(containsThreeOfaKind(hand)) { System.out.println("Three of a Kind!"); } else if (containsTwoPair(hand)) { System.out.println("Two Pair!"); } else if (containsPair(hand)) { System.out.println("Pair!"); } else System.out.println("High Card!"); } //cardInSets is called with hand[] and pair to check if a pair is found. public static boolean containsPair(int hand[]) { int pair = 2; return cardInSets(hand, pair) != NOT_FOUND; } //cardInSets is called with hand[] and pair to check if a pair is found. //Then calls back again with fcard result to check for another pair. public static boolean containsTwoPair(int hand[]) { int pair = 2; int fcard = cardInSets(hand, pair); int scard = cardInSets(hand, pair, fcard); return fcard!=NOT_FOUND && scard!=NOT_FOUND; } //cardInSets is called with hand[] and threeaKind to check if three of a kind has been found. public static boolean containsThreeOfaKind(int hand[]) { int threeaKind = 3; return cardInSets(hand, threeaKind) != NOT_FOUND; } //hasStraight will resolve to true, unless the previous value++ is not equivalent. //The loop ends as soon as hasStaight is false or the end of the hand is reached. public static boolean containsStraight(int hand[]) { boolean hasStraight = true; int i=1; do { hasStraight = (hand[i] == (hand[i-1] + 1)); } while(hasStraight && ++i < hand.length);
  • 6.
    return hasStraight; } //cardInSets iscalled with hand[] and threeKinds to check if three of a kind is found. //Then calls back again with pair and tcard result to check for a pair of another card type. public static boolean containsFullHouse(int hand[]) { int threeKinds = 3; int pair = 2; int tcard = cardInSets(hand, threeKinds); int pcard = cardInSets(hand, pair, tcard); return tcard!=NOT_FOUND && pcard!=NOT_FOUND; } //cardInSets is called with hand[] and fouraKind to check if four of a kind is found. public static boolean containsFourOfaKind(int hand[]) { int fouraKind = 4; return cardInSets(hand, fouraKind) != NOT_FOUND; } //When ignoreCard isn't needed, it overloads with a sentinel 0. public static int cardInSets(int hand[], int numKinds) { return cardInSets(hand, numKinds, 0); } //The method cardInSets is a generic evaluation that returns if the parameter specified numKinds(like 2 for pair) has been found in the hand. //The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation. //After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair evaluation will not be of the same card type. public static int cardInSets(int hand[], int numKinds, int ignoreCard) { numKinds--; int last = hand[0]; int card=NOT_FOUND; int numMatch = 0; for (int i = 1; i < hand.length; i++) { if (last == hand[i] && ignoreCard!=last) { numMatch++; //hasNumKinds = hasMatch==numKinds;
  • 7.
    if (numMatch==numKinds) { card=last; break; } }else numMatch = NOT_FOUND; last = hand[i]; } return card; } //For ease of evaluation, the hand is sorted after input by bubble sort. public static int[] bubbleSortCards(int hand[]) { int temp; for (int i=0; i < hand.length-1; i++) { for (int j=1; j < hand.length-i; j++) { if (hand[j-1] > hand[j]) { temp = hand[j-1]; hand[j-1] = hand[j]; hand[j] = temp; } } } return hand; } }