forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from em14Vito/master
add homework
- Loading branch information
Showing
26 changed files
with
1,213 additions
and
0 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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>First_Homework1</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
2 changes: 2 additions & 0 deletions
2
group23/729693763/First_Homework1/.settings/org.eclipse.core.resources.prefs
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,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
11 changes: 11 additions & 0 deletions
11
group23/729693763/First_Homework1/.settings/org.eclipse.jdt.core.prefs
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,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
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,2 @@ | ||
It contain source code and Test file by JUnit, | ||
you can run SuitTest for testing whole file. |
117 changes: 117 additions & 0 deletions
117
group23/729693763/First_Homework1/src/com/danny/hw1/ArrayList.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,117 @@ | ||
package com.danny.hw1; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
|
||
import javax.print.attribute.Size2DSyntax; | ||
|
||
public class ArrayList implements List{ | ||
//ArrayList Size | ||
private int size=0; | ||
private int array_len=2; | ||
|
||
//total element in here | ||
private Object[] elementData = new Object[array_len]; | ||
|
||
@Override | ||
public void add(Object o) { | ||
// TODO Auto-generated method stub | ||
adjustCapacity(); | ||
this.elementData[size++] = o; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
// TODO Auto-generated method stub | ||
//Check range. if index is invalid,then would not add anythings | ||
rangeCheckForAdd(index); | ||
|
||
adjustCapacity(); | ||
System.arraycopy(elementData, index, elementData, | ||
index + 1,size - index); | ||
this.elementData[index] = o; | ||
this.size++; | ||
|
||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
// TODO Auto-generated method stub | ||
rangeCheckExist(index); | ||
|
||
return elementData[index]; | ||
|
||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
// TODO Auto-generated method stub | ||
rangeCheckExist(index); | ||
|
||
Object oldValue = elementData[index]; | ||
//deal with copy operation | ||
int moveStep = size - index - 1; | ||
if ( moveStep > 0 ) | ||
System.arraycopy(elementData, index+1, elementData, index,moveStep); | ||
elementData[--size] = null; // clear to let GC do its work | ||
|
||
return oldValue; | ||
|
||
} | ||
|
||
@Override | ||
public int size() { | ||
// TODO Auto-generated method stub | ||
return this.size; | ||
} | ||
|
||
//Get Iterator | ||
public Iterator iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
/******* Iterator *******/ | ||
private class ArrayListIterator implements Iterator{ | ||
|
||
private int currentIndex = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return currentIndex < size(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
rangeCheckExist(currentIndex); | ||
|
||
return elementData[currentIndex++]; | ||
} | ||
} | ||
|
||
|
||
/*******Inner function*******/ | ||
//Increase arraylist size | ||
private void adjustCapacity(){ | ||
//array length can not satisfy data size; | ||
if ( this.array_len < size() + 1 ) { | ||
this.array_len *= 2; | ||
this.elementData = Arrays.copyOf(elementData, array_len); | ||
} else { | ||
return ; | ||
} | ||
} | ||
|
||
private void rangeCheckForAdd(int index) { | ||
//Add operation is allow to add value in [ size() ] even if [ size() ] has not data | ||
if ( index > size() || index < 0 ) | ||
throw new IndexOutOfBoundsException("Invalid Adding Index:"+index); | ||
} | ||
private void rangeCheckExist(int index) { | ||
if ( index >= size() || index < 0 ) | ||
throw new IndexOutOfBoundsException("Invalid Index:"+index); | ||
} | ||
|
||
|
||
|
||
|
||
} |
101 changes: 101 additions & 0 deletions
101
group23/729693763/First_Homework1/src/com/danny/hw1/BinaryTreeNode.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,101 @@ | ||
package com.danny.hw1; | ||
|
||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class BinaryTreeNode { | ||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
if ( this.data == null && left == null && right == null ) { | ||
this.data = o; | ||
return this; | ||
} else { | ||
BinaryTreeNode temp = findNode(this, o); | ||
|
||
BinaryTreeNode newNode = new BinaryTreeNode(); | ||
newNode.data = o; | ||
//assert more than one null node in the temp(left,right,or both); | ||
if ( temp.compareTo(o) >= 0 ) { | ||
temp.left = newNode; | ||
} else { | ||
temp.right = newNode; | ||
} | ||
return newNode; | ||
} | ||
} | ||
|
||
public BinaryTreeNode() { | ||
// TODO Auto-generated constructor stub | ||
data=null; | ||
left =null; | ||
right = null; | ||
|
||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
private int compareTo(Object o){ | ||
// o1 > o2 == 1; o1 = o2 == 0 ; o1 < o2 == -1 | ||
//假设这个比较现在只用在数字上 | ||
return Integer.parseInt(this.data.toString()) - Integer.parseInt(o.toString()); | ||
} | ||
|
||
private static BinaryTreeNode findNode(BinaryTreeNode root,Object o){ | ||
if ( root.left == null && root.right == null || | ||
( root.compareTo(o) < 0 && root.right == null) || | ||
( root.compareTo(o) >= 0 && root.left == null) ){ | ||
return root; | ||
} else if ( root.compareTo(o) >= 0 ) { | ||
//root data is bigger than object | ||
return findNode(root.left, o); | ||
} else { | ||
return findNode(root.right, o); | ||
} | ||
// else if(root.compareTo(o) < 0 ){ | ||
// return findNode(root.right, o); | ||
// } | ||
// | ||
} | ||
|
||
//For Test value | ||
private Map<Object, Integer> teMap = new TreeMap<>(); | ||
public void printWholeTree(BinaryTreeNode root,int layer){ | ||
if(root == null) { | ||
return ; | ||
} | ||
teMap.put(root.data,layer); | ||
|
||
layer++; | ||
printWholeTree(root.left,layer); | ||
printWholeTree(root.right,layer); | ||
|
||
} | ||
|
||
public Map<Object,Integer > getTeMap() { | ||
return teMap; | ||
} | ||
|
||
public void setTeMap(Map<Object, Integer> teMap) { | ||
this.teMap = teMap; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
group23/729693763/First_Homework1/src/com/danny/hw1/Iterator.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,6 @@ | ||
package com.danny.hw1; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
Oops, something went wrong.