Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ap_comp_sci_a [2024/04/11 06:29] mrdoughap_comp_sci_a [2024/04/20 05:37] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== AP CSA Study Guide ====== ====== AP CSA Study Guide ======
 +Curly brackets alignment is a bit janky, not fully accurate but it shouldnt affect consumibility of notes
  
 ===== Unit 1 – Primitive Types ===== ===== Unit 1 – Primitive Types =====
Line 118: Line 119:
     - + (addition)     - + (addition)
  
-  o - (subtraction) o * (multiplication) o / (division) +o - (subtraction) o * (multiplication) o / (division) 
  
   * Ex: 20 % 8   * Ex: 20 % 8
  
-  %%//%%returns 2 (NOT 2.5) o % (modulus) +%%//%%returns 2 (NOT 2.5) o % (modulus) 
  
   * Gives you the remainder   * Gives you the remainder
Line 134: Line 135:
   * Integer division (divisor and dividend are both integers) results in an integer output/quotient.   * Integer division (divisor and dividend are both integers) results in an integer output/quotient.
  
-  o You can control the type (int or double) of output by casting the operands.+o You can control the type (int or double) of output by casting the operands.
  
   * Example: (int) 6.0/8 = 0 (double) 6/8 = 0.75   * Example: (int) 6.0/8 = 0 (double) 6/8 = 0.75
Line 146: Line 147:
   * Relational Operators o == (equal to)   * Relational Operators o == (equal to)
  
-  o != (not equal to) o   (greater than) o < (less than) o  = (greater than or equal to) o <= (less than or equal to) +o != (not equal to) o (greater than) o < (less than) o >= (greater than or equal to) o <= (less than or equal to) 
  
   * Logical Operators   * Logical Operators
Line 193: Line 194:
     * !. ++. --     * !. ++. --
     * ,/,%     * ,/,%
-    *  , <,  =, <=+    * >, <, >=, <=
     * %%==%%, !=     * %%==%%, !=
     * &&     * &&
Line 218: Line 219:
     - System.out.print     - System.out.print
  
-  o System.out.println+o System.out.println
  
   - System class -- displays output to the screen.   - System class -- displays output to the screen.
Line 238: Line 239:
     - constant     - constant
  
-  Answer: A final dataType name = value +Answer: A final dataType name = value 
  
 ===== Unit 2 – Using Objects ===== ===== Unit 2 – Using Objects =====
Line 355: Line 356:
     - Scope of a variable or method means that it’s in an area where the identifier is     - Scope of a variable or method means that it’s in an area where the identifier is
  
-  visible and can be easily accessed+visible and can be easily accessed
  
   - What does a scope consist of?   - What does a scope consist of?
Line 425: Line 426:
     * stringOne. compareTo (stringTwo) < 0     * stringOne. compareTo (stringTwo) < 0
       * stringOne precedes stringTwo in a dictionary       * stringOne precedes stringTwo in a dictionary
-    * stringOne. compareTo (stringTwo)   0+    * stringOne. compareTo (stringTwo) 0
       * stringTwo precedes stringOne in a dictionary       * stringTwo precedes stringOne in a dictionary
     * stringOne. compareTo (stringTwo) == 0     * stringOne. compareTo (stringTwo) == 0
Line 451: Line 452:
   - double (double value) o double doubleValue()   - double (double value) o double doubleValue()
  
-  o int compareTo (Double other)+o int compareTo (Double other)
  
   - boolean equals (Object obj) o String toString()   - boolean equals (Object obj) o String toString()
Line 461: Line 462:
   * Methods you need to know in the Math Class o static int abs(int x)   * Methods you need to know in the Math Class o static int abs(int x)
  
-  o static double abs(double x) o static double pow(double base, double exp) o static double sqrt(double x) o static double random() +o static double abs(double x) o static double pow(double base, double exp) o static double sqrt(double x) o static double random() 
  
 ===== Unit 3 – Boolean Expressions ===== ===== Unit 3 – Boolean Expressions =====
Line 485: Line 486:
   * An if statement tells the computer that if a condition is true, then it should execute the block of code within the function.   * An if statement tells the computer that if a condition is true, then it should execute the block of code within the function.
  
