Units 1-5 Review
// In this example you can see the difference in data sizes
int x = 2;
int y = 4;
// without casting
System.out.println(y/x);
// With casting
System.out.println((double)y/(double)x);
// this example shows truncating with narrowing
double a = 3.7;
// without casting
System.out.println(a);
// with casting
System.out.println((int)a);
// assign primitive variable
int x = 1;
// instantiate arraylist of integers
ArrayList<Integer> intList = new ArrayList<Integer>();
// create Integer from int
Integer a_wrapper = new Integer(x);
intList.add(x);
System.out.println(intList);
// assigning variables
String x = "Hippo";
String y = "pota";
String z = "mus";
int a = 12;
// concatenating all strings
System.out.println(x + y + z);
// concatenate x "Hippo" and int a
System.out.println(x + a);
import java.lang.Math; // importing
// by itself
Math.random(); // generates random number between 0 and 1, inclusive
// can multiply by integer to expand range (0-100)
int randomNumber = (int)(Math.random() * 101);
System.out.println(randomNumber);
// assigning variables
int x = 2;
int y = 2;
int z = 1;
// return True
System.out.println(x == y);
// return False
System.out.println(x == z);
// assigning strings
String a = "Hi";
String b = "Bye";
String c = "Hi";
// this should return false
System.out.println(a.equals(b));
// this should return true
System.out.println(a.equals(c));
// simple expression
boolean x = true;
boolean y = false;
System.out.println(x);
System.out.println(y);
// compound
boolean compound = !(x && y) || (x || y) && !(x || y);
System.out.println(compound)
boolean x = false;
boolean y = false;
// De Morgan's Law two
boolean testDMLaw = !(!x || !y) && !(!x && !y);
System.out.println(testDMLaw);
Unit 4: Iteration
Link to notebook Iteration uses loops that continue based on certain conditions. You can have several loops nested in one another
- For continues
- While continues while a certain condition(s) remain true
- Recursive recurs by calling itself
// regular for loop
for (int i = 1; i<=9; i+=2) { // condition for ending loop is when current integer is <= 9
System.out.println(i);
}
// enhanced loop for array
int[] array = {2, 4, 6, 8, 10};
// syntax is for (dataType variable : array)
for (int a : array) {
System.out.println(a);
}
// regular while loop
int i = 0;
while (i <= 5) {
System.out.println(i);
i++;
}
// if i set the parameter i to >5 in the beginning it will not output anything
int i = 6; // regular while loop
while (i <= 5) {
System.out.println(i);
i++;
}
// do while loop
int i = 0;
do {
System.out.println(i);
i++;
}
while (i <= 5); // condition is at the end
// Outer loop
for (int i = 1; i <= 3; i++) { // printing i and adding 1 each time, 3 times
System.out.println("Outer: " + i);
// Inner loop
for (int j = 1; j <= 2; j++) { // runs 2 times for each loop of the outer
System.out.println(" Inner: " + j);
}
}
Unit 5: Classes
A class is a blueprint with properties and methods. They serve as blueprints for creating objects. Properties can either be public or private (accessible outside of the class), and methods can modify objects (object-oriented). You can use methods such as getter and setter to modify the class properties.
class ThisClass {
}
class Hello {
public static void main (String[] args) {
Hello object = new Hello();
}
}
Hello.main(null);
public class Main {
int a;
public Main(int a) {
this.a = a; // using this to access a
}
public static void main(String[] args) {
Main newObject = new Main(2); // this is the constructor
System.out.println(newObject.a);
}
}
Main.main(null);
public class Main {
private String hello; // private variable
public String getHello() { // getting private hello str
return hello;
}
public void setHello (String hello) { // method to set hello
this.hello = hello;
}
}
public void PublicTest() {
System.out.println("Hello, world!");
}
private void PrivateTest() {
System.out.println("Hello, world!");
}
protected void ProtectedTest() {
System.out.println("Hello, world!");
}