Unit 1-5 Lesson Notes
Lesson Score + Notes/Tangibles
Unit 1
- 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
- String class are unable to be changed
- declare a variable that can be accessible and/or changed
- (=) is used to initialize variables or change its associated value
- Operators are + - * / %
- Compound operators are +=, -=, *=, /=, %=
- Increment/decrement ++ or --
Unit 2
- classes are blueprints for creating objects
- objects are instances within a class
- attributes are inherited by objects
- Non static methods are dot operators
- methods are certain set of code that runs a specific task
- 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
- 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
Unit 3
- boolean expression is true or false
- 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)
Unit 4
- While/for loops: repeats lines of code until a specific condition comes out false; can iterate over multiple elements
- For loops are most tested
- Strings: array chairs
Unit 5
- 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
Additional Vocab
Primitives:
- Doubles - 64 bits
- Integers - 32 bits - use int when declaring, not Integer. Integer is non-primitive
- Booleans - 1 bit
- String is a non-primitive data type
Non-primitive or Wrapper classes data types use methods to perform actions
String Class
Strings are immutable - unable to be changed
Methods that act upon string objects do not change the state of the defined string object.
Constructors - initialize the attributes for an object
- Constructors start with public, and have the same name as the class. In this case, these constructors are for the class Person
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from)
Inheritance in Java is the method to create a hierarchy between classes by inheriting from other classes
Casting
- change one data type to another. Two casting operators are
- int
-
double
-
Division - Seen above, casting can round the division for y/x so that it returns an integer, this is to allow for certain code beyond that that would only be able to take integer values for a specific reason.
- Rounding/Truncating - Seen in both examples above, casting is great for rounding a value to an integer or for another reason in order to simplify code.
double x = 4.5;
double y = 6.52;
int z = (int) (y/x);
int aa = (int) (x*y);
System.out.println(z);
System.out.println(aa)
Wrapper Classes - Classes that are able to make primitive types into objects to be used in things such as ArrayLists that would not allow primitives. See below for an example.
// Wrapper classes example
ArrayList<Integer> ton = new ArrayList<Integer>();
// ArrayList<int> would not work for this
// Concatenation example
String a = "Hello";
String b = "World";
int c = 333;
String d = a + " " + b + " " + c;
System.out.println(d);
Concatenation - Concatenation involves combining two or more strings together. This is done by using the + operator. Anything that's not a string is attempted to be converted to a string in order to be concatenated on the end, like the int in this example.
Static/Class Variables - Static variables are variables that are shared across all instances of a class. This means that no matter how many objects you instantiate of the same class, modifying a static variable or method will update it for all instances. This is useful for things like a counter that you want to increment for every instance of a class.
Inheritance/Extends - Using an extends method allows it to inherit attributes and methods from the class it is extending. This is useful when you want to modify or create a subclass of a class without having to rewrite all of the code for the class you are extending.
public int scoreGuess(String guess)
{
int count = 0;
for (int i = 0; i <= secret.length() - guess.length(); i++)
{
if (secret.substring(i, i + guess.length()).equals(guess))
{
count++;
}
}
return count * guess.length();
}
public String findBetterGuess(String guess1, String guess2)
{
if (scoreGuess(guess1) > scoreGuess(guess2))
{
return guess1;
}
if (scoreGuess(guess2) > scoreGuess(guess1))
{
return guess2;
}
if (guess1.compareTo(guess2) > 0)
{
return guess1;
}
return guess2;
}
Polymorphism - Polymorphism is the ability to have multiple methods with the same name but different parameters. This is useful when you want to have multiple methods that do the same thing but with different parameters. This is also useful when you want to have a method that can take multiple types of parameters
Overloading is when you have multiple methods with the same name but different parameters.
Overriding is when you have a method with the same name and parameters as a method in a superclass but you want to modify the method in the subclass.
Late binding is when you have allow the compiler to determine which method to use at runtime instead of compile time. Abstract Class - Objects cannot be created from an abstract class, they can only be extended. This is useful when you want to create a class that can be extended but not instantiated
abstract class People {
public void walked() {
System.out.println("Rohit walked 10 miles");
}
}
class KURTIS extends People {
public void walked() {
System.out.println("kurtis walked 9 miles");
}
}
class NATHAN extends People {
public void walked() {
System.out.println("nathan walked 8 miles");
}
}
// Instantiating the People class would throw an error as it is abstract
KURTIS kur = new KURTIS();
NATHAN nat = new NATHAN();
kur.walked();
nat.walked();
Subclass Constructor / Super - Extending a class and then constructing the new extends class will call the constructor of the superclass. This is useful when you want to have a constructor that will call the constructor of the superclass.
Referencing Superclass - Using the super keyword allows you to reference the superclass of the current class. This is useful when you want to reference the superclass of the current class.
Standard Methods - ToString is a standard method that is used to convert an object into a string. Equals is useful for comparing two objects to see if they are equal. HashCode is useful for getting a unique hash code for an object.
Big O Notation - Implementing Big O notation is useful for determining the efficiency of an algorithm. Making algorithms more efficient is useful for making programs compile and run faster and likely reduce the load of it, which could be especially good for websites where users may be runnning the same function through an api many times.