Collegeboard APCSA Notes
CollegeBoard Notes
Unit 1 Primitive Types
- Primitive types categorized into two types of data types
- Primitive data types - includes byte, short, int, long, float, double, boolean and char
- Non-primitive data types - such as String, Arrays and Classes
- There are eight primitive types
- A Byte (1 byte) and stores whole numbers from -128 to 127
- A short (2 bytes) and stores whole numbers from -32,768 to 32,767
- An int (4 bytes) and stores whole numbers from -2,147,483,648 to 2,147,483,647
- A long (8 bytes) and stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- A float (4 bytes) and stores fractional numbers which is great when storing 6 to 7 decimal digits
- A double (8 bytes) and stores fractional numbers which is great when storing 15 decimal digits
- A boolean (1 bit) and stores only 2 values, true or false
- A char (2 bytes) and stores a single character/letter or ASCII values
Unit 2 Using Objects
- Java is object-oriented programming language (OOP)
- Everything in Java is associated with classes and objects, along with its attributes and methods. In real life, a car is an object and has attributes, like weight and color, and methods, like drive and brake.
- A Class is like an object constructor, or a "blueprint" for creating objects.
- To create classes, use the keyword "class" (usually main class is created)
- In Java, an object is created from a class. We have already created the class named Main, so now we can use this to create objects.
- You can also create multiple objects for one class
Unit 3 Boolean Expressions and if Statements
- Boolean is a primitive data type that can only store true or false values
- Boolean can be declared with the keyword "boolean"
import java.util.Scanner;
public class booleanExample //define class using class keyword
{
public static void main(String[] args) //java main method
{
boolean isCorrect = true; //declare the boolean variables using boolean keyword
boolean isWrong = false;
if (isCorrect)
{
System.out.println("You are right.");
}
else
{
System.out.println("You are wrong.");
}
}
}
booleanExample.main(null);
This can also be used to compare values and numbers. For example, using a comparison operator, like the > operator, we can find out if an expression is true. This can be easily used in real life situations like the following.
int myAge = 16; // stating the two integers with their values
int votingAge = 18;
System.out.println(myAge > votingAge); // returns false, because 16 is less than 18
Unit 4 Iteration
- An Iterator is an object that loops through collection
- To use an Iterator in Java, you must import it from the java.util package.
- To get an Iterator, the iterator() method can be used
- There are three types of iterations that can be used
- For Loops
- While Loops
- Recursions
- You can also add or remove numbers using an Iterator
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(12);
numbers.add(8);
numbers.add(2);
numbers.add(23);
Iterator<Integer> it = numbers.iterator();
while(it.hasNext()) {
Integer i = it.next();
if(i < 10) {
it.remove();
}
}
System.out.println(numbers);
}
}
Unit 5 Writing Class
- There are a couple of steps to writing a class
- make sure to use the class keyword to create a class in the start
- To create an object of Main, specify the class name, followed by the object name, and use the keyword new
- Constructors are used to create new objects
- Instance variables are used to store any information regarding objects created in a class
- Methods are used to show programs a new command they must run
- specific methods are used to perform specific tasks in a class
Unit 6 Array
- Used to store multiple values in a single variable
- To declare an array, define the variable type with square brackets and insert values formatted in a comma-separated list, using curly braces
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
- Access array elements by referring to the index number in the array. The counting for the elements starts at 0.
System.out.println(cars[0]);
- You can also change the value of a specific element using the index number
- To find out how many elements an array has, use the length property
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
Unit 7 Arraylist
- The ArrayList class is a resizable array, which can be found in the java.util package
- The size of an Array cannot be modified so if you want to add or remove elements to/from an array, you have to create a new one whereas for Arraylists, elements can be added and removed whenever you want
- The syntax for each type is also different
- The ArrayList class has many useful methods. For example, to add elements to the ArrayList, use the add() method
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
- To access an element in the ArrayList, use the get() method and refer to the index number
cars.get(0);
Unit 8 2D Array
- A 2D Array is the simplest form of a multidimensional array
- The syntax for declaration is
data_type[][] array_name = new data_type[x][y];
int[][] arr = new int[10][20];
- The syntax for initialization is
array_name[row_index][column_index] = value;
arr[0][0] = 1;
- Elements in two-dimensional arrays are commonly referred by x[i][j] where ‘i’ is the row number and ‘j’ is the column number
- To output all the elements of a 2D array, use nested for loops
- For this 2 for loops are needed, one to traverse the rows and another to traverse the columns
Unit 9 Inheritance
- Inheritance is essentially what attributes/methods the child class inherits from the parent class
- This can be used with the fibonacci sequence mini-lab earlier in the trimester
- Is useful when you want to inherit the same attributed and methods from another class
- "this" is used when referring to the current object in the method
- "super" is used to call any superclass method like the parent class
Unit 10 Recursion
- Recursion is used to make a function call itself and breaks complicated problems down into simple problems
- Use recursion to add all of the numbers up to 10
public class Main {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
}
When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and returns the result. When k becomes 0, the function just returns 0. Since the function does not call itself when k is 0, the program stops there and returns the result.