PrepForOopExam
Hello everyone!
I assume that you're currently studying for your OOP exam and you are probably tired from exercise 5 , don't know how or from where to get all the needed information.
ME TOO!
So that's why I thought it can be a good idea to create this repo and let EVERYONE share his/her favorite youtube playlist, website or any useful OOP related content.
Feel free to contribute!
Exams
TA
Course Drive
Summary
Campus.il Course website (Hebrew)
Index
- Basic Java syntax
- Data Types
- Declaring Variables in Java
- Arrays
- Java Keywords (static,final etc.)
- Operators in Java
- Visibility Modifiers
- Enum
- String
- Packages
- Modules
- Basic Compilation process in Java
- JVM
- Basic OOP terminology
- information hiding
- Minimal API
- encapsulation
- Abstraction
- Polymorphism
- Inheritance
- Composition
- Interfaces
- Abstract class
- Basic OOP principles
- Program to interface not implementation principle
- The single choice Principle
- Modularity principle
- Open-Closed prinicple
- Leskov substituion principle
- Algorithms
- Exceptions
- Java collections
- Design Patterns
- Generics
- Functional Programming
- Local and Anonymous Classes
- Lambdas
- Functional Interfaces
- Java.util.functions
- Closures
- Callbacks
- UML
- Advanced topics in Java
- Serialization
- Reflection
- Copy Constructor
- Clone
- Frameworks (Spring)
- Regex
Basic Java Syntax
Java Keywords
-
null
Bike example = null; // no object created here and example points to nothing
example.gear; // accessing null object attributes leads to runtime errors // NullPointerException
Bike example; // By default example value is null
Visibility Modifiers
In Java, methods and data members of a class/interface can have one of the
following four access specifiers.
- private (accessible within the class where defined)
- default or package private (when no access specifier is specified)
- protected
- public (accessible from any class)
But, the classes and interfaces themselves can have only two access specifiers
when declared outside any other class.
- public
- default (when no access specifier is specified)
Note : Nested interfaces and classes can have all access specifiers.
## [String Class](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html)
The most common java class. Altough it is no a primitive, can be initialized using the '=' sign.
String myString = "hello"; // legal, just like writing new String("hello")
String class has many important useful methods! you can read more HERE about them. String class is immutable meaning we can't change the content of string.
Basic Compilation process in Java
JVM vs JRE vs JDK
compilation process
Basic OOP terminology
Object, Class, Instance, Attributes
Abstract class
Basic OOP principles
ALL-IN-ONE all the OOP prinicples we've learned and more!
SOLID
-
The Single Responsibility Principle
A class should never have more than one reason to change.
every class should have only one responsibility!
and that responsibility should be entirely encapsulated by the class.
Responsibility can be defined as a reason to change, so a class or module should have one, and only one, reason to change.
Why? Maintainability: changes should be necessary only in one module or class.
How? Apply Curly's Law which means choosing a single, clearly defined goal for any particular bit of code: Do One Thing.
-
The Open-Closed prinicple
Software entities should be open for extension, but closed for modification i.e. such an entity can allow its behavior to be modified without altering its source code.
Why? Improve maintainability and stability by minimizing changes to existing code.
How? Write classes that can be extended (as opposed to classes that can be modified).
Expose only the moving parts that need to change, hide everything else.
-
The Leskov substituion principle Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.
Program to interface not implementation principle
Exceptions
Java collections
I'll leave here Javadoc links for every important data structure.
-
Interface Map<K,V> is an object that maps keys to values, no duplicates and each key can map to at most one value.
- HashMap<K,V> is a Hash table based implementation of the Map interface.
-
List is an ordered collection (also known as a sequence).
- ArrayList is a Resizable-array implementation of the List interface.
-
Set is a collection that contains no duplicate elements.
- HashSet implements the Set interface, backed by a hash table (actually a HashMap instance).
Design Patterns
ALL-IN-ONE awesome website for diving into design patterns!
Ohad Klein Design patterns Recommended
Factory
Decorator
Iterator
Strategy
Singleton
Observer
Derek Banas - Observer How The Observer Pattern Works
State
Memento
Facade
Dependecy Injection
Generics
Functional Programming
Algorithms
MinSumPath
/***
* Given a grid of integers of size N x M finds the minimal sum of the path from the upper left corner
* (0,0) to the bottom right corner (N-1, M-1)
* @param grid - assuming nut NULL.
* @return minimal sum of said path
*/
public static int CalculateMinSumPath(int[][] grid){
// this solution uses the same grid as the DP array. You can also create a new grid and fill it.
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
// if i = j = 0 do nothing.
if (i == 0 && j != 0)
grid[i][j] += grid[i][j - 1];
else if (i != 0 && j == 0)
grid[i][j] += grid[i - 1][0];
else if (i != 0 && j != 0)
grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
}
}
// return grid[n-1, m-1]
return grid[grid.length - 1][grid[0].length - 1];
}
}
Unique morse code words
// all characters' morse code representation, a to z.
private static final String[] MORSE_CODES = {".-", "-...", "-.-.", "-..",".", "..-.","--.","....","..",".---",
"-.-", ".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
/**
* find amount of unique morse code translations can be extracted from a list of words.
* @param words array of strings
* @return amount of unique morse codes.
*/
public static int uniqueMorseRepresentations(String[] words) {
// initialize hashSet for the morse translations.
Set<String> uniqueMorse = new HashSet<>();
for (String word : words) {
// build string one char at a time.
StringBuilder morse = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
// translate character to morse using the MORSE_CODE array.
char c = word.charAt(i);
morse.append(MORSE_CODES[c - 'a']);
}
// add the translation to hashSet.
uniqueMorse.add(morse.toString());
}
// size of set is number of unique morse codes.
return uniqueMorse.size();
}
Find duplicate
/**
* method finds a duplicate number in an array of N+1 ints, where numbers are in range 1->N.
* It is important that all numbers are positive.
* @param numList a list of N+1 numbers in range 1->N.
* @return the duplicate number.
*/
public static int findDuplicate(int[] numList) {
// create 2 pointers on the array
int i = numList[0];
int j = numList[0];
// iterate over the array as a "hashcode", each value is the hashcode for the next index.
// i jumps 1 time, j jumps twice. stop when they meet (a cycle has been closed).
do {
i = numList[i];
j = numList[numList[j]];
} while (i != j);
// find the originator of the cycle. use two pointers, one of the start and one of the end of the cycle.
// each time jump 1 index, and stop when we get to the duplicate.
int numCandidate1 = numList[0];
int numCandidate2 = i;
while (numCandidate1 != numCandidate2) {
numCandidate1 = numList[numCandidate1];
numCandidate2 = numList[numCandidate2];
}
return numCandidate2;
}
UML
Advanced topics in Java
Regex
RegexOne - A number of short and precise regex problems. General and not specifc to java.
Regex101 - A regex playground that supports java with problems to solve using regex.