Review for the AP CS Exam

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Advertisements

Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Arrays And ArrayLists - S. Kelly-Bootle
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Information and Computer Sciences University of Hawaii, Manoa
1 Review for the AP CS Exam  Object Oriented Program Design – Program Design Read and Understand class specifications and relationships among the classes.
ArrayList, Multidimensional Arrays
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 ArrayList Starring: Purse, Sr. Co-Starring: Purse, Jr. Poe Numbers.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Sorting Mr. Jacobs.
Lecture 10 Collections Richard Gesick.
Lecture 20: Wrapper Classes and More Loops
Searching.
Data Structures I (CPCS-204)
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Review for the AP CS Exam
CS 215 Final Review Ismail abumuhfouz Fall 2014.
(like an array on steroids)
Methods Chapter 6.
Recitation 13 Searching and Sorting.
Agenda Warmup AP Exam Review: Litvin A2
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Interfaces and Inheritance
Chapter 17 Object-Oriented Data Structures
Top Ten Words that Almost Rhyme with “Peas”
CS313D: Advanced Programming Language
Computer Science II Exam 1 Review.
Algorithm design and Analysis
Java - Data Types, Variables, and Arrays
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Can store many of the same kind of data together
Grouped Data Arrays, and Array Lists.
Sub-Quadratic Sorting Algorithms
Stacks Abstract Data Types (ADTs) Stacks
ArrayLists 22-Feb-19.
Sorting Chapter 8.
Introduction to Data Structure
Review of Previous Lesson
Searching.
Java Programming Language
ArrayLists 27-Apr-19.
Generics, Stack, Queue Based on slides by Alyssa Harding
CS210- Lecture 3 Jun 6, 2005 Announcements
CSE 143 Lecture 21 Advanced List Implementation
Arrays.
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

Review for the AP CS Exam Object Oriented Program Design Program Design Read and Understand class specifications and relationships among the classes (has a and is a relationships) Decompose a problem into classes, define relationships and responsibilities of those classes

Program design Top down approach Bottom up approach Make a list of all of the things that a program must accomplish Each of those things becomes its own module (perhaps a method or perhaps a class with several methods) Bottom up approach Starts with specific, detailed way of achieving something(ie recursive area fill or interacting with a piece of hardware) Builds program up around that detail

Class design Class Design Design and implement a class Design an interface (remember that an interface has only methods, no data) Extend a class using inheritance (Design question) Remember that any class that implements an interface must implement all methods in the interface Use extends and implements appropriately declare constructors and methods with meaningful names appropriate parameters appropriate return types (remember constructors do not have return types)

Class design Class Design appropriate declaration of methods as private or public all data should be private

Program Implementation Classes that fill common needs should be built so they can be reused by other programs. OO design is an important part of program implementation Implementation Techniques Encapsulation and information hiding Procedural abstraction

Program Implementation Programming constructs Primitives vs references Remember that references must be associated with an object Beware of aliasing:when two references point to the same object

Floating Point Number Probs Here is an explanation from borland’s web site; Round-Off Problems : One of the problems with floating point numbers is round-off. Round off errors occur when attempting to represent certain numbers in any number base. For example, 1/3 is not exactly representable in base ten, while 1/10th is easily representable. But since we're dealing with computers, we are specifically in base two numbers. As opposed to base ten, 1/10th is not exactly representable in base two. For example, the fractional portions of base two are: 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 The numbers 1/2, 1/4, 1/8, all powers of two, are exactly representable in a computer. But since 1/10 lies between 1/8 and 1/16, it is not exactly representable using binary notation. So internally the computer has to decide which fractional binary portions to add together to sum close to 1/10. For example: 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 0 0 0 1 1 1 0 0 0 this adds up to: 0.1093 which is close to 0.1000 but could easily be rounded to .11 so the computer internal algorithm must try to find another combination of binary fractions which come closer to 0.1000 When it's internal algorithm is satisfied, it will have a number which is CLOSE to 1/10th but not EXACT. This inexactness is known as ROUND-OFF error.

Constant Declarations final double WIDTH = 1.1619; Use these whenever you are using a numeric value that you type several places in a program Variable declarations Scope of variables Shadowing problem: Most local variable gets precedence

Method declarations Method declarations A method’s signature consists of name, parameter types and order of parameters Overloaded methods have the same name but different parameters Constructors are often overloaded What is a static method?a method that does not require an instance of the class to be created What is a private method?accessible only to class member methods

Interface Declarations Interfaces A collection of methods that a class must implement An interface you should know interface java.lang.Comparable int compareTo(Object other) // return value < 0 if this is less than other // return value = 0 if this is equal to other // return value > 0 if this is greater than other

Interface Declarations Example Double x = new Double(1.618); Double y = new Double(3.14159); //Write a statement that compares x to y and prints out “phi is less than pi” if (x.compareTo(y) < 0) { System.out.println(“phi is less than pi”); }

Math Operators Math ops +,-,/,*,% two types of division type casting (int) and (double) Students are expected to understand "truncation towards 0" behavior as well as the fact that positive floating-point numbers can be rounded to the nearest integer as (int)(x + 0.5), negative numbers as (int)(x - 0.5) Math.abs(double x), Math.sqrt(double x), Math.pow(double base, double exp)