-  o Most basic control flow statement o If it’s false, the computer should skip the code and continue with the rest of the program. +o Most basic control flow statement o If it’s false, the computer should skip the code and continue with the rest of the program. 
  
   * Control flow statements   * Control flow statements
Line 537: Line 538:
   * De Morgan’s Laws   * De Morgan’s Laws
  
-  - not (a and b) -  (not a) or (not b)+  - not (a and b) -(not a) or (not b)
  
-  - not (a or b) -  (not a) and (not b)+  - not (a or b) -(not a) and (not b)
  
   * How De Morgan’s Laws are expressed in programming languages! o !(a && b) is equivalent to !a || !b   * How De Morgan’s Laws are expressed in programming languages! o !(a && b) is equivalent to !a || !b
  
-  o !(a || b) is equivalent to !a && !b+o !(a || b) is equivalent to !a && !b
  
   * Negating boolean expressions   * Negating boolean expressions
  
   - You can do this with relational operators   - You can do this with relational operators
-    - <,  , or ==+    - <, >, or ==
  
   * Truth Tables are used to prove boolean expressions.   * Truth Tables are used to prove boolean expressions.
Line 575: Line 576:
     * stringOne. compareTo (stringTwo) < 0     * stringOne. compareTo (stringTwo) < 0
       * stringOne precedes stringTwo in a dictionary       * stringOne precedes stringTwo in a dictionary
-    * stringOne. compareTo (stringTwo)   0+    * stringOne. compareTo (stringTwo) 0
       * stringTwo precedes stringOne in a dictionary       * stringTwo precedes stringOne in a dictionary
     * stringOne. compareTo (stringTwo) == 0     * stringOne. compareTo (stringTwo) == 0
Line 616: Line 617:
     - Loop stops once the condition is false     - Loop stops once the condition is false
  
-  * Example of a while loop+>  * Example of a while loop
  
