Question 1: Arrays & ArrayLists
PART A: Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.
public class PokerHandValueCalculator {
public static void main(String[] args) {
// This is an array representing the values of a poker hand
int[] handValues = {1, 13, 10, 11, 12};
// this calculates the total value of a poker hand
int totalHandValue = calculateHandValue(handValues);
// prints the total value of poker hand
System.out.println("Total value of poker hand: " + totalHandValue);
}
// calculates total value of poker hand
public static int calculateHandValue(int[] values) {
// stores the total hand value using variable
int total = 0;
// This block of code Loops through each value in the handValues array and adds them to the total
for (int value : values) {
total += value;
}
return total;
}
}
PokerHandValueCalculator.main(null);
Total value of poker hand: 47
Part A Explanation
For Part A, I used the PokerHandValueCalculator class that calculates the total value of a poker hand which is given in an array of card values. Basically, the handValues array shows a hand of cards using integers, where the card number is from 1-A, and there four shapes: Hearts (H), Clubs (C), Spades (S), and Diamonds (D). The calculateHandValue method sums these values to calculate the total value of the hand. This simplifies the the tough part of poker but serves as a good example of how I modeled a poker system in code using array lists.
PART B: Write a static method rowSums that calculates the sums of each of the rows in a given two-dimensional array and returns these sums in a one-dimensional array. The method has one parameter, a two-dimensional array arr2D of int values. The array is in row-major order: arr2D [r] [c] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.
public class PokerHandSums {
public static void main(String[] args) { // Main method declaration
// This declares and initializes a 2D array representing poker hands
int[][] pokerHands = {
{1, 10, 11, 12, 13},
{2, 3, 4, 5, 6},
{7, 8, 9, 10, 11}
};
// This calls the calculateHandSums method and stores the returned array
int[] handSums = calculateHandSums(pokerHands);
// This keeps repeating the handSums array and prints the sum of each hand
for (int i = 0; i < handSums.length; i++) {
System.out.println("Total value of hand " + (i + 1) + ": " + handSums[i]);
}
}
// Right here is a method that calculates the sum of each hand in the pokerHands array
public static int[] calculateHandSums(int[][] hands) {
// this stores the sum
int[] sums = new int[hands.length];
// this loops through each hand
for (int i = 0; i < hands.length; i++) {
int sum = 0;
// loops through each card in the hand and calculates the value/sum
for (int j = 0; j < hands[i].length; j++) {
sum += hands[i][j];
}
// Stores the sum of the hand in the sums array
sums[i] = sum;
}
return sums;
}
}
PokerHandSums.main(null);
Total value of hand 1: 47
Total value of hand 2: 20
Total value of hand 3: 45
Part B Explanation
Here I used PokerHandSums class to calculate the total value of each poker hand in a two dimensional array which returns these totals in a one dimensional array. The pokerHands shows all many hands of cards, with each row bring a different hand. he calculateHandSums method calculates the sum of the values for each hand and stores these sums in a new array, which it then returns.
PART C: A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum. Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse (mat1) returns true and the call isDiverse(mat2) returns false.
import java.util.Arrays;
public class PokerDiversityChecker {
public static void main(String[] args) {
// This declares and initializes a 2D array representing poker hands
int[][] pokerSession1 = {
{1, 11, 10, 12, 13},
{2, 3, 4, 5, 6},
{7, 8, 9, 10, 11}
};
int[][] pokerSession2 = {
{1, 11, 10, 12, 13},
{2, 3, 4, 5, 6},
{1, 11, 10, 12, 13}
};
// this checks the diversity of poker sessions and checks if it is true, basically no repetitions through isDiverse
System.out.println("Poker session 1 is diverse: " + isDiverse(pokerSession1)); // Expected: true
System.out.println("Poker session 2 is diverse: " + isDiverse(pokerSession2)); // Expected: false
}
// this uses a method to check if the poker session is diverse
public static boolean isDiverse(int[][] hands) {
// Calculating total values for each hand
int[] handValues = rowSums(hands);
Arrays.sort(handValues); // Sort the total values
// this checks for duplicates
for (int i = 0; i < handValues.length - 1; i++) {
if (handValues[i] == handValues[i + 1]) {
return false; //duplicate total values not diverse
}
}
return true; // this returns if all is good and no duplication is found
}
// Method to calculate the total values for each hand in a poker session
public static int[] rowSums(int[][] hands) {
// Here is the array to store total values for each hand
int[] sums = new int[hands.length];
for (int i = 0; i < hands.length; i++) {
sums[i] = arraySum(hands[i]); // calculating total value for each hand
}
return sums;
}
// Method to calculate the sum of elements in an array
public static int arraySum(int[] hand) {
int sum = 0;
for (int value : hand) {
sum += value;
}
return sum;
}
}
PokerDiversityChecker.main(null); // Invoking the main method
Poker session 1 is diverse: true
Poker session 2 is diverse: false
Part C Explanation
In this problem, I decided to think that pokerSession1 and pokerSession2 represent two sets of poker hands. The isDiverse method checks if each of hands is unique based on their value. It also checks for duplication, so that no two hands are the same in a session. This program is a good way to analyze which outcomes might be possible based on the poker hand given.
When it says a poker session is diverse in the context of this code, it means that each poker hand in the session has a different total value. In other words, none of the poker hands have the same sum of values.
This relates to our project due to our upcoming feature PLACEPoker. We plan to embed a multitude of games in order to build a centralized gaming platform with channels of communication.