College Board Vocab and Implementation
Unit 1: Primitives
- Two main types of data in java
- primitives
- int
- boolean
- char
- double
- objects
- strings
- other classes
- primitives
- Primitives do not have methods and properties while these are present in classes (ie. String length() method)
- Homework from student presentation
// Set numbers to variables
int x = 7;
int y = 2;
// Print division of x and y w/o casting (will do floor division)
System.out.println(x/y);
// Print division of x and y w/ casting (will give exact answer)
System.out.println((float)x/(float)y);
// Can also work in the reverse order
float z = 3.7f;
// Printing "z" w/o casting
System.out.println(z);
// Printing "z" w/ casting as int
System.out.println((int)z);
// assign value to variable
int x = 5;
// instantiate arraylist of ints
ArrayList<Integer> intList = new ArrayList<Integer>();
// create integer from int for wrapper
Integer a_wrapper = new Integer(x);
intList.add(x);
System.out.println(intList);
Unit 2: Using Objects
- Objects are just an instance created out of a class created w/ a constructor (which takes in parameters describing the object
- Methods in objects can be void (returns nothing) or have a return type specified
- Static methods and properties are tied to class rather than object (ie. same value for all objects)
- Methods can be overloaded (have different sets of parameters) as long as order of types differs between method definitions even with same name
- Homework from student presentation
// assignment of variables
String x = "Hello ";
String y = "Hi ";
String z = "Howdy! ";
int a = 15;
// Concatenate all the strings together
System.out.println(x + y + z);
// When we concatenate int a to string z, it converts a to a string
System.out.println(z + a);
int x = 10;
for (int n = 0; n < 3; n++) {
int ran_num = (int)(Math.random() * x + 1);
System.out.println(ran_num);
}
int a = 1;
int b = 1;
int c = 2;
String x = "Howdy";
String y = "Howdy";
String z = "Hello";
System.out.println(a == b);
System.out.println(a == c);
System.out.println(x.equals(y));
System.out.println(x.equals(z));
Unit 3: Booleans and If Statements
- Boolean = true or false
- Created when using comparison
- ==
- <
- >
- If statements used to check conditions
- Created when using comparison
- Homework from student presentation
boolean x = true;
boolean y = false;
// compound boolean expressions
boolean compound_boolean = !(x && y) || (x || y) && !(x || y);
// result
System.out.println(compound_boolean);
boolean a = false;
boolean b = true;
boolean c = false;
boolean d = true;
// complicated boolean expression
boolean complicated = !((!(a && b)) || (!(a || b)));
// De Morgan's Law one
boolean simplified_one = !((!a || !b) || (!a && !b));
// De Morgan's Law two
boolean simplified_two = !(!a || !b) && !(!a && !b);
// print result (should all be same)
System.out.println(complicated + " " + simplified_one + " " + simplified_two);
Unit 4: Iteration
- While loop = while condition met
- For loop = for temp var in condition
- loops can be nested
- Homework from student presentation
int[] array = {1, 2, 3, 4, 5};
// looping through even numbers
System.out.println("Loop of even numbers through ten");
for (int i = 0; i<10; i+=2) {
System.out.print(i);
}
System.out.println("\nLooping through array");
// looping through array with for loop
for (int i = 0; i<array.length; i++) {
System.out.print(array[i]);
}
System.out.println("\nLooping through array using enhanced loop");
// looping through array with enhanced for loop
for (int i : array) {
System.out.print(i);
}
While Loop vs Do While Loop
- While loops run while a condition is true, the condition is checked before each iteration of the code is run
- Do while loops also run while a condition is true, but the condition is checked AFTER each iteration of the code is run
- This means that no matter what the do block runs at least once before the condition is checked
int i = 0;
boolean falseBool = false;
// printing even numbers with while loop
while (i < 10) {
System.out.println(i);
i += 2;
}
// if condition is false while loop does not run at all
while (falseBool) {
System.out.println("while loop");
}
// if condition is false in do while, the loop runs once
do {
System.out.println("do-while loop");
} while (falseBool);
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// using nested for loops for 2D array
for (int i = 0; i<arr.length; i++) {
for (int j = 0; j<arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
Unit 5: Writing Classes
- Classes can be used for creating objects and have two main things: properties and methods
- Properties are used to store information about each object of a class (can be made private/public which determines accessibility outside of class)
- Methods are used to modify the object & do things
- Getter and Setter Methods can be used to modify properties of a class which are made private
- Homework from student presentation
// create a class
class HelloWorld {
// code
}
class HelloWorld {
public static void main (String[] args) {
HelloWorld object = new HelloWorld();
}
}
HelloWorld.main(null);
class HelloWorld {
int prop1;
int prop2;
// Constructor here
public HelloWorld (int prop1input, int prop2input) {
// setting properties using this to reference prop1 & prop2 of the object
this.prop1 = prop1input;
this.prop2 = prop2input;
}
// tester method
public static void main (String[] args) {
HelloWorld object = new HelloWorld(50, 75);
}
}
HelloWorld.main(null);
class HelloWorld {
int prop1;
int prop2;
// Constructor here
public HelloWorld (int prop1input, int prop2input) {
// setting properties using this to reference prop1 & prop2 of the object
this.prop1 = prop1input;
this.prop2 = prop2input;
}
// getter allows outside class to access prop1
public int getProp1() {
return this.prop1;
}
// tester method
public static void main (String[] args) {
HelloWorld object = new HelloWorld(50, 75);
// using getter to access prop1
System.out.println(object.getProp1());
}
}
HelloWorld.main(null);
class HelloWorld {
int prop1;
int prop2;
// Constructor here
public HelloWorld (int prop1input, int prop2input) {
// setting properties using this to reference prop1 & prop2 of the object
this.prop1 = prop1input;
this.prop2 = prop2input;
}
// getter allows outside class to access prop1
public int getProp1() {
return this.prop1;
}
// setter allows outside class to set prop1
public void setProp1 (int propVal) {
this.prop1 = propVal;
}
public static void main (String[] args) {
HelloWorld object = new HelloWorld(50, 75);
// using getter to access prop1
System.out.println(object.getProp1());
// changing value of prop1
object.setProp1(10);
// using getter to access new value of prop1
System.out.println(object.getProp1());
}
}
HelloWorld.main(null);
class HelloWorld {
// prop1 cannot be directly accessed
private int prop1;
// prop2 can be directly accessed
public int prop2;
// Constructor here
public HelloWorld (int prop1input, int prop2input) {
// setting properties using this to reference prop1 & prop2 of the object
this.prop1 = prop1input;
this.prop2 = prop2input;
}
// getter allows outside class to access prop1
public int getProp1() {
return this.prop1;
}
// setter allows outside class to set prop1
public void setProp1 (int propVal) {
this.prop1 = propVal;
}
public static void main (String[] args) {
HelloWorld object = new HelloWorld(50, 75);
// would throw error
// object.prop1 = 10;
// works (because public)
object.prop2 = 100;
System.out.println(object.prop2);
}
}
HelloWorld.main(null);
class HelloWorld {
// static method
static String awesomeMethod (String a) {
return a + " awesome";
}
// static property
static int staticProp = 17;
public static void main(String[] args) {
// no object needed for any of this
System.out.println(HelloWorld.awesomeMethod("method"));
System.out.println(HelloWorld.staticProp);
}
}
HelloWorld.main(null);