AP Exam CSA Crunch Sheet - Version 3
AP Exam CSA Crunch Sheet - Version 3
6 13 17 20 31
Insertion Sort Unit 10: Recursion public static int factorial(int num){
20 31 6 17 13 if(num >= 1)
return num * factorial(num - 1);
Current index looks A recursive method calls itself, and each call has its own local else
“backwards” 20 31 6 17 13 variables. Remember to include a BASE CASE! return 1;
through the array to }
find where it should 6 20 31 17 13
Step 1: Assign the low index to 0 and the high index to the highest index. ∎ Written recursively
Binary Search
Step 2: While the lowest index is less than or equal to the highest index, find the middle index.
∎ Don't need to know the code
Merge Sort
be inserted at. Step 3: If you find the value of the number at the middle index, return it.
Elements are shifted 6 17 20 31 13 Otherwise, check to see if that value is higher than the middle element. ∎ Fast on large datasets
from the point of If it is, look through the higher part of the array by making your low index ∎ Breaks down an array into
equal to the middle index. single arrays; rebuilds the arrays
insertion. 6 13 17 20 31 Otherwise, look through the lower half of the array by switching the high
index to the midpoint.
into a single sorted array
public class TechTool Cloaking
Legal Illegal
{ /* implementation not shown */ } TechTool t1 = new Computer(); Computer c2 = new TechTool();
public class Computer extends TechTool TechTool t2 = new LapTop(); LapTop L1 = new Computer();
Computer c1 = new LapTop(); LapTop L2 = new TechTool();
{ /* implementation not shown */ }
public class LapTop extends Computer Useful for ArrayLists:
ArrayList<TechTool> list = new ArrayList<TechTool>();
{ /* implementation not shown */ } list.add(new TechTool());
list.add(new Computer());
list.add(new LapTop());
• A subclass inherits all instance variables and public
methods from their super class. Aliasing
• Constructors and private methods are NOT inherited. Example Legal Illegal
• To call explicitly on a super's methods or constructors, TechTool a = new TechTool(); a = b; b = a;
use the keyword "super." in front of the method name. Computer b = new Computer(); b = c; c = b;
• You can only extend one super class. LapTop c = new LapTop(); a = c; c = a;
Polymorphism • object – An object can have things it knows (attributes) and things it can do (methods). An object is created
• Polymorphism is a process in which a call to an overridden by a class.
method is resolved at runtime. • class – A class defines what all objects of that class know (attributes) and can do (methods).
• An overridden method is called through the reference variable of • parent class (super class) – the class that another class is inheriting from
Unit 9: Inheritance
a superclass (whatever type the object is DECLARED as, that • child class (subclass) – the class that is doing the inheriting. It inherits access to the object instance
method needs to be in that class or a parent of that class) at variables and methods in the parent class. Private instance variables still have to be called with accessor.
compile time. The program will not compile if this is not satisfied. Private methods are not inherited.
• When the program runs, the method that runs is determined from
what the object was CREATED as. • overridden method – A child class can have the same method signature (method name and parameter list)
as a parent class. Since methods are executed starting with the class that created the object (think: what
TechTool t2 = new LapTop(); constructor did I use?), that method will be called instead of the inherited parent method, so the child method
t2.toString(); overrides the parent method.
This will check to make sure that toString() is in the • overloaded method – At least two methods with the same name but different parameter lists. The parameter
TechTool class. If it is, it will compile. Then, when the program lists can differ by the number of parameters and/or the types.
is executed, it will look in the LapTop class first for the public static double area(int side)
toString() method and run it if it find it there. public static double area(int length, int width)
Object Superclass: All classes in Java extend the Object class.
//returns a String describing the object //returns true if this object is equal to another
//default implementation: ClassName@MemoryAddress //default implementation only checks for reference equality
public String toString() public boolean equals(Object other)