- Rough Scoring Guide: AP Score : % correct
- 5 : 80%
- 4 : 60%
- 3 : 46%
- 2 : 36%
- AP CS A exam content
- Java Quick Reference Sheet
- This sheet will be provided to you during the exam.
- Keywords and Concepts
- Public vs Private
- public: visible outside the class (and to static methods)
- private: not visible outside the class (or to static methods).
- Void vs non-void
- void: does something but does not return anything
- Ex: void setName(String newName);
- non-void: returns a variable of the type declared in the method
- Ex: String getName();
- void: does something but does not return anything
- Declare
- stating a variable and the type of variable
- Ex: int x; Account a;
- Instantiate
- setting aside memory space for an object, i.e. running the constructor with the word “new”
- Ex: Account a = new Account();
- Initialize
- setting the initial value of an variable
- Ex: int x = 9;
- Object variables are often initialized in the constructor.
- Composition
- Composed of (“has a”).
- Ex: An array of integers. The array is composed of integers.
- Inheritance
- Parent / child class relationships
- Extends
- Polymorphism
- The ability of objects to change types and be related by inheritance
- Casting a parent class object as a child class.
- Overloading
- Methods with same name but different parameters in the same class.
- Ex: changeYear (int x); changeYear (String y)
- Overriding
- Methods with the same name and parameters, but in parent/child classes. Child class methods override the parent class methods.
- Ex: parent and child toString() methods
- Final
- Once set, the variable value cannot be changed.
- Static
- Variable: the variable has one value for all objects of that type.
- Method: the method can be called without having to create an object of the class
- Example
- Math library methods (random, abs, sqrt, pow) are static. You can call them directly.
- Math.pow(x,y);
- Math.abs(x);
- Scanner and Random library methods are not static. You have to create a Scanner or Random object and then call the method from that object.
- Scanner scan = new Scanner();
- scan.nextLine();
- Scanner.nextLine() won’t work.
- Math library methods (random, abs, sqrt, pow) are static. You can call them directly.
- Super
- access the parent class method
- This
- Refers to the class variable, not the local variable.
- Public vs Private
- Binary Numbers
- Bits and bytes. Know basically what they are.
- You should not have to do any binary to decimal conversions.
- Primitive Variables
- All lower case first letter. Upper case signifies an object.
- int: 4 bytes:
- .MAX_ and .MIN_ Value
- -2,147,483,648 to 2,147,483,647
- long: 8 bytes
- float (decimals): 4 bytes
- double: 8 bytes
- What happens when you go over the limit of an int or float?
- int x = 2147483647 + 1; will not thrown an error.
- x == -21474863648
- I’ll explain this in class.
- boolean: true or false
- char: single quotes. Double quotes are Strings.
- Math class
- .abs(): Absolute value
- .pow(x,y): x to the y power
- .random(): random double from 0 to 1
- Strings
- Methods and Manipulation
- Must know for the exam
- .substring
- .indexOf
- .equals
- .compareTo
- Not on the Java Subset sheet, but you should know.
- .concat
- .contains
- .ignoreCase and toLowerUpperCase
- Must know for the exam
- Strings are immutable
- You cannot change the value of a String once it is set. You can reassign the String variable to a new value, but you cannot change the value.
- Won’t work
- String name = “John”;
- name.concat (“ Doe);
- name + ” Doe”;
- Will work
- String name = “John”;
- name = name.concat (“ Doe);
- name = name + ” Doe”;
- name += ” Doe”;
- Methods and Manipulation
- Operators
- Increment
- By one: ++ (i++ or ++i)
- By amount: +=
- Multiply by amount *=
- Decrement
- By one – – (i- – or – -i)
- By amount -=
- Divide by amount: /=
- Modulus, remainder, %
- 16 % 3 == 1
- Increment
- Order of Operations
- Boolean logic
- Or ||
- And &&
- Equivalency ==
- The single “=” means set equal to. They will dock points in the FRQ if you mix these up as it changes the logic.
- DeMorgan’s Law: The distributive property of not !
- ! (a && b) == !a || !b
- ! (a || b) == !a && !b
- !( (x > y) && (z == w) ) is equivalent to ( (x <= y) || (z != w) )
- Random numbers
- Math Library
- Math. random()
- The only one needed for the AP Exam
- Double from 0 – 1.
- Multiply by a number to change the range.
- Add a number to change the lower bound.
- Random library
- Random rand = new Random();
- int or float within a range
- Won’t show up on MCQ, but you can use it on FRQ.
- Math Library
- Case or Switch Statements
- Loops
- While
- Do while
- For
- ForEach
- Variable scope
- Local vs. global (class) variables
- this. references the class variable
- Variables only exist within the area (scope) in which they are defined. Example:
- int x = 9;
- void doThis (int x) { x ++; }
- doThis (x);
- System.out.print (x);
- x == 9 because the print statement is outside the method. The x in the doThis method == 10, but this never actually changes the x outside the method.
- Primitive vs object variable changing
- x=y, change y, print x
- Primitives and Strings don’t change x (even though Strings are objects, they are immutable)
- Objects, arrays, and ArrayLists will change x
- x=y, change y, print x
- Error Messages and Exceptions
- ArithmeticException
- division by zero
- NullPointerException
- Trying to use methods in an object that has not been initialized.
- Ex: Account b; b.withrdraw (amount);
- ArrayIndexOutOfBoundsException
- Trying to access a location in an array that does not exist.
- IndexOutOfBoundsException
- Trying to access a location in an arrayList or other object that doesn’t exist.
- IllegalArgumentException
- Passing in the wrong variable type into a method.
- Ex. void setValue(int x){}; setValue(“zero”);
- ConcurrentModificationException
- Trying to modify an object when it is not allowed.
- Ex. Removing elements from an ArrayList while using a ForEach loop.
- NaN
- Not a Number
- Square root of negative number, etc.
- ArithmeticException
- Other Classes
- Wrapper Class
- ArrayLists cannot use primitive variables.
- The Wrapper classes are the object version of primitives. They use a capital letter.
- Integer, Float, Double, Boolean, Character, etc
- Strings don’t have a Wrapper class because they are objects, not primitive variables.
- Object Class
- The parent of all classes.
- This may show up in the MCQs.
- List Class
- The parent of ArrayLists. (Technically an Interface that inherits the Collections class.)
- Again, this may show up in the MCQs.
- Wrapper Class
- Free Response Question Tips
- Focus on the logic not the semantics.
- You only need to know and use the Java code covered in this class.
- You can use Java code not covered in this class, as long as it works. You don’t get any extra credit for it, though.
- You don’t get docked for inefficient code, only code that doesn’t work.
- You must use Java code, not Python, C++, or any other language.