Last active
November 21, 2016 17:38
-
-
Save xtpor/6938ebe450eaaa069dea1454d9d31fb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Programming 101 | |
- function | |
- parameters | |
- arguments | |
- return type | |
- return value | |
- scope | |
``` | |
int add (int a, int b) { | |
return a + b; | |
} | |
//-- | |
int x = add(1, 2); | |
``` | |
## OOP 101 | |
class | |
- constructor (keyword `new`) | |
- method / function | |
- fields / attributes | |
- class vs. instance (object) | |
- keyword `this` | |
``` | |
int add (? this, int a, int b) { | |
} | |
//-- | |
int x = a[0].b().obj.add(1, 2); | |
``` | |
## inheritance | |
- superclass | |
- subclass | |
- keyword `extends` | |
- abstract class | |
public class Hello { | |
// attributes | |
private int a; | |
public static void main (String[] argv) { | |
int a = 5; | |
int b = 5; | |
System.out.println("hello world!"); | |
} | |
public int add (int b) { | |
int a = 10; | |
return this.;a + b; | |
} | |
} | |
class A { | |
A () { | |
this(...) | |
... | |
} | |
A (...) { | |
} | |
void foo () { | |
System.out.println("foo"); | |
} | |
} | |
class B { | |
B () { | |
... | |
} | |
void bar () { | |
System.out.println("bar"); | |
} | |
} | |
// foo, foobar | |
class C extends A { | |
C () { | |
super(); # A() | |
} | |
void foo() { | |
System.out.println(); | |
super.foo(); | |
} | |
void foobar () { | |
System.out.println("foobar"); | |
} | |
} | |
// foo, barfoo | |
class D extends A { | |
void barfoo () { | |
System.out.println("barfoo"); | |
} | |
} | |
Object | |
/ \ | |
A B | |
|\ | |
C D | |
A is superclass/baseclass of C | |
A is superclass/baseclass of D | |
C is subclass of A | |
D is subclass of A | |
Object - toString() | |
| | |
*Food - foo | |
/ \ | |
*JunkFood *HealthyFood | |
/ / \ | |
Coke Apple Orange - bar | |
ImageViewer a = new PNGViewer(); | |
ImageViewer b = new JPGViewer(); | |
ImageViewer c = new GIFViewer(); | |
ImageViewer d = new BMPViewer(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum(10) # 1 + 2 + 3 + ... + 10 | |
1. sum(1) = 1 | |
2. sum(n) = n + sum(n - 1) | |
public static int sum (int n) { | |
if (n == 1) { | |
return 1; | |
} else { | |
return n + sum(n - 1); | |
} | |
} | |
sum(4) | |
= 4 + sum(3) # case 2 | |
= 4 + 3 + sum(2) # case 2 | |
= 4 + 3 + 2 + sum(1) # case 2 | |
= 4 + 3 + 2 + 1 # case 1 | |
= 10 | |
//-- | |
incr(3) = 4 | |
incr(4) = 5 | |
desc(4) = 3 | |
desc(3) = 2 | |
add(a, b) = a + b | |
rule 1: add(0, y) = y | |
rule 2: add(x, y) = add(x - 1, y + 1) | |
add(3, 5) | |
= add(2, 6) # rule 2 | |
= add(1, 7) # rule 2 | |
= add(0, 8) # rule 2 | |
= 8 # rule 1 | |
public static int add(int x, int y) { | |
if (x == 0) { | |
return y; | |
} else { | |
return add(x - 1, y + 1); | |
} | |
} | |
//-- | |
fac(3) = 3 * 2 * 1 | |
rule 1: fac(0) = 0 | |
rule 2: fac(n) = n * fac(n - 1) | |
fac(4) | |
= 4 * fac(3) #2 | |
= 4 * 3 * fac(2) #2 | |
= 4 * 3 * 2 * fac(1) #2 | |
= 4 * 3 * 2 * 1 * fac(0) #2 | |
= 4 * 3 * 2 * 1 * 1 #1 | |
= 24 | |
//-- | |
gt(5, 3) = true | |
gt(5, 3) #3 | |
= gt(4, 2) #3 | |
= gt(3, 1) #3 | |
= gt(2, 0) #3 | |
= true #2 | |
gt(2, 6) #3 | |
= gt(1, 5) #3 | |
= gt(0, 4) #3 | |
= false #1 | |
#1: gt(0, y) = false | |
#2: gt(x, 0) = true | |
#3: gt(x, y) = gt(x - 1, y - 1) | |
public static boolean gt (int x, int y) { | |
if (x == 0) { | |
return false; | |
} else if (y == 0) { | |
return true; | |
} else { | |
return gt(x - 1, y - 1); | |
} | |
} | |
//-- | |
print(5) # 5, [4, 3, 2, 1] | |
print(5) | |
=> print(4) #2 | |
=> print(3) #2 | |
=> print(2) #2 | |
=> print(1) #1 | |
print(1) => output(1) | |
print(n) => output(n); print(n -1) | |
public static void print (int n) { | |
if (n == 1) { | |
System.out.println(1); | |
/* do nothing */ | |
} else { | |
System.out.println(n); | |
print(n - 1); | |
} | |
} | |
public static void print (int n) { | |
System.out.println(n); | |
if (n == 1) { | |
/* do nothing */ | |
} else { | |
print(n - 1); | |
} | |
} | |
public static void print (int n) { | |
System.out.println(n); | |
if (n != 1) { | |
print(n - 1); | |
} | |
} | |
public static void print (int n) { | |
System.out.println(n); | |
if (n != 1) print(n - 1); | |
} | |
//-- | |
print(5) # [1, 2, 3, 4], 5 | |
print(4) # [1, 2, 3], 4 | |
print(3) # [1, 2], 3 | |
print(2) # [1], 2 | |
print(1) # 1 | |
print(5) | |
=> print(4); output(5) | |
=> print(3); output(4); output(5) | |
=> print(2); output(3); output(4); output(5) | |
=> print(1); output(2); output(3); output(4); output(5) | |
=> output(1); output(2); output(3); output(4); output(5) | |
print(1) => output(1); | |
print(n) => print(n - 1); output(n); | |
public static void print (int n) { | |
if (n == 1) { | |
System.out.println(1); | |
} else { | |
print(n -1); | |
System.out.println(n); | |
} | |
} | |
public static void print (int n) { | |
if (n == 1) { | |
} else { | |
print(n -1); | |
} | |
System.out.println(n); | |
} | |
public static void print (int n) { | |
if (n != 1) { | |
print(n -1); | |
} | |
System.out.println(n); | |
} | |
public static void print (int n) { | |
if (n != 1) print(n - 1); | |
System.out.println(n); | |
} | |
//-- | |
head([1, 2, 3]) = 1; | |
tail([1, 2, 3]) = [2, 3]; | |
elementAt([a, b, c, d], 2) = c | |
elementAt([a, b, c, d], 2) | |
= elementAt([b, c, d], 1) | |
= elementAt([c, d], 0) | |
= c | |
elementAt(array, 0) = head(array) | |
elementAt(array, i) = elementAt(tail(array), i - 1) | |
//-- | |
head, tail, max | |
max(2, 3) = 3 | |
max(5, 10) = 10 | |
arrayMax([7, 2, 9, 4]) = 9 | |
arrayMax([7, 2, 9, 4]) | |
= max(7, arrayMax([2, 9, 4])) | |
= max(7, max(2, arrayMax([9, 4]))) | |
= max(7, max(2, max(9, arrayMax([4])))) | |
= max(7, max(2, max(9, 4))) | |
= max(7, max(2, 9)) | |
= max(7, 9) | |
= 9 | |
#1: length == 1 ? arrayMax(array) = head(array) | |
#2: length != 1 ? arrayMax(array) = max(head(array), arrayMax(tail(array))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main (String[] argv) { | |
B b = new B("hello world"); | |
b.bar(); | |
} | |
} | |
class A { | |
private String foo; | |
public int a; | |
public A (String f) { | |
System.out.println("Call A constructor"); | |
a = 5; | |
foo = f; | |
} | |
public void bar() { | |
System.out.println(foo); | |
} | |
} | |
class B extends A { | |
public int a; | |
// auto | |
public B (String a) { | |
super(a); | |
this.a = 6; | |
System.out.println("Call B constructor"); | |
} | |
public void bar() { | |
int a = 4; | |
System.out.println("Predefined constant"); | |
System.out.println("A's a = " + super.a); | |
System.out.println("B's a = " + this.a); | |
System.out.println("local a = " + a); | |
super.bar(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment