Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Java Elevens Lab.

Similar presentations


Presentation on theme: "AP Java Elevens Lab."— Presentation transcript:

1 AP Java Elevens Lab

2 Learning Objectives Be able to create and test a card Class.
Be able to create and test a Deck class. Be able to create and test a Perfect Shuffle method and an efficient Selection Shuffle as described in Activity 3 Be able to add a shuffle method to the Deck class.

3 References Elevens Lab Student Packet in the Assignments folder
Elevens video ( The elevens Lab is discussed from the 2:47 to the 11:33 mark in the video Elevens Lab Starter Code in the Assignments Folder

4 Elevens: Card Class Start a new Project for your Elevens Lab
Add the Card class from Activity 1 Edit -> Add Class from File -> Open the Card Class from the Assignments Folder Open the Card Class and complete the missing methods Constructor Accessors for rank, suit and point value matches method for checking for equality toString method Add the CardTester class and fill in the code as needed to test all of your Card Class Methods. Need at least three Cards created to check for equality and inequality. Call each of the methods to check to see if they work. Turn in YourNameCardActivity1 and YourNameDriverActivity1

5 Elevens Deck Class What variables will the Deck contain?
Example: Info sent to create the Deck (cards) ranks = {“A”, “B”, “C”} suits = {“Pigs”, “Chickens”} values = { 1, 4, 10} What Cards will the deck (cards) contain? How can you do this? Hands on Pseudo Code What will the value of size be? Variables: size = the number of undealt cards Cards = An arrayList of all of the cards, both dealt and undealt 3) [“A”, “Pigs”, 1] , [“B”, “Pigs”, 4], [“C”, “Pigs”, 10], [“A”, “Chickens”, 1] , [“B”, “Chickens”, 4], [“C”, “Chickens”, 10] 4) Nest loops for each rank, for each suit, create a Card and add it to cards 5) Size will start out as the size() of the cards ArrayList

6 Elevens: Deck Class Deck Constructor Translating to Java
Create a new ArrayList of <Card> (cards) When constructing the Deck For each rank (In the array ranks) For each suit (In the ArrayList suits) Create a Card for the rank, suit and value and add it to the Card ArrayList (cards) Set the ‘size’ of non dealt cards to the size of the ‘cards’ ArrayList Shuffle the deck Translating to Java cards = new ArrayList<Card>(); for (int rankCount = 0; rankCount< ranks.length ; rankCount++) { for (int suitCount =0; suitCount<suits.length;suitCount++) cards.add(new Card(ranks[rankCount], suit[suitCount], values[rankCount]); } size =cards.size(); shuffle();

7 Elevens: Deck Class Size Deal isEmpty method
Returns a true if there are not more cards to deal (size ==0) Size Returns the number of undealt cards Deal If there are no cards left, return null Else return the last card and decrement the size toString (Next Slide)

8 Deck Tester String[] ranks = {“A”, “B”, “C”};
String[] suits = {“Green”, “Orange”}; int[] pointValues = {11, 12, 13}; Deck d = new Deck(ranks, suits, pointValues); Demonstrate each method created Deal a card Show there are fewer cards and the size is changed

9 Activity 3: Shuffling Read Pages 7- 10 Complete Exercises 1 and 2
Answer Questions 1-3 Write the code for flip Write the code for arePermutations Answer question 3

10 Finding the mid-point (shelf 5) for the given array?
Perfect Shuffle values array temp array Index Card “A”, “Chicken”, 1 1 “A”, ”Pig”, 1 2 “B”, “Chicken”, 2 3 “B”, “Pig”, 2 4 “C”, “Chicken”, 3 5 “C”, “Pig”, 3 6 “D”, “Chicken”, 4 7 “D”, “Pig”, 4 8 “E”, “Chicken”, 5 9 “E”, “Pig”, 5 Index Card “A”, “Chicken”, 1 1 “C”, “Pig”, 3 2 “A”, ”Pig”, 1 3 “D”, “Chicken”, 4 4 “B”, “Chicken”, 2 5 “D”, “Pig”, 4 6 “B”, “Pig”, 2 7 E”, “Chicken”, 5 8 “C”, “Chicken”, 3 9 “E”, “Pig”, 5 Finding the mid-point (shelf 5) for the given array?

11 Perfect Shuffle Algorithm
Create a temporary Array for storing the sorted values Take the first half of the value array Place their items in the even shelves of the temp array Take the second half of the value array Place in the odd shelves of the temp array Copy the temporary array back to the original array int [] temp = new int[values.length]; int mid = values.length / 2; int evenShelf = 0; for (int firstHalf = 0; firstHalf<mid;firstHalf++) { temp[evenShelf] = values[firstHalf]; evenShelf+=2; } int oddShelf = 1; for (int secondHalf=mid; secondHalf<values.length;secondHalf++) temp[oddShelf] = values[secondHalf]; oddShelf+2; For(int copyShelf=0; copyShelf<values.length; copyShelf++) values[copyShelf] = temp[copyShelf];

12 Selection Shuffle For every position in the deck
Track the last unshuffled position in the deck. (lastPos) Random select a card address (Address 0 to lastPos) Switch the card at this position with the card in the last position. Dry Run Address Card ‘A’ 1 ‘B’ 2 ‘C’ 3 ‘D’ 4 ‘E’

13 Selection Shuffle Pseudo Code Code For every position in the deck
Track the last unshuffled position in the deck. (lastPos) Pick a Random card address (Address 0 to lastPos) Switch the card at this position with the card in the last position. for (int lastPos = values.length-1;lastPos>0; lastPos--) { int pick = (int) (Math.random()*(lastPos+1)); int temp = values[pick];//Switch values[pick] = values[lastPos]; values[lastPos] = temp; }

14 Complete Activity 3 Exercise 1: Use the Shuffler.java file to implement the Perfect Shuffle and the Selection Shuffle for an array of integers. Exercise 2: Shuffler.java provides the main method that calls the shuffling methods. Execute the main method and inspect the output to see how well =each shuffle method actually randomizes the array elements. You should execute main with different values of SHUFFLE_COUNT and VALUE_COUNT

15 Complete Activity 4 Add the Shuffle Method and modify as needed to the Deck Class. Use the selection shuffle from Activity 3. The DeckTester.java file, found in the Activity4 Starter Code folder, provides a basic set of Deck tests. It is similar to the DeckTester class you might have written in Activity 2. Add code to the bottom of the main method to create a deck of 52 cards and test the shuffle method. You can use the Deck toString method to “see” the cards after every shuffle.

16 Activity 4: Shuffling the Deck
Complete the exercises Add the shuffle method to the Deck class. The Deck constructor should create the Deck and call the shuffle method. The shuffle method also needs to reset the value of size to indicate that all of the cards can be dealt again. Complete the DeckTester class

17 Part 5: Extra Assertions

18 Activity 6: Playing Elevens
Play the game (Yes you can play this video game in class) Answer Questions 1-3 and turn in as YourNameElevensActivity6


Download ppt "AP Java Elevens Lab."

Similar presentations


Ads by Google