Unit 1-10 Notes
Unit 1: Primitive Types
Key Concepts:
- Variables and data types (int, double, boolean, char)
- Arithmetic operations
- Casting (widening/narrowing)
Example:
int x = 5;
double y = 2.0;
double result = x / y; // result = 2.5
Type casting:
int a = 7;
int b = 2;
double c = (double) a / b; // 3.5
Unit 2: Using Objects
Key Concepts:
- Object creation using new
- String methods
- Wrapper classes (e.g., Integer, Double)
- Autoboxing/unboxing
Example:
String word = "APCSA";
System.out.println(word.length()); // 5
System.out.println(word.substring(2)); // "CSA"
System.out.println(word.indexOf("C")); // 2
Wrapper Example:
Integer n = 10; // autoboxing
int x = n.intValue(); // unboxing
Unit 3: Boolean Expressions and if Statements
Key Concepts:
- Relational operators:
==
,!=
,>
,<
,>=
,<=
- Logical operators:
&&,
||
,!
- Control flow with if, else if, and else
Example:
int age = 16;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
Boolean expression:
boolean valid = !(x < 10 || x > 100);
Unit 4: Iteration
Key Concepts:
- for, while, and do-while loops
- Loop control: break, continue
- Nested loops
Examples:
for (int i = 0; i < 5; i++) {
System.out.print(i + " "); // 0 1 2 3 4
}
int i = 0;
while (i < 5) {
System.out.print(i + " ");
i++;
}
Unit 5: Writing Classes
Key Concepts:
- Defining a class and constructor
- Instance variables
- this keyword
Example:
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public void bark() {
System.out.println(name + " says woof!");
}
}
Unit 6: Array
Key Concepts:
- Array declaration, instantiation, and indexing
- Iterating through arrays
- Common algorithms (sum, max, etc.)
Example:
int[] scores = {90, 80, 70};
for (int score : scores) {
System.out.println(score);
}
Find max value:
int max = scores[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] > max) max = scores[i];
}
Unit 7: ArrayList
Key Concepts:
- Importing and using ArrayList
- add(), get(), set(), remove(), size()
- For-each loop limitations
Example:
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.set(0, "Ann");
System.out.println(names); // [Ann, Bob]
Unit 8: 2D Arrays
Key Concepts:
- Declaration and nested loops
- Row-major traversal
- Applications in grid problems
Example:
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(grid[1][2]); // 6
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.print(grid[row][col] + " ");
}
}
Unit 9: Inheritance
Key Concepts:
- extends keyword
- Method overriding
- super keyword
Example:
public class Animal {
public void speak() {
System.out.println("Animal sound");
}
}
public class Dog extends Animal {
public void speak() {
System.out.println("Woof");
}
}
Unit 10: Recursion
Key Concepts:
- Base case and recursive call
- Stack behavior
- Tracing recursive calls
Example:
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
String reversal:
public static String reverse(String s) {
if (s.length() <= 1) return s;
return reverse(s.substring(1)) + s.charAt(0);
}