-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
005261d
commit d04aa6c
Showing
19 changed files
with
811 additions
and
21 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# FileName: Makefile | ||
# Author: Fasion Chan | ||
# @contact: fasionchan@gmail.com | ||
# @version: $Id$ | ||
# | ||
# Description: | ||
# | ||
# Changelog: | ||
# | ||
|
||
build: | ||
javac CommandLineArgs.java | ||
|
||
run: | ||
java CommandLineArgs | ||
|
||
clean: | ||
rm -rf *.class |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Author: fasion | ||
# Created time: 2019-07-20 14:56:34 | ||
# Last Modified by: fasion | ||
# Last Modified time: 2019-07-22 09:27:55 | ||
|
||
build: | ||
javac HelloWorld.java | ||
|
||
run: | ||
java HelloWorld | ||
|
||
clean: | ||
rm -rf *.class |
43 changes: 43 additions & 0 deletions
43
src/java/base/ObjectOriented/encapsulation/Counter/Counter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Author: fasion | ||
* Created time: 2019-07-20 15:38:55 | ||
* Last Modified by: fasion | ||
* Last Modified time: 2019-07-22 16:03:33 | ||
*/ | ||
|
||
public class Counter { | ||
int count; | ||
|
||
public Counter() { | ||
count = 0; | ||
} | ||
|
||
public void add() { | ||
count += 1; | ||
} | ||
|
||
public void reset() { | ||
count = 0; | ||
} | ||
|
||
public int get() { | ||
return count; | ||
} | ||
|
||
public static void main(String args[]) { | ||
Counter counter = new Counter(); | ||
System.out.printf("Initial: %d\n", counter.get()); | ||
|
||
counter.add(); | ||
System.out.printf("After first add: %d\n", counter.get()); | ||
|
||
counter.add(); | ||
System.out.printf("After second add: %d\n", counter.get()); | ||
|
||
counter.reset(); | ||
System.out.printf("After reset: %d\n", counter.get()); | ||
|
||
counter.add(); | ||
System.out.printf("After another add: %d\n", counter.get()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/java/base/ObjectOriented/encapsulation/Counter/Makefile
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Author: fasion | ||
# Created time: 2019-07-21 09:25:54 | ||
# Last Modified by: fasion | ||
# Last Modified time: 2019-07-22 09:27:32 | ||
|
||
build: | ||
javac Counter.java | ||
|
||
run: | ||
java Counter | ||
|
||
clean: | ||
rm -rf *.class |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# FileName: Makefile | ||
# Author: Fasion Chan | ||
# @contact: fasionchan@gmail.com | ||
# @version: $Id$ | ||
# | ||
# Description: | ||
# | ||
# Changelog: | ||
# | ||
|
||
build: | ||
javac Fibonacci.java | ||
|
||
run: | ||
java Fibonacci | ||
|
||
clean: | ||
rm -rf *.class |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Author: fasion | ||
* Created time: 2019-07-20 16:16:06 | ||
* Last Modified by: fasion | ||
* Last Modified time: 2019-07-22 13:59:07 | ||
*/ | ||
|
||
public class ArrayList<AnyType> implements Iterable<AnyType> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
|
||
private int listSize; | ||
private AnyType[] listItems; | ||
|
||
public ArrayList() { | ||
listItems = (AnyType[]) new Object[DEFAULT_CAPACITY]; | ||
listSize = 0; | ||
} | ||
|
||
public int size() { | ||
return listSize; | ||
} | ||
|
||
public int capacity() { | ||
return listItems.length; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size() == 0; | ||
} | ||
|
||
public boolean isFull() { | ||
return size() == capacity(); | ||
} | ||
|
||
public AnyType get(int idx) { | ||
if (idx < 0 || idx >= size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
return listItems[idx]; | ||
} | ||
|
||
public AnyType set(int idx, AnyType newVal) { | ||
if (idx < 0 || idx >= size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
|
||
AnyType old = listItems[idx]; | ||
listItems[idx] = newVal; | ||
|
||
return old; | ||
} | ||
|
||
public void ensureCapacity(int newCapacity) { | ||
if (newCapacity <= capacity()) { | ||
return; | ||
} | ||
|
||
AnyType [] oldItems = listItems; | ||
|
||
listItems = (AnyType []) new Object[newCapacity]; | ||
for (int i = 0; i < size(); i++) { | ||
listItems[i] = oldItems[i]; | ||
} | ||
} | ||
|
||
public void add(int idx, AnyType x) { | ||
if (size() == capacity()) { | ||
ensureCapacity(capacity() * 2); | ||
} | ||
|
||
for (int i = size(); i > idx; i--) { | ||
listItems[i] = listItems[i-1]; | ||
} | ||
|
||
listItems[idx] = x; | ||
listSize++; | ||
} | ||
|
||
public void add(AnyType x) { | ||
add(size(), x); | ||
} | ||
|
||
public AnyType remove(int idx) { | ||
if (idx < 0 || idx >= size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
|
||
AnyType value = listItems[idx]; | ||
|
||
for (int i = idx; i < size() - 1; i++) { | ||
listItems[i] = listItems[i+1]; | ||
} | ||
|
||
listSize--; | ||
|
||
return value; | ||
} | ||
|
||
public void print(String hint) { | ||
System.out.printf("%s[", hint); | ||
|
||
for (int i = 0; i < listSize; i++) { | ||
System.out.printf("%s ", listItems[i]); | ||
} | ||
|
||
System.out.printf("]\n"); | ||
} | ||
|
||
private class ArrayListIterator implements java.util.Iterator<AnyType> { | ||
private int current = 0; | ||
|
||
public boolean hasNext() { | ||
return current < size(); | ||
} | ||
|
||
public AnyType next() { | ||
if (!hasNext()) { | ||
throw new java.util.NoSuchElementException(); | ||
} | ||
return listItems[current++]; | ||
} | ||
|
||
public void remove() { | ||
ArrayList.this.remove(--current); | ||
} | ||
} | ||
|
||
public java.util.Iterator<AnyType> iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
public static void main(String args[]) { | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
list.print("After init: "); | ||
System.out.println(); | ||
|
||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
|
||
list.print("After add 1, 2, 3: "); | ||
System.out.printf("#1 is: %d\n", list.get(1)); | ||
System.out.println(); | ||
|
||
list.add(0, -1); | ||
list.print("After insert -1: "); | ||
System.out.println(); | ||
|
||
list.remove(1); | ||
list.print("After remove #1: "); | ||
System.out.println(); | ||
|
||
list.add(4); | ||
list.print("After append 4: "); | ||
System.out.println(); | ||
|
||
for (int i = 5; i < 20; i++) { | ||
list.add(i); | ||
} | ||
|
||
list.print("Finally: "); | ||
System.out.println(); | ||
|
||
System.out.println("iterate over list"); | ||
java.util.Iterator<Integer> it = list.iterator(); | ||
while (it.hasNext()) { | ||
System.out.println(it.next()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Author: fasion | ||
# Created time: 2019-07-20 14:56:34 | ||
# Last Modified by: fasion | ||
# Last Modified time: 2019-07-22 13:28:43 | ||
|
||
build: | ||
javac ArrayList.java | ||
|
||
run: | ||
java ArrayList | ||
|
||
clean: | ||
rm -rf *.class |
Oops, something went wrong.