Skip to content

Commit

Permalink
Merge pull request onlyliuxin#32 from tanghaojie/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
guodongym authored Mar 6, 2017
2 parents 91a884e + f367373 commit eeedb24
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
eclipse.preferences.version=1
encoding//src/Week2/array/ArrayUtil.java=UTF-8
encoding//src/week2/array/ArrayUtil.java=UTF-8
encoding/src=UTF-8
15 changes: 0 additions & 15 deletions group12/495473393/Code/src/Week2/array/Main.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public class ArrayList implements List {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public class BinaryTreeNode {
private Object data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public interface Iterator {
public boolean hasNext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public class LinkedList implements List {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public interface List {
public void add(Object o);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public class Queue {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week1;
package week1;

public class Stack {
private ArrayList elementData = new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week2.array;
package week2.array;

import java.util.Arrays;

Expand All @@ -12,6 +12,9 @@ public class ArrayUtil {
* @return
*/
public void reverseArray(int[] origin) {
if (origin == null) {
return;
}
int len = origin.length;
int forLen = len / 2;
int temp;
Expand All @@ -31,6 +34,9 @@ public void reverseArray(int[] origin) {
*/

public int[] removeZero(int[] oldArray) {
if (oldArray == null) {
return null;
}
int[] newArray = new int[oldArray.length];
int index = 0;
for (int x : oldArray) {
Expand All @@ -52,6 +58,13 @@ public int[] removeZero(int[] oldArray) {
*/

public int[] merge(int[] array1, int[] array2) {
if (array1 == null && array2 == null) {
return null;
} else if (array1 == null) {
return array2;
} else if (array2 == null) {
return array1;
}
int[] newArray = new int[array1.length + array2.length];
int newIndex = 0;
int index1 = 0;
Expand Down Expand Up @@ -97,7 +110,13 @@ public int[] merge(int[] array1, int[] array2) {
* @return
*/
public int[] grow(int[] oldArray, int size) {
return null;
if (oldArray == null) {
return null;
}
if (size < 0) {
size = 0;
}
return Arrays.copyOf(oldArray, oldArray.length + size);
}

/**
Expand All @@ -108,17 +127,57 @@ public int[] grow(int[] oldArray, int size) {
* @return
*/
public int[] fibonacci(int max) {
return null;
if (max <= 1) {
return null;
}
int now = 2;
int index = 1;
int[] fiboArray = new int[] { 1, 1 };
while (now < max) {
now = fiboArray[index] + fiboArray[index - 1];
if (index + 2 > fiboArray.length) {
fiboArray = Arrays.copyOf(fiboArray, fiboArray.length * 2);
}
fiboArray[++index] = now;
}
return Arrays.copyOf(fiboArray, index);
}

/**
* 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
*
* @param max
* @return
* @throws Exception
*/
public int[] getPrimes(int max) {
return null;
public int[] getPrimes(int max) throws Exception {
if (max < 2) {
return null;
}
int[] primes = new int[] { 2 };
int index = 0;
int newPrimes = 3;
while (newPrimes < max) {
if (index + 2 > primes.length) {
primes = Arrays.copyOf(primes, primes.length * 2);
}
primes[++index] = newPrimes;

boolean foundPrime = false;
while (!foundPrime) {
newPrimes += 2;
foundPrime = true;
int mid = newPrimes / 2 + 1;
for (int i = 3; i <= mid; i++) {
if (newPrimes % i == 0) {
foundPrime = false;
break;
}
}
}
}

return Arrays.copyOf(primes, ++index);
}

/**
Expand All @@ -128,7 +187,28 @@ public int[] getPrimes(int max) {
* @return
*/
public int[] getPerfectNumbers(int max) {
return null;
int perfectNumber = 6;
if (max < perfectNumber) {
return null;
}
int[] perfectNumbers = new int[] { perfectNumber };

while (perfectNumber < max) {
perfectNumber++;
int sum = 0;
for (int i = 1; i < perfectNumber; i++) {
if (perfectNumber % i == 0) {
sum += i;
}
}
if (sum == perfectNumber) {
int[] newArr = new int[perfectNumbers.length + 1];
System.arraycopy(perfectNumbers, 0, newArr, 0, perfectNumbers.length);
perfectNumbers = newArr;
perfectNumbers[perfectNumbers.length - 1] = perfectNumber;
}
}
return perfectNumbers;
}

/**
Expand All @@ -139,7 +219,15 @@ public int[] getPerfectNumbers(int max) {
* @return
*/
public String join(int[] array, String seperator) {
return null;
if (array == null || array.length == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append(array[0]);
for (int i = 1; i < array.length; i++) {
sb.append(seperator + array[i]);
}
return sb.toString();
}

}
150 changes: 150 additions & 0 deletions group12/495473393/Code/src/week2/array/ArrayUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
*
*/
package week2.array;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* @author TangHaoJie
*
*/
public class ArrayUtilTest {

private ArrayUtil au;

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
au = new ArrayUtil();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* {@link week2.array.ArrayUtil#reverseArray(int[])} 的测试方法。
*/
@Test
public void testReverseArray() {
int[] a1 = null;
au.reverseArray(a1);
assertArrayEquals(null, a1);

int[] a2 = new int[0];
au.reverseArray(a2);
assertArrayEquals(new int[0], a2);

int[] a3 = new int[] { 1, 2, 3, 4, 5, 6 };
au.reverseArray(a3);
assertArrayEquals(new int[] { 6, 5, 4, 3, 2, 1 }, a3);
}

/**
* {@link week2.array.ArrayUtil#removeZero(int[])} 的测试方法。
*/
@Test
public void testRemoveZero() {
int[] a1 = null;
int[] b1 = au.removeZero(a1);
assertArrayEquals(b1, a1);

int[] a2 = new int[0];
int[] b2 = au.removeZero(a2);
assertArrayEquals(b2, a2);

int[] a3 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] b3 = au.removeZero(a3);
assertArrayEquals(b3, a3);

int[] a4 = new int[] { 0, 0, 1, 2, 0, 3, 4, 0, 5, 6 };
int[] b4 = au.removeZero(a4);
assertArrayEquals(b4, new int[] { 1, 2, 3, 4, 5, 6 });

int[] a5 = new int[] { 1, 2, 0, 3, 4, 0, 5, 6, 0, 0, 0 };
int[] b5 = au.removeZero(a5);
assertArrayEquals(b5, new int[] { 1, 2, 3, 4, 5, 6 });
}

/**
* {@link week2.array.ArrayUtil#merge(int[], int[])} 的测试方法。
*/
@Test
public void testMerge() {
int[] a1 = null;
int[] b1 = null;
int[] c1 = au.merge(a1, b1);
assertArrayEquals(c1, null);

int[] a2 = new int[0];
int[] b2 = new int[0];
int[] c2 = au.merge(a2, b2);
assertArrayEquals(c2, new int[0]);

int[] a3 = new int[] { 1, 3, 5, 7, 9 };
int[] b3 = new int[] { 2, 4, 6, 8, 10 };
int[] c3 = au.merge(a3, b3);
assertArrayEquals(c3, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
}

/**
* {@link week2.array.ArrayUtil#grow(int[], int)} 的测试方法。
*/
@Test
public void testGrow() {
int[] a1 = null;
int[] b1 = au.grow(a1, 0);
assertArrayEquals(b1, a1);

int[] a2 = new int[0];
int[] b2 = au.grow(a2, 0);
assertArrayEquals(b2, a2);

int[] a3 = new int[] { 1, 2 };
int[] b3 = au.grow(a3, -10);
assertArrayEquals(b3, a3);

int[] a4 = new int[] { 1, 2 };
int[] b4 = au.grow(a4, 5);
assertArrayEquals(b4, new int[] { 1, 2, 0, 0, 0, 0, 0 });
}

/**
* {@link week2.array.ArrayUtil#fibonacci(int)} 的测试方法。
*/
@Test
public void testFibonacci() {
}

/**
* {@link week2.array.ArrayUtil#getPrimes(int)} 的测试方法。
*/
@Test
public void testGetPrimes() {
}

/**
* {@link week2.array.ArrayUtil#getPerfectNumbers(int)} 的测试方法。
*/
@Test
public void testGetPerfectNumbers() {
}

/**
* {@link week2.array.ArrayUtil#join(int[], java.lang.String)} 的测试方法。
*/
@Test
public void testJoin() {
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week2.litestruts;
package week2.litestruts;

/**
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week2.litestruts;
package week2.litestruts;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Week2.litestruts;
package week2.litestruts;

import java.util.HashMap;
import java.util.Map;
Expand Down
Loading

0 comments on commit eeedb24

Please sign in to comment.