Control Control techniques Methods break; //breaks out of nearest loop or case conditional (if, if else, switch case optional) iteration (for and while only required) recursion Powerful technique when a method invokes itself Must have some sort of stopping or base case Remember to use McCarthy method or something similar to trace many method calls decrease efficiency

For each loop used heavily The for each loop will probably be used heavily in the exam ArrayList<String> songs = some list Write a for each loop to print the songs for(String s: songs) System.out.println(s);

For each loop used heavily int[] nums = some array of integers Write a for each loop to find the average as a double int sum = 0; for( int x: nums) { sum += x; double avg = (double)sum/nums.length;

Program Analysis Program Analysis Testing/Debugging Test Libraries and Classes in isolation Identify boundary cases Understand error handling ArithmeticException Divide by zero IndexOutOfBoundsException ClassCastException Attempt to cast to subclass of which it is not an instance NullPointerException Needed an object, got the shaft!!! IllegalArgumentException

Analysis of Algorithms Algo Analysis Big Oh Notation Asymptotic growth limit O(n) linear O(n2) quadratic (two dim array, nested for) O(logn) divide and conquer(assume base 2, this is comp sci man!!) O(1) constant time O(nlogn) better sorting algo’s including logn= find where this element belongs and n to move each element into position

Analysis of Algorithms Data Search Array Unordered O(n) Array Ordered O(log n) -> binary search ArrayList

Data Structures int : 32 bit signed integer double: 64 bit signed floating point boolean: true or false

Classes and Class Hierarchies Remember chess piece example abstract base class with abstract method isValidMove() abstract Piece abstract isValidMove Pawn isValidMove Knight isValidMove Queen isValidMove

Classes and Class Hierarchies Children must redefine this method Example of dynamic binding or late binding Calls to isValidMove are polymorphic

Classes and Class Hierarchies “Cast down the hierarchy” Ok to cast from Object to Integer assuming class is an Integer Ok to cast from piece to pawn if you know the piece is a pawn Children inherit all methods and classes (but cannot play with ancestor’s private vars and methods) Also, static methods and vars are one per class

One/Two dimensional arrays Once created a certain size, fixed use .length, the member var Use [ ] to access elements starting at 0 …length-1 0 and <length works for boundaries Use array[0].length for column dimensions Use r and c for loop control vars for ease of reading Must create individual objects if array of references

ArrayList int size() boolean add(Object x) Object get(int index) Object set(int index, Object x) // replaces the element at index with x // returns the element formerly at the specified position void add(int index, Object x) // inserts x at position index, sliding elements // at position index and higher to the right // (adds 1 to their indices) and adjusts size Object remove(int index) // removes element from position index, sliding elements // at position index + 1 and higher to the left // (subtracts 1 from their indices) and adjusts size

Stacks FIFO data structures Good when you need to untangle some type of operations that have to be suspended and returned to

Stack interface public interface Stack { // postcondition: returns true if stack is empty, false otherwise boolean isEmpty(); // precondition: stack is [e1, e2, ..., en] with n >= 0 // postcondition: stack is [e1, e2, ..., en, x] void push(Object x); // precondition: stack is [e1, e2, ..., en] with n >= 1 // postcondition: stack is [e1, e2, ..., e(n-1)]; returns en // throws an unchecked exception if the stack is empty Object pop(); // precondition: stack is [e1, e2, ..., en] with n >= 1 // postcondition: returns en // throws an unchecked exception if the stack is empty Object peekTop(); }

Strings Keep in mind substring first you want, first you don’t want. class java.lang.String implements java.lang.Comparable int compareTo(Object other) // specified by java.lang.Comparable boolean equals(Object other) int length() String substring(int from, int to) // returns the substring beginning at from // and ending at to-1 String substring(int from) // returns substring(from, length()) int indexOf(String s) // returns the index of the first occurrence of s; // returns -1 if not found Keep in mind substring first you want, first you don’t want. Also, there is a substring with only one argument, starts at that char and goes to end substring probably a better idea that charAt, since chars not in subset

Math.random() All of the random() calls are made using Math.random() This gives you a number between 0 and 1(non inclusive) Example, use Math.random() to generate a # for choosing an element from an ArrayList called locations int position = (int)(Math.random()*locations.size());

Sorting Quadratic Sorts O(n2) Selection Insertion Bubble Choose the maximal or minimal element by a loop and then swap that with the current place you are moving through Minimizes swaps Insertion Pick an element, shift everyone out of the way to make room for it (Church pew shuffle) Bubble Nested for loops allow one element to “bubble” to the front or back each time

Sorting nlogn Sorts Merge Start breaking up your list into smaller lists and merge them together Requires temporary storage area of size n for merging into

Sorting Sort Best Average Worst Selection O(n2) Insertion Merge O(nlog n)

Thats all folks!!!! Neo journeyed to the machine city and faced off with Smith Frodo took the ring to the Mountain of Doom On May 3, you will accomplish your mission, the APCS exam It is your purpose!