0% found this document useful (0 votes)
7 views3 pages

Pa7 HW1

asdf

Uploaded by

Krish Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Pa7 HW1

asdf

Uploaded by

Krish Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PA7 HW1 Krish Joshi, Lorton Chung

Activity 1: Design and Create a Card Class


No Questions

Activity 2: Initial Design of a Deck Class


Questions:
1. Explain in your own words the relationship between a deck and a card.
a. Answer: The deck holds the cards and declares the cards using the
inputs.
2. Consider the deck initialized with the statements below. How many cards
does the deck contain? String[] ranks = {"jack", "queen", "king"};
String[] suits = {"blue", "red"};
int[] pointValues = {11, 12, 13};
Deck d = new Deck(ranks, suits, pointValues);
Answer: The deck contains 6 cards

3. The game of Twenty-One is played with a deck of 52 cards. Ranks run from
ace (highest) down to 2 (lowest). Suits are spades, hearts, diamonds, and
clubs as in many other games. A face card has point value 10; an ace has
point value 11; point values for 2, ..., 10 are 2, ..., 10, respectively. Specify
the contents of the ranks, suits, and pointValues arrays so that the statement
Deck d = new Deck(ranks, suits, pointValues); initializes a deck for a Twenty-
One game.
String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
String[] suits = { "Spades", "Diamonds", "Clubs", "Hearts" };
int[] values = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

4. Does the order of elements of the ranks, suits, and pointValues arrays
matter?
Answer: They only matter in the order relevant to each other.
Activity 3: Shuffling the Cards in a Deck
Questions:
1. Write a static method named flip that simulates a flip of a weighted coin by
returning either "heads" or "tails" each time it is called. The coin is twice as
likely to turn up heads as tails. Thus, flip should return "heads" about twice as
often as it returns "tails."
public static String flipCoin() {
Random rand = new Random();
int flip = rand.nextInt(4);
if (flip == 0 || flip == 1 || flip == 2) {
return "heads";
} else {
return "tails";
}
}

2. Write a static method named are Permutations that, given two int arrays of
the same length but with no duplicate elements, returns true if one array is a
permutation of the other (i.e., the arrays differ only in how their contents are
arranged). Otherwise, it should return false.
public static boolean arePermutations(int[] arr1, int[] arr2) {
if (arr1.length != arr2.length) {
return false;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
3. Suppose that the initial contents of the values array in Shuffler.java are {1, 2,
3, 4.}. For what sequence of random integers would the efficient selection
shuffle change values to contain {4, 3, 2, 1}?
a. Step 1) Swaps 1 with 4
b. Step 2) Swaps 2 with 3
c. Step 3) Swaps 3 with 4

Activity 4: Adding a Shuffle Method to the


Deck Class
Questions:
No Questions:

You might also like