Download presentation
Presentation is loading. Please wait.
Published byAlvin Nicholson Modified over 8 years ago
1
AP Computer Science Exam Review 2012
2
Number Systems 1001010 Binary
3
Number Systems 1001010 2^6 2^5 2^4 2^3 2^2 2^1 2^0 64 32 16 8 4 2 1 +64 + 0 + 0 + 8 + 0 + 2 + 0 = 74
4
Number Systems EF7 Hexadecimal
5
Number Systems EF7 Hex 16^2 16^1 16^0 256 16 1 + 14(256) + 15(16) + 7(1) = 3831
6
Number Systems EF7 1110 1111 0111 Hexadecimal Binary
7
Compilers
8
Primitive Data Types int x = 5 / 2; int x = 5.0 / 2; double x = 5 / 2; double x = 5.0 / 2; double x = (double) 5 / 2; double x = (double) (5 / 2);
9
Primitive Data Types int x = 5 / 2; int x = 5.0 / 2; double x = 5 / 2; double x = 5.0 / 2; double x = (double) 5 / 2; double x = (double) (5 / 2); 2 Err 2.0 2.5 2.0
10
If vs. If-Else PROGRAM A if (mark < 50) grade = “F”; if (mark < 65) grade = “C”; if (mark < 73) grade = “C+”; if (mark < 86) grade = “B”; if (mark < 100) grade = “A”; else Grade = “A+”; PROGRAM A if (mark < 50) grade = “F”; else if (mark < 65) grade = “C”; else if (mark < 73) grade = “C+”; else if (mark < 86) grade = “B”; else if (mark < 100) grade = “A”; else Grade = “A+”;
11
Short Circuit Evaluation if ( x != 0 && 10/x > 1) {... } Does this cause a run time error if x = 0?
12
Dangling Else if (n > 0) { if (n % 2 == 0) System.out.println(n); } else System.out.println(n + “ is not positive”);
13
For Loop Traces for (int i = 5; i > 0; i-- ) { for (int j = i; j > 0; j--) { System.out.print(j); } System.out.println(""); }
14
One Dimensional Arrays int[] arr = new int[5]; 0 1 2 3 4 Instantiation Getting lengths arr.length; arr[4] int[] arr = {1,2,3,4,5}; Initialization for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + “ “); } Traversal for (int i : arr) { System.out.print(arr[i] + “ “); }
15
Two Dimensional Arrays int[][] matrix = new int[3][5]; Row count:matrix.length; Col count: matrix[0].length; for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[0].length; col++) { System.out.print(matrix[row][col] + “ “); } System.out.println(“”); } Traversal Getting lengths Instantiation 012012 0 1 2 3 4 matrix[0][4 ] matrix[2][2 ] Row Col int[][] matrix = {{1,2},{3,4}}; Initialization
16
ArrayLists List lst = new Arraylist ; lst.add(10); lst.add(11); list.add(12); Elements: lst.size(); for (Integer i : lst) { System.out.print(i + “ “); } for (int i = 0; i < lst.size(); i++) { System.out.print(lst.get(i) + “ “); } Traversal Getting lengths Instantiation 0 1 2 lst.get(1); lst.get(2);
17
for (int i = 0; i < lst.size(); i++) { if (lst.get(i) > 10) { lst.remove(i); } Removing From Lists int i = 0; while (i < lst.size()){ if (lst.get(i) > 10) lst.remove(i); else i++; } Wrong Way: Correct Way:
18
ArraysArrayLists ● Fixed size ● Can contain primitive and object elements ● Can grow and shrink ● Can only contain object elements; must use wrapper classes to deal with primitives if (lst.get(i) > 10) lst.add(10); auto-boxing auto-unboxing
19
String Methods String s1 = new String(“Hello”); String s1 = “Hello”; s1.length; s1.substring(1); s1.substring(1,2); s1.substring(1,1); s1.compareTo(“Good-bye”); s1 == “Hello”; HELLO 0 1 2 3 4
20
String Methods String s1 = new String(“Hello”); String s1 = “Hello”; s1.length; s1.substring(1); s1.substring(1,2); s1.substring(1,1); s1.compareTo(“Good-bye”); s1 == “Hello” HELLO 0 1 2 3 4 + int invoking string is > - int invoking string is < 0 strings are equal Returns Meaning of Returned Value 5 ELLO E +1 NO!! Use s1.equals(“Hello”);
21
Static vs. Nonstatic Methods class Stat { public static void bar() { … } public static void baz() { … } } class Client { public static void main (String[] args) { Stat.bar(); Stat.baz(); }
22
Static vs. Nonstatic Methods class Marine extends Unit { public void move(int x, int y) { … } public void baz() { … } } class Client { public static void main (String[] args) { Marine n = new Marine(); n.move(4,5); n.baz(); }
23
Static vs. Nonstatic Methods Are the trigonometry functions in the Math class static or nonstatic? double x = Math.sin(direction) * speed; double y = Math.cos(direction) * speed; Math m = new Math(); m.sin(); Math p = new Math(); p.sin()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.