[go: up one dir, main page]

0% found this document useful (0 votes)
16 views6 pages

Ap Exam Csa Crunch Sheet

The AP Exam CSA Crunch Sheet provides a comprehensive overview of key concepts in AP Computer Science A, covering topics such as primitive data types, object-oriented programming, boolean expressions, iteration, and arrays. It includes essential definitions, algorithms, and examples for each unit, aiding students in their exam preparation. The document serves as a quick reference guide for important programming principles and techniques relevant to the course.

Uploaded by

Cerise Tsoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Ap Exam Csa Crunch Sheet

The AP Exam CSA Crunch Sheet provides a comprehensive overview of key concepts in AP Computer Science A, covering topics such as primitive data types, object-oriented programming, boolean expressions, iteration, and arrays. It includes essential definitions, algorithms, and examples for each unit, aiding students in their exam preparation. The document serves as a quick reference guide for important programming principles and techniques relevant to the course.

Uploaded by

Cerise Tsoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

lOMoARcPSD|33456997

AP Exam CSA Crunch Sheet

AP Computer Science A (Design Science Middle College High School)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Cerise Tsoi (tsoicerise@gmail.com)
lOMoARcPSD|33456997

Unit 1: Primitive Data Unit 2: Using Objects


An object is a specific instance of a class with defined attributes
System.out.print and print leaves the cursor at the end of the line Object vs Class
A class is the formal implementation, or blueprint, of the attributes and
System.out.println and println moves the cursor to the next line
behaviors of an object
defaults:
int = 0 int  double Created as private at the top of the class. These are the properties of
int, double, boolean Object Data: Instance Variables
double = 0.0 double ! int the object
boolean = false
Default constructor: same name with no parameters. Assigns the
Integer.MAX_VALUE The maximum and minimum possible integer instance variables default values
Constructors and Object creation
Integer.MIN_VALUE value a variable can hold Parameter constructor: same name with parameters. Assigns the
instance variables to the parameters passed
Primitives (int, double boolean) store
Primitive vs Reference values and References (Strings and Objects) null used to indicate that a reference is not associated with any object
store memory locations
final variables create a constant
Defined by methods
final variables final classes prevent inheritance Object Behavior: Methods
Non-static methods are called through objects of the class
final methods prevent overriding
when a method is used on a null reference
+, -, *, /, % operators NullPointerException Dog fido = null;
fido.sit();
+=, -=, *=, /=, %=, ++, - multiple methods with the same name but a different signature
- compound operators Overloaded public double area(int side)
public double area(int length, int width)
int + int results in an int
Mixed expressions \”, \\, \n Escape sequences used in string literals to print special characters
int + double results in a double
Strings are immutable and can be treated like primitives
String first = “this”;
String in the java.lang package String second = first;
ArithmeticException Divide by 0
available by default Any changes made to the variable second will not affect the first
variable (not how other references behave)
length()
substring(int from, int to)
Round up: (int)(x + 0.5) substring(int from)
Rounding Know String methods
Round down: (int)(x – 0.5) indexOf(String str)
equals(String other)
compareTo(String other)
Explicit:
(int) 3.5  casts to an int
Trying to access an index that is not valid. Indexes go from 0 to
Casting (double) 3  casts to a double IndexOutOfBoundsException
word.length() – 1
Implicit:
1.0 * 3  casts to a double
Only compare same case letters
“A”.compareTo(“D”)  negative
Check for an even number num % 2 == 0 compareTo(String str)
“D”.compareTo(“A”)  positive
“A”.compateTo(“A”)  zero
Getting a single letter from a word substring(index, index + 1)
Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their
Wrapper Class: Integer and Double
corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double.
Static methods Methods that are not associated with an object behavior
int/double abs(int/double) pow(double base, double ex) sqrt(double x)
Math Class random()
To get a random integer between start and finish
random() width = finish – start + 1
(int)(Math.random() * width) + start

Downloaded by Cerise Tsoi (tsoicerise@gmail.com)


lOMoARcPSD|33456997

Unit 3: Boolean Expressions and if Statements Unit 4: Iteration


