AP Java Elevens Lab.

Slides:



Advertisements
Similar presentations
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
Elevens Lab Student Material – on website
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Topic 3 The Stack ADT.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
By the end of this session you should be able to...
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
Designing a Card Game Solitaire--Thirteens. Game.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
AP Java Enhancing the Chatbot. Natural Language Processing Note: What is Natural Language Processing? What are some of the opportunities? What are some.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures Arrays and Lists Part 2 More List Operations.
Unified Modeling Language (UML)
Click to edit Master text styles Stacks Data Structure.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
Permutations and Combinations Review Plus a little Dry Run, Sorting and Code.
© A+ Computer Science - Elevens is a lab about classes and Lists. List is a major concept being tested by the Elevens lab. Elevens.
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
An Array-Based Implementation of the ADT List
Using recursion for Searching and Sorting
Java Arrays and ArrayLists COMP T1 #5
Stacks II David Lillis School of Computer Science and Informatics
Lecture 5 of Computer Science II
AP Java Enhancing the Chatbot.
CSC 222: Object-Oriented Programming
GC211Data Structure Lecture2 Sara Alhajjam.
Game Extras Pepper.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 7 Part 1 Edited by JJ Shepherd
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
C Basics.
While Loops BIS1523 – Lecture 12.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Arrays versus ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CIS16 Application Development and Programming using Visual Basic.net
Arrays in Java What, why and how Copyright Curt Hill.
STL - Algorithms.
Defining methods and more arrays
Selection Insertion and Merge
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
AP Java Enhancing the Chatbot.
Arrays.
Sorting Chapter 8.
Console.WriteLine(“Good luck!”);
Task 2 Implementation help
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 6: References and linked nodes reading: 16.1
Objects with ArrayLists as Attributes
CS203 Lecture 15.
First Semester Review.
CSC 205 Java Programming II
Lecture 20 – Practice Exercises 4
10.3 Bubble Sort Chapter 10 - Sorting.
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

AP Java Elevens Lab

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.

References Elevens Lab Student Packet in the Assignments folder Elevens video (https://www.youtube.com/watch?v=MCaTxw2Bu-M) 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

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

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

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();

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)

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

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

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?

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];

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’

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; }

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

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.

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

Part 5: Extra Assertions

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