-  public class Test { public static void main(String[] args) { int x = 10; while (x   0) { System.out.println(x); x = x - 2; } } } +   public class Test { public static void main(String[] args)  
 + 
 +  int x = 10;  
 +  while (x 0)  
 +{ 
 +  System.out.println(x); x = x - 2;  
 +     
 +   
 +
  
 **while Loops and for Loops (continued)** **while Loops and for Loops (continued)**
Line 624: Line 633:
   * Example of a for loop   * Example of a for loop
  
-  public class Test { public static void main(String[] args) { for (int x = 3; x   0; x--) { System.out.println(x); } } } +public class Test { 
 +     public static void main(String[] args) { 
 +        for (int x = 3; x 0; x--)  
 +          
 +           System.out.println(x);  
 +          
 +         
 +   
  
   * Body of the loops   * Body of the loops
Line 630: Line 646:
   - A single statement after “while” or “for”   - A single statement after “while” or “for”
  
-  OR+OR
  
   - Block of code between the opening and closing curly braces.   - Block of code between the opening and closing curly braces.
Line 638: Line 654:
   * Loops are usually incorporated in: o String traversals   * Loops are usually incorporated in: o String traversals
  
-  o String processing+o String processing
  
   - The code processes a string by each character.   - The code processes a string by each character.
Line 648: Line 664:
     - int length()     - int length()
  
-  o int indexOf(String Str)+o int indexOf(String Str)
  
   - String substring (int from, int to)   - String substring (int from, int to)
Line 673: Line 689:
   * Example of a nested loop:   * Example of a nested loop:
  
-  public class NestedLoops { public static void main(String[] args) { for (int row = 1; row <= 3; row++) { for (int col = 1; col <= 5; col++) { System.out.print("*"); } System.out.println();+public class NestedLoops  
 + 
 +public static void main(String[] args)  
 +   
 +  for (int row = 1; row <= 3; row++)  
 +     
 +    for (int col = 1; col <= 5; col++)  
 +       
 +      System.out.print("*");  
 +         
 +        System.out.println();  
 +
  
-  } } +} } 
  
 **Informal Code Analysis** **Informal Code Analysis**
Line 737: Line 764:
   * Example of a class: public class Name {   * Example of a class: public class Name {
  
-  private String first; private String last; public Name(String theFirst, String theLast) { first = theFirst; last = theLast; } +private String first;  
 +private String last;  
 +public Name(String theFirst, String theLast)  
 +   
 +    first = theFirst; last = theLast;  
 +  
  
-  }+}
  
   * Keywords public and private are used for: o Classes   * Keywords public and private are used for: o Classes
  
-  o Data o Constructors o Methods +o Data o Constructors o Methods 
  
 **Constructors** **Constructors**
Line 803: Line 835:
     * @param y the y position to place the snake     * @param y the y position to place the snake
  
-  %%*/%% public Snake(int x, int y) { xPos = x; yPos = y; } +%%*/%% public Snake(int x, int y) { xPos = x; yPos = y; } 
  
 **Accessor Methods** **Accessor Methods**
Line 867: Line 899:
   * Static variables and methods use the keyword static. o Must be before header or declaration   * Static variables and methods use the keyword static. o Must be before header or declaration
  
-  o Can be public or private+o Can be public or private
  
   * Static variables   * Static variables
Line 919: Line 951:
   * When an array is created using the keyword new, all of its elements are initialized with a specific value based on the type of elements:   * When an array is created using the keyword new, all of its elements are initialized with a specific value based on the type of elements:
  
-  o Elements of type int are initialized to 0 o Elements of type double are initialized to 0.0 o Elements of type boolean are initialized to false o Elements of a reference type are initialized to the reference value null. No objects are automatically created. +o Elements of type int are initialized to 0 o Elements of type double are initialized to 0.0 o Elements of type boolean are initialized to false o Elements of a reference type are initialized to the reference value null. No objects are automatically created. 
  
   * Square brackets %%([%% ]) are utilized to get to and adjust a component in an Array utilizing a list. The indexed value, for instance array[index], can be utilized anywhere a normal variable can be utilized, for instance to get or assign any values or variables.   * Square brackets %%([%% ]) are utilized to get to and adjust a component in an Array utilizing a list. The indexed value, for instance array[index], can be utilized anywhere a normal variable can be utilized, for instance to get or assign any values or variables.
Line 931: Line 963:
   * Traversing an array with an indexed for loop or while loop expects components to be accessed with their indices.   * Traversing an array with an indexed for loop or while loop expects components to be accessed with their indices.
  
-  public class OffByone { public static void main(String[] args) { int[ ] scores = { 10, 9, 8, 7, 6}; +public class OffByone  
 +   
 +    public static void main(String[] args)  
 +       
 +         int[ ] scores = { 10, 9, 8, 7, 6}; 
  
   * Make this loop print out all the scores! for (int i = 1; i <= scores.length; i++)   * Make this loop print out all the scores! for (int i = 1; i <= scores.length; i++)
  
-  { System.out.println( scores[i] ); } + 
 +System.out.println( scores[i] );  
 +
  
-  } } +} } 
  
 **Enhanced for Loop for Arrays** **Enhanced for Loop for Arrays**
Line 981: Line 1019:
   * The ArrayList constructor ArrayList() develops a vacant rundown of size 0.   * The ArrayList constructor ArrayList() develops a vacant rundown of size 0.
  
-  * Java permits the nonexclusive sort ArrayList<E , where the conventional kind E determines the sort of the components, such as String or Integer. Without it, the sort will be Object.+  * Java permits the nonexclusive sort ArrayList<E>, where the conventional kind E determines the sort of the components, such as String or Integer. Without it, the sort will be Object.
  
-  * ArrayList< is favored over ArrayList since it permits the compiler to discover mistakes that would some way or another be found at run-time.+  * ArrayList<Eis favored over ArrayList since it permits the compiler to discover mistakes that would some way or another be found at run-time.
  
-  * When ArrayList< is indicated, the kinds of the reference boundaries and return type when utilizing its techniques are type E.+  * When ArrayList<Eis indicated, the kinds of the reference boundaries and return type when utilizing its techniques are type E.
  
   * ArrayLists can't hold primitive data types such as int or double, so you should utilize the wrapper classes Integer or Double to place numerical values into an ArrayList.   * ArrayLists can't hold primitive data types such as int or double, so you should utilize the wrapper classes Integer or Double to place numerical values into an ArrayList.
Line 1057: Line 1095:
   * The Code of Ethics and Professional Conduct is written by Association for Computing Machinery (ACM).   * The Code of Ethics and Professional Conduct is written by Association for Computing Machinery (ACM).
  
-  o “Contribute to Society and human well-being.” o “Avoid harm to others.” o “Respect the privacy of others.” o “Give proper credit for intellectual property.” +o “Contribute to Society and human well-being.” o “Avoid harm to others.” o “Respect the privacy of others.” o “Give proper credit for intellectual property.” 
  
   - “Access computing and communication resources only when authorized to do so.”   - “Access computing and communication resources only when authorized to do so.”
Line 1064: Line 1102:
     - Email scams: Scammers send you emails which make you respond with personal     - Email scams: Scammers send you emails which make you respond with personal
  
-  information or click on certain dangerous links.+information or click on certain dangerous links.
  
   - Privacy settings: Privacy settings and the complexity of your passwords   - Privacy settings: Privacy settings and the complexity of your passwords
  
-  determines whether you are volunteering your personal information to be out there or keep your information safe.+determines whether you are volunteering your personal information to be out there or keep your information safe.
  
   - Sharing passwords: DO NOT share your password (even with your closest friends or partner).   - Sharing passwords: DO NOT share your password (even with your closest friends or partner).
Line 1078: Line 1116:
   * Enterprise software: it’s software that is not designed for individuals, but for big organizations or companies.   * Enterprise software: it’s software that is not designed for individuals, but for big organizations or companies.
  
-  o They help organizations be more effective because of the software that is built especially for businesses. o Many companies end up with unlimited access to a lot of personal information due to this idea of enterprise software. o They still hold the responsibility of keeping this information safe. +o They help organizations be more effective because of the software that is built especially for businesses. o Many companies end up with unlimited access to a lot of personal information due to this idea of enterprise software. o They still hold the responsibility of keeping this information safe. 
  
   * Disaster recovery plan   * Disaster recovery plan
Line 1135: Line 1173:
   * “Row-major order”   * “Row-major order”
  
-  Ordering 2D array elements where traversal occurs across each row While “column-major order” traversal occurs down each column. public class TwoDArrayInitGet { public static void main(String[] args) { String[][] seating = { {"Sarah", "Maria"}, +Ordering 2D array elements where traversal occurs across each row While “column-major order” traversal occurs down each column. public class TwoDArrayInitGet { public static void main(String[] args) { String[][] seating = { {"Sarah", "Maria"}, 
  
-  {"Cameron", "Ella"}, {"Emma", "Luke"} }; String name = seatingInfo[0][0]; System.out.println(name + " is at [0,0]"); } } +{"Cameron", "Ella"}, {"Emma", "Luke"} }; String name = seatingInfo[0][0]; System.out.println(name + " is at [0,0]"); } } 
  
 **Traversing 2D Arrays** **Traversing 2D Arrays**
Line 1171: Line 1209:
   * **2d Array Initialization** - You can also initialize (set) the values in the array when you first create it. In this case you don’t need to specify the size of the array, it will be determined from the number of values that you specify.   * **2d Array Initialization** - You can also initialize (set) the values in the array when you first create it. In this case you don’t need to specify the size of the array, it will be determined from the number of values that you specify.
  
-  Example: String[][] seatingInfo = { {"Jamal", "Maria"}, {"Jake", "Suzy"}, {"Emma", "L uke"}}; This will create a 2d array with 3 rows and 2 columns.+Example: String[][] seatingInfo = { {"Jamal", "Maria"}, {"Jake", "Suzy"}, {"Emma", "L uke"}}; This will create a 2d array with 3 rows and 2 columns.
  
   * **2d Array Number of Rows** - The number of rows (or height) is the length of the outer array. For an array arr use arr.length to get the number of rows in the array.   * **2d Array Number of Rows** - The number of rows (or height) is the length of the outer array. For an array arr use arr.length to get the number of rows in the array.
Line 1195: Line 1233:
   - Common attributes and behaviors of related classes into a single class called   - Common attributes and behaviors of related classes into a single class called
  
-  a superclass.+a superclass.
  
   - Classes extend a superclass   - Classes extend a superclass
Line 1223: Line 1261:
   * Example (part 1) class Human   * Example (part 1) class Human
  
-  { private String name; public Human(String theName) { + 
 +    private String name; public Human(String theName)  
 +  
  
-  this.name = theName; } public String getName() { return name; } public boolean setName(String newName) { if (newName != null) { this.name = newName; return true; +this.name = theName;  
 +    } public String getName()  
 +      { return name;  
 +       
 +  public boolean setName(String newName)  
 +     
 +      if (newName != null)  
 +    { this.name = newName; return true; 
  
   * Example (part 2)   * Example (part 2)
  
-  } return false; } } public class Employee extends Human { private static int nextEmployeeId = 1; private int employeeId; public Employee(String theName) { super(theName); employeeId = nextEmployeeId; nextEmployeeId ++; +} return false;  
 + 
 + 
 +public class Employee extends Human  
 +{ private static int nextEmployeeId = 1; private int employeeId; public Employee(String theName)  
 +{ super(theName); employeeId = nextEmployeeId; nextEmployeeId ++; 
  
-  } public int getEmployeeId() { return employeeId; } public static void main(String[] args) { Employee emp = new Employee("Ella"); System.out.println(emp.getName()); System.out.println(emp. getEmployeeId ()); } } +>  
 +   
 +    public int getEmployeeId()  
 +   
 +    return employeeId;  
 +   
 +    public static void main(String[] args)  
 +       
 +         Employee emp = new Employee("Ella"); System.out.println(emp.getName()); System.out.println(emp. getEmployeeId ());  
 +        
 +   
  
 **Overriding Methods** **Overriding Methods**
Line 1249: Line 1311:
   * Two uses of the keyword super o super();   * Two uses of the keyword super o super();
  
-  o super(arguments); o super.method();+o super(arguments); o super.method();
  
   * Calls the superclass method but not the constructors from the class.   * Calls the superclass method but not the constructors from the class.
Line 1283: Line 1345:
   * These are two Object class methods and constructors o String toString()   * These are two Object class methods and constructors o String toString()
  
-  o boolean equals(Object other)+o boolean equals(Object other)
  
   * Subclasses of Object can override these methods with implementations that are specific to the class.   * Subclasses of Object can override these methods with implementations that are specific to the class.
Line 1303: Line 1365:
   * public static int mystery(int n)   * public static int mystery(int n)
  
-  { if (n < 5) { return n; } else { +{ if (n < 5) { return n; } else { 
  
-  int a = n / 5; int b = n % 5; return mystery(a + b); } } What result do you get for the following callings? mystery (3) answer = 3 mystery (20) answer = 2 +int a = n / 5; int b = n % 5; return mystery(a + b); } } What result do you get for the following callings? mystery (3) answer = 3 mystery (20) answer = 2 
  
   * Recursive method -- method which calls itself   * Recursive method -- method which calls itself
Line 1354: Line 1416:
   * Expecting you comprehend what the recursion is managing without following every last bit of it.   * Expecting you comprehend what the recursion is managing without following every last bit of it.
  
-  PRACTICE QUESTIONS+PRACTICE QUESTIONS
  
   * The practice questions shown below are from [[https://runestone.academy/runestone/books/published/apcsareview/Recursion/rEasyMC.html|https://runestone.academy/runestone/books/published/apcsareview/Recursion/rEasyMC.h]] [[https://runestone.academy/runestone/books/published/apcsareview/Recursion/rEasyMC.html|tml]]   * The practice questions shown below are from [[https://runestone.academy/runestone/books/published/apcsareview/Recursion/rEasyMC.html|https://runestone.academy/runestone/books/published/apcsareview/Recursion/rEasyMC.h]] [[https://runestone.academy/runestone/books/published/apcsareview/Recursion/rEasyMC.html|tml]]
Back to top