while(condition) { … }
==, !=, <,
Comparison operators between two ints or two doubles while loop Update the variable in the condition to avoid an infinite loop
>, <=, >=
if if(condition) { … } for loop for(initialization; condition; update) { }
Algorithm: Identify if an
if(condition) { .. } integer is or is not evenly if(number % divisor == 0)
if-else
else { … } divisible by another { /*evenly divisible */ }
integer
if(condition) { … }
Algorithm: Identify the
else if(condition) { … } number % 10  ones place
if-else if individual digits in an
else if(condition) {… } (number / 10) % 10  tens place
integer

if(condition) {
if(condition) { … } count = 0
else { … } Algorithm: Determine while(condition) {
} the frequency with if(criterion) {
Nested ifs
else { which a specific criterion count ++
if(condition) { … } is met }
else { … } }
}
A B A&&B A || B !A
T T T T F sum = 0;
Boolean
Algorithm: Compute a for(int i = 0; i < array.length; i++){
Operators & T F F T F sum += array[i];
sum, average, or mode
Truth Tables F T F T T }
F F F F T
“Distribution” of Boolean Operators
DeMorgan’s
!(A&&B) = !A || !B
Law
!(A||B) = !A && !B
do not use with references to Strings or Objects; these test
== and !=
aliasing with references

//count number of “a”s


//count how many “like”s in a sentence
Algorithm: Find String word = …;
String sentence = …;
count = 0;
if one or more Algorithm: Determine for(int i = 0; i < sentence.length() – 4; i++) {
for(int i = 0; i < word.length(); i++) {
substrings has a the number of substrings if(sentence.substring(i, i + 4).equals(“like”)){
if(word.substring(i, i + 1).equals(“a”)) {
particular that meet specific criteria count++;
count++;
property }
}
}
}
min = array[0];
minIndex = 0;
Algorithm: String original = …;
for(int i = 1; i < array.length; i++) {
Algorithm: Create a new String reversed = “”;
Determine a if(array[i] < min) {
min = array[i]; string with the characters for(int i = original.length(); i > 0; i--){
minimum or reversed += original.substring(i – 1, i);
minIndex = I; reversed
maximum value }
}
}

Downloaded by Cerise Tsoi (tsoicerise@gmail.com)


lOMoARcPSD|33456997

Unit 5: Writing Classes Unit 6: Arrays


public vs  public variables and methods can be accessed anywhere, and by Array creation and int[] arr = new int[10];
private any class. All variables are automatically public. access arr[5] = 9;
int arrayLength = arr.length;
 private variables and methods can only be accessed in the class
where they are contained.
data A pillar of OOP that protects data from being accessed or modified ArrayIndexOutOfBounds Arrays have indexes from 0 to arr.length – 1 and trying to
Exception
encapsulation by any part of a program, except with explicit calls to the accessor access any integer outside of this range will result in this error.
and mutator methods.
constructors  must be public and need to have the same name as the class Traversing Arrays for(int i = 0; i < arr.length; i ++) { … }
 default constructor sets all the instance variables to default values
 parameter constructors set the instance variables to values that are standard loop header to go through the array
passed to it
//, /* */, //single line comment Enhanced for loop for(int value : arr) { .. }
/** */ /*block of comments /** Java documentation lets you go through each value in the array arr. An enhanced for
that span several lines */ used on the AP exam */ loop DOES NOT let you modify the array elements; just look.
Pre and Post  precondition: condition that must be true just prior to the Swapping Algorithm //swapping two Strings in an array arr
Conditions execution. You do not have to check these in your program. //at indexes 3 and 6
String temp = arr[3];
 postcondition: condition that must always be true after the arr[3] = arr[6];
