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:28] 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 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 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();  
 +
  
 > } }  > } } 
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;  
 +  
  
 > } > }
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] );  
 +
  
 > } }  > } } 
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**
Back to top