AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Java Review Interface, Casting, Generics, Iterator.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Arrays And ArrayLists - S. Kelly-Bootle
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CMSC 202 Classes and Objects: Reusing Classes with Composition.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Inheritance and Polymorphism
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
AP Java Elevens Lab.
More About Objects and Methods
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Andy Wang Object Oriented Programming in C++ COP 3330
Testing and Debugging.
Lecture 2 of Computer Science II
Recitation 7 October 7, 2011.
ArrayLists 22-Feb-19.
Introduction to Data Structure
Programming with inheritance Based on slides by Alyssa Harding
slides created by Ethan Apter and Marty Stepp
Review for Midterm 3.
Presentation transcript:

AP Computer Science

 Not necessary but good programming practice in Java  When you override a super class method notation will make sure you have properly implemented the method Compiler will generate an error if you and do not override the parent method  Incorrect signature usually the reason public int compareTo(Object obj) public int compareTo(MyClass mc)

 Complete the implementation of the provided Card class. You will be required to complete: a constructor that takes two String parameters that represent the card’s rank and suit, and an int parameter that represents the point value of the card; accessor methods for the card’s rank, suit, and point value; a method to test equality between two card objects; and the toString method to create a String that contains the rank, suit, and point value of the card object. The string should be in the following format:  rank of suit (point value = pointValue)  2. Once you have completed the Card class, find the CardTester.java file. Create three Card objects and test each method for each Card object.

 Want an algorithm that is: Efficient Makes every permutation of the Deck possible Makes every permutation of the Deck equally likely

 Idea #1  Start with an array named cards that contains 52 cards  Create an array named shuffled. Initialize shuffled to contain 52 “empty” elements.  Then for k = 0 to 51,  generate a random integer j between 0 and 51 While cards[j] is empty j = new random number between 0 and 51  Copy cards[j] to shuffled[k];  Set cards[j] to empty.

 Why is this a bad idea in terms of our original goals? Efficient Makes every permutation of the Deck possible Makes every permutation of the Deck equally likely

 A better idea Why?  For k = 51 down to 1, Generate a random integer r between 0 and k, Exchange cards[k] and cards[r].

 Use the file Shuffler.java to implement the efficient selection shuffle method You will be shuffling arrays of integers.  Shuffler.java also provides a 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 the implementation of the Deck class by coding each of the following: shuffle — implement this method using the algorithm previously described Deck constructor — This constructor receives three arrays as parameters. The arrays contain the ranks, suits, and point values for each card in the deck. The constructor creates an ArrayList, and then creates the specified cards and adds them to the list. For example, if ranks = {"A", "B", "C"}, suits = {"Giraffes", "Lions"}, and values = {2,1,6}, the constructor would create the following cards: ["A", "Giraffes", 2], ["B", "Giraffes", 1], ["C", "Giraffes", 6], ["A", "Lions", 2], ["B", "Lions", 1], ["C", "Lions", 6] and would add each of them to cards. The parameter size would then be set to the size of cards, which in this example is 6. Finally, the constructor should shuffle the deck by calling the shuffle method.

isEmpty — This method should return true when the size of the deck is 0; false otherwise. size — This method returns the number of cards in the deck that are left to be dealt. deal — This method “deals” a card by removing a card from the deck and returning it, if there are any cards in the deck left to be dealt. It returns null if the deck is empty. There are several ways of accomplishing this task. Here are two possible algorithms: Algorithm 1: Because the cards are being held in an ArrayList, it would be easy to simply call the List method that removes an object at a specified index, and return that object. Removing the object from the end of the list would be more efficient than removing it from the beginning of the list. Note that the use of this algorithm also requires a separate “discard” list to keep track of the dealt cards. This is necessary so that the dealt cards can be reshuffled and dealt again. Algorithm 2: It would be more efficient to leave the cards in the list. Instead of removing the card, simply decrement the size instance variable and then return the card at size. In this algorithm, the size instance variable does double duty; it determines which card to “deal” and it also represents how many cards in the deck are left to be dealt. This is the algorithm that you should implement.

 Once you have completed the Deck class, find DeckTester.java file  Add code in the main method to create three Deck objects and test each method for each Deck object.  Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method. You can use the Deck toString method to “see” the cards after every shuffle.

 There are many techniques devoted to correcting bugs in your program My favorite is running a debugger Setting break points Adding Watches  We will also look at a very simple java construct assert :  You will need to enable asserts in your project Right click project folder Properties  Run  VM options = –ea  Assertions should now be part of your test bed before you submit your work