execution of a section of program code. arr[6] = temp;
Accessor public returnType getVariable() Algorithm: Determine if //count all the odd integers
Methods { return variable; } all elements have a count = 0;
particular property for(int i = 0; i < arr.length; i++) {
One for each variable that you will need to access from outside the if(arr[i] % 2 == 1) {
class. count++;
}
}
Mutator public void setVariable(variableType name) Algorithm: Access all count = 0;
Methods { variable = name; } consecutive pairs of for(int i = 0; i < arr.length – 1; i++) {
elements if(arr[i] == arr[i+1]){
One for each variable that you plan on modifying outside the class. count++;
}}
Static Methods Static variables are used as constants in your program: Algorithm: Determine //boolean changes to true if a duplicate is
and Variables static double PI = 3.1415926; the presence or absence found
of duplicate elements boolean found = false;
Static methods are not associated with an object and are not for(int i = 0; i < arr.length; i++) {
for(int k = i; k < arr.length; k++) {
inherited. They are called on using the class name they are located. if(arr[i] == arr[k]){
public static double calcAverage(int a, int b, int
found = true;
c)
}
}}
Local vs Local variables can only be used in the block of code that they are Algorithm: Shift or rotate //shifts the elements to the left
Global declared in. elements left or right in int temp = arr[0];
Variables Global variables are declared at the top of the class and can be used an array for(int i = 0; i < arr.length – 1; i++) {
arr[i] = arr[i+1];
at any point in the class. }
arr[arr.length – 1] = temp;
this keyword Within a non-static method or a constructor, the keyword this is a Algorithm: Reverse the for(int i = 0; i < arr.length; i++) {
reference to the current object—the object whose method or order of the elements in int j = arr.length – i – 1;
an array int temp = arr[i];
constructor is being called
arr[i] = arr[j];
arr[j] = temp;
}

Downloaded by Cerise Tsoi (tsoicerise@gmail.com)


lOMoARcPSD|33456997

Unit 7: ArrayList Unit 8: 2D Arrays


Constructing ArrayList<Integer> myGrades = new 2D arrays are int[][] matrix = new int[3][5]
ArrayLists ArrayList<Integer>(); stored as arrays
of arrays

ArrayList methods myGrades.size() Accessing matrix[row index][column index]


myGrades.add(obj) elements of 2d [0][0] [0][1] [0][2] [0][3] [0][4]
myGrades.add(index, obj) arrays [1][0] [1][1] [1][2] [1][3] [1][4]
myGrades.get(index)  returns obj [2][0] [2][1] [2][2] [2][3] [2][4]
myGrades.set(index, obj)  returns old obj [3][0] [3][1] [3][2] [3][3] [3][4]
myGrades.remove(index)  returns removed obj [4][0] [4][1] [4][2] [4][3] [4][4]
Traversing for(int i = 0; i < myGrades.size(); i++) { Traversing Row for(int r = 0; r < matrix.length; r++) {
ArrayLists System.out.println(myGrades.get(i)); Major Order for(int c = 0; c < matrix[0].length; c++) {
} //…
}}
Concurrent for(int item: myGrades) { Traversing for(int c = 0; c < matrix[0].length; c++) {
Modification myGrades.remove(item); Column Major for(int r = 0; r < matrix.length; r++) {
Exception } Order //…
}}
Standard algorithms Use list.size() instead of array.length Nested for each for(int[] row: matrix) { //always row major
that are used with Use list.get(i) instead of array[i] loops for(int element: row) {
1D arrays Use list.set(i, obj) instead of array[i] = obj //…
}}
Linear Search public static int searchLinear(int num, int[] Algorithm: //count all the odd integers
list) { Determine if all count = 0;
for(int i = 0; i < list.length; i++){ elements have a for(int i = 0; i < matrix.length; i++){
if(num == list[i]) for(int k = 0; k < matrix[0].length; k++){
return i;
particular if(matrix[i][k] % 2 == 1) {
} property count++;
return -1; }
} }}
Selection Sort Algorithm: for(int i = 0; i < matrix.length; i++){
20 31 6 17 13 for(int k = 0; k < matrix[0].length; k++){
Printing off the
Selects the smallest elements of a 2D System.out.print(matrix[i][k]);
6 31 20 17 13 }
element to the right array System.out.println();
of the current index }
and swaps them. 6 13 20 17 31

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
Downloaded by Cerise Tsoi (tsoicerise@gmail.com)
lOMoARcPSD|33456997

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(); b = a; a = b;
use the keyword "super." in front of the method name. LapTop b = new LapTop(); c = b; b = a;
• You can only extend one super class. Computer c = new Computer(); c = a; a = c;
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)

public class Point { public boolean equals(Point other) {


private int x, y; return this.x == other.x && this.y == other.y;
/*constructors and methods not shown */ }
public String toString() {
return x + ", " + y;
}
}

(C) Goldie's Math Emporium, LLC

Downloaded by Cerise Tsoi (tsoicerise@gmail.com)

You might also like