Unit 1-5 Lesson Blogs
Lesson Notes + Tangibles
- Unit 1: Primitives
- Unit 2: Objects
- Unit 3: Boolean
- Unit 4: Iteration
- Unit 5: Writing Classes
- Unit 6: Arrays
Unit 1: Primitives
Takeaways:
- Java provides
wrapper classes
for all primitive data types like char, int, double, boolean where you can call in static methods that return values - Primitive wrapper objects or string objects are immutable so the methods can't change
- Boolean, char, int, float, double are all primitive data types
- String, Array, etc. are non-primitive data types
- Learned to declare a variable that can be accessible and/or changed
- Learned how to store data as variables
- (=) is used to initialize variables or change its associated value
- Operators are + - * / %
- Compound operators are +=, -=, *=, /=, %=
- Increment/decrement ++ or --
public class Integers
{
public static void main(String[] args)
{
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
int low = Integer.MAX_VALUE;
int high = Integer.MIN_VALUE;
}
}
Integers.main(null);
// creating a class, wrapper classes for int
Unit 2: Objects
Takeaways:
- Learned the difference between classes vs. objects
- Classes are blueprints for creating objects
- Objects are instances within a class
- Methods: certain set of code that runs a specific task
- Class attributes are inherited by objects
- Learned about myPainter in Code.org
- Non static methods are dot operators
- Overloaded methods give multiple methods the same name with different signatures
- Void methods don't return values
- Non void methods return a value of the same type defined in the method signature
- Strings are created with String class constructor
- Wrapper class: integer and double class are part of the java.lang package
-
Comparing numbers and objects
using .equals() -
static methods
are a part of a class instead of an instance of a class which is why it's in a bracket in the class -
class methods
were just like in Code.org lessons
public class ComparingObjects
{
public static void main(String[] args)
{
//creating constructor of the Double class
Double x = new Double(100.50);
//creating constructor of the Long class
Long y = new Long(12345);
//invoking the equals() method
System.out.println("Objects are not equal, hence it's " + x.equals(y));
}
}
ComparingObjects.main(null);
// comparing objects using .equals()
Unit 3: Boolean
Takeaways:
- Whether boolean expression is true or false dictates whether the code will run
- If-else statements sets up alternate code if the first expression turns false
- Else-if statements allow for more conditions to be defined
- De Morgan's law: logical operators && (and) ll(or) and !(not)
- Comparing objects through ==
-
Compound boolean expression
check multiple values in a single statement with && - likeDeMorgan's Law
public class Test1
{
public static void main(String[] args)
{
boolean cleanedRoom = true;
boolean didHomework = false;
if (cleanedRoom && didHomework)
{
System.out.println("You can go out");
}
else
{
System.out.println("No, you can't go out");
}
}
}
Test1.main(null);
import java.util.ArrayList;
/*
* Creator: Nighthawk Coding Society
* Mini Lab Name: Hello Series,featuring Monkey Jumpers
*/
/**
* Class for Monkey: a 2D array of Monkey
* As well as method to print the Poem
*/
class Monkey {
//The area between class definition and the 1st method is where we keep data for object in Java
private static ArrayList<String[]> monkeyList = new ArrayList<String[]>(); //2D Array: AP CSA Unit 8: 2D array of strings
private String[] monkeyASCII;
/**
* Constructor initializes a 2D array of Monkey
*/
public Monkey(String[] monkeyASCII) {
this.monkeyASCII = monkeyASCII;
monkeyList.add(monkeyASCII);
}
/**
* Loop and print monkey in array
* ... repeat until you reach zero ...
*/
public static void printPoem() {
//begin the poem
System.out.println();
System.out.println("Monkey Jumpers Poem in Java with Objects!!!");
// monkey (non-primitive) defined in constructor knows its length
int monkeyCount = monkeyList.size();
for (int i = 1; i <= monkeyCount; i++) //loops through 2D array length forwards
{
//this print statement shows current count of Monkey
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " little monkey jumping on the bed...");
//how many separate parts are there in a monkey monkey?
for (int row = 0; row < i; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each monkey part by part, will eventually print entire column*/
for (int col = 0; col < monkeyList.get(row).length; col++) {
// prints specific part of the monkey from the column
System.out.print(monkeyList.get(row)[col] + " ");
//this is new line between separate parts
System.out.println();
}
//this new line gives separation between stanza of poem
System.out.println();
}
//countdown for poem, decrementing monkeyCount variable by 1
monkeyCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("Too many monkeys jumping on the bed");
System.out.println("0000000000000000000000000000000000");
System.out.println(" THE END ");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
Monkey monkey0 = new Monkey(new String[]{
"ʕง ͠° ͟ل͜ ͡°)ʔ ", //[0][0] eyes
" \\_⏄_/ ", //[0][1] chin
" --0-- ", //[0][2] body
" ⎛ ⎞ " //[0][3] legs
});
Monkey monkey1 = new Monkey(new String[]{
" ʕ༼ ◕_◕ ༽ʔ", //[1][0]
" \\_⎏_/ ",
" ++1++ ",
" ⌋ ⌊ "
});
Monkey monkey2 = new Monkey(new String[]{
" ʕ(▀ ⍡ ▀)ʔ", //[2][0]
" \\_⎐_/ ",
" <-2-> ",
" 〈 〉 "
});
Monkey monkey3 = new Monkey(new String[]{
"ʕ ͡° ͜ʖ ° ͡ʔ", //[3][0]
" \\_⍾_/ ",
" ==3== ",
" _/ \\_ "
});
Monkey monkey4 = new Monkey(new String[]{
" (◕‿◕✿) ", //[4][0]
" \\_⍾_/ ", //[4][1]
" ==4== ", //[4][2]
" _/ \\_ " //[4][3]
});
Monkey.printPoem(); //a new monkey list and output in one step
}
}
Monkey.main(null);
Unit 5: Writing Classes
Takeaways:
- Classes are blueprints to create objects and define attributes
- Instances: attributes, constructors, methods, objects
- Public classes: no restricted access - constructors
- Private classes: restricted access - instance variables
-
Accessor method (getter)
: allows other objects to obtain values of instance variables or static variables - Non void methods return a single value
-
toString() method
is a overridden method that provides description of a specific object -
Mutator Method (setter)
: void method that changes value of instance variables/static variable