Skip to content

Commit

Permalink
Merge pull request #38 from huaerhly/master
Browse files Browse the repository at this point in the history
week-two
  • Loading branch information
zavier authored Mar 3, 2017
2 parents 48ae950 + 429c46b commit 57a9ba4
Show file tree
Hide file tree
Showing 11 changed files with 646 additions and 0 deletions.
8 changes: 8 additions & 0 deletions group01/932573198/20170227/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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="lib" path="lib/dom4j-1.6.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group01/932573198/20170227/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group01/932573198/20170227/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>20170227</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>
11 changes: 11 additions & 0 deletions group01/932573198/20170227/.settings/org.eclipse.jdt.core.prefs
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
225 changes: 225 additions & 0 deletions group01/932573198/20170227/src/com/coderising/array/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package com.coderising.array;

public class ArrayUtil {

/**
* 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a =
* [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
*
* @param origin
* @return
*/
public void reverseArray(int[] origin) {
int l = origin.length, n;
for (int i = 0; i < l / 2; i++) {
n = origin[i];
origin[i] = origin[l - 1 - i];
origin[l - 1 - i] = n;
}
}

/**
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5}
*
* @param oldArray
* @return
*/

public int[] removeZero(int[] oldArray) {
int n = 0;
for (int i = 0; i < oldArray.length; i++) {
if (oldArray[i] == 0)
n++;
}
int[] newArray = new int[oldArray.length - n];
for (int i = 0, j = 0; i < oldArray.length; i++) {
if (oldArray[i] != 0) {
newArray[j] = oldArray[i];
j++;
}
}
return newArray;
}

/**
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 =
* [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
*
* @param array1
* @param array2
* @return
*/

public int[] merge(int[] array1, int[] array2) {
int n = 0; // 重复的整数个数
for (int i = 0, j = 0; i < array1.length && j < array2.length;) {
int z = array1[i] - array2[j];
if (z < 0)
i++;
else if (z > 0)
j++;
else {
i++;
j++;
n++;
}
}
int[] newArray = new int[array1.length + array2.length - n];

for (int i = 0, j = 0, k = 0; i < array1.length && j < array2.length;) {
int z = array1[i] - array2[j];
if (z < 0) {
newArray[k] = array1[i];
k++;
i++;
} else if (z > 0) {
newArray[k] = array2[j];
k++;
j++;
} else {
newArray[k] = array1[i];
k++;
i++;
j++;
}
// 判断循环是否即将结束,若结束则将另外一个数组未比较的元素放入新数组
if (i >= array1.length) {
for (int a = j; a < array2.length; a++, k++) {
newArray[k] = array2[a];
}
}
if (j >= array2.length) {
for (int a = i; a < array1.length; a++, k++) {
newArray[k] = array1[a];
}
}
}
return newArray;
}

/**
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
* 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
* [2,3,6,0,0,0]
*
* @param oldArray
* @param size
* @return
*/
public int[] grow(int[] oldArray, int size) {
int[] newArray = new int[oldArray.length + size];
for (int i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
return newArray;
}

/**
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 ,
* 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 []
*
* @param max
* @return
*/
public int[] fibonacci(int max) {
int n = 0;
for (int i = 1, j = 1, k; i < max; n++) {
k = i + j;
i = j;
j = k;
}
int[] newArray = new int[n];
if (n > 1) {
newArray[0] = 1;
newArray[1] = 1;
}
for (int i = 2; i < n; i++) {
newArray[i] = newArray[i - 1] + newArray[i - 2];
}
return newArray;
}

/**
* 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
*
* @param max
* @return
*/
public int[] getPrimes(int max) {
int n = 0;
for (int i = 2; i < max; i++) {
if (primeNumber(i))
n++;
}

int[] newArray = new int[n];
for (int i = 2, j = 0; i < max; i++) {
if (primeNumber(i)) {
newArray[j++] = i;
}
}
return newArray;
}

// 判断是否为素数
public boolean primeNumber(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0)
return false;
}
return true;
}

/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
*
* @param max
* @return
*/
public int[] getPerfectNumbers(int max) {
int n = 0;
for (int i = 6; i < max; i++) {
if (perfectNumber(i))
n++;
}

int[] newArray = new int[n];
for (int i = 6, j = 0; i < max; i++) {
if (perfectNumber(i)) {
newArray[j++] = i;
}
}
return newArray;
}

// 判断是否为完数
public boolean perfectNumber(int number) {
int n = 0;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0)
n += i;
}
if (number != n)
return false;
return true;
}

/**
* 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9"
*
* @param array
* @param s
* @return
*/
public String join(int[] array, String seperator) {
StringBuffer s = new StringBuffer();
for (int i = 0; i < array.length; i++) {
s.append(String.valueOf(array[i]));
s.append("-");
}
String str = s.substring(0, s.length() - 1);
return str;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.coderising.array;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ArrayUtilTest {

ArrayUtil util = null;

@Before
public void setUp() throws Exception {
util = new ArrayUtil();
}

@Test
public void testReverseArray() {
int[] a = {1,2,3,4};
int[] b = {4,3,2,1};
util.reverseArray(a);
Assert.assertArrayEquals(b, a);
int[] c = {1,2,3,4,5};
int[] d = {5,4,3,2,1};
util.reverseArray(c);
Assert.assertArrayEquals(d, c);
}

@Test
public void testRemoveZero() {
int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int[] newArray = {1,3,4,5,6,6,5,4,7,6,7,5};
int[] newArr = util.removeZero(oldArr);
Assert.assertArrayEquals(newArray, newArr);
}

@Test
public void testMerge() {
int[] a1 = {3, 5, 7, 8};
int[] a2 = {4, 5, 6, 7};
int[] newArray = {3,4,5,6,7,8};
int[] newArr = util.merge(a1, a2);
Assert.assertArrayEquals(newArray, newArr);
}

@Test
public void testGrow() {
int[] oldArray = {2,3,6};
int size = 3;
int[] newArray = {2,3,6,0,0,0};
int[] newArr = util.grow(oldArray, size);
Assert.assertArrayEquals(newArray, newArr);
}

@Test
public void testFibonacci() {
int[] arr1 = {1,1,2,3,5,8,13};
Assert.assertArrayEquals(arr1, util.fibonacci(15));
int[] arr2 = new int[0];
Assert.assertArrayEquals(arr2, util.fibonacci(1));
}

@Test
public void testGetPrimes() {
int[] arr1 = {2,3,5,7,11,13,17,19};
Assert.assertArrayEquals(arr1, util.getPrimes(23));
int[] arr2 = new int[0];
Assert.assertArrayEquals(arr2, util.getPrimes(2));
}

@Test
public void testGetPerfectNumbers() {
int[] arr = {6, 28, 496};
Assert.assertArrayEquals(arr, util.getPerfectNumbers(497));
}

@Test
public void testJoin() {
int[] arr = {1,2,3,4,5};
String s = "1-2-3-4-5";
Assert.assertEquals(s, util.join(arr, "-"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.coderising.litestruts;

/**
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
*
* @author liuxin
*
*/
public class LoginAction {
private String name;
private String password;
private String message;

public String getName() {
return name;
}

public String getPassword() {
return password;
}

public String execute() {
if ("test".equals(name) && "1234".equals(password)) {
this.message = "login successful";
return "success";
}
this.message = "login failed,please check your user/pwd";
return "fail";
}

public void setName(String name) {
this.name = name;
}

public void setPassword(String password) {
this.password = password;
}

public String getMessage() {
return this.message;
}

@Override
public String toString() {
return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]";
}

}
Loading

0 comments on commit 57a9ba4

Please sign in to comment.