From 9b553a515004aa6fc78dd3b17f4735ec07171247 Mon Sep 17 00:00:00 2001 From: Tennyson Date: Wed, 1 Mar 2017 20:05:23 +0800 Subject: [PATCH 1/8] ArrayUtil --- .../296933284/DataStructuresTest/.gitignore | 14 -- .../{DataStructuresTest => coding}/.classpath | 5 +- group14/296933284/coding/.gitignore | 6 + .../{DataStructuresTest => coding}/.project | 0 .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 0 .../src/com/coderising/array/ArrayUtil.java | 199 ++++++++++++++++++ .../com/coderising/array/ArrayUtilTest.java | 105 +++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../src/com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 41 ++++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/struts.xml | 11 + .../src/com/coding/basic/ArrayList.java | 0 .../com/coding/basic/BinarySearchTree.java | 0 .../src/com/coding/basic/Iterator.java | 0 .../src/com/coding/basic/JavaTest.java | 0 .../src/com/coding/basic/LinkedList.java | 1 - .../src/com/coding/basic/List.java | 0 .../src/com/coding/basic/Queue.java | 0 .../src/com/coding/basic/Stack.java | 0 21 files changed, 463 insertions(+), 17 deletions(-) delete mode 100644 group14/296933284/DataStructuresTest/.gitignore rename group14/296933284/{DataStructuresTest => coding}/.classpath (57%) create mode 100644 group14/296933284/coding/.gitignore rename group14/296933284/{DataStructuresTest => coding}/.project (100%) create mode 100644 group14/296933284/coding/.settings/org.eclipse.core.resources.prefs rename group14/296933284/{DataStructuresTest => coding}/.settings/org.eclipse.jdt.core.prefs (100%) create mode 100644 group14/296933284/coding/src/com/coderising/array/ArrayUtil.java create mode 100644 group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java create mode 100644 group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java create mode 100644 group14/296933284/coding/src/com/coderising/litestruts/Struts.java create mode 100644 group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group14/296933284/coding/src/com/coderising/litestruts/View.java create mode 100644 group14/296933284/coding/src/com/coderising/litestruts/struts.xml rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/ArrayList.java (100%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/BinarySearchTree.java (100%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/Iterator.java (100%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/JavaTest.java (100%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/LinkedList.java (99%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/List.java (100%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/Queue.java (100%) rename group14/296933284/{DataStructuresTest => coding}/src/com/coding/basic/Stack.java (100%) diff --git a/group14/296933284/DataStructuresTest/.gitignore b/group14/296933284/DataStructuresTest/.gitignore deleted file mode 100644 index e32d56a40c..0000000000 --- a/group14/296933284/DataStructuresTest/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ -/bin/ diff --git a/group14/296933284/DataStructuresTest/.classpath b/group14/296933284/coding/.classpath similarity index 57% rename from group14/296933284/DataStructuresTest/.classpath rename to group14/296933284/coding/.classpath index 373dce4005..83169ef5db 100644 --- a/group14/296933284/DataStructuresTest/.classpath +++ b/group14/296933284/coding/.classpath @@ -1,7 +1,8 @@ - - + + + diff --git a/group14/296933284/coding/.gitignore b/group14/296933284/coding/.gitignore new file mode 100644 index 0000000000..1ea200494a --- /dev/null +++ b/group14/296933284/coding/.gitignore @@ -0,0 +1,6 @@ +/bin/ +.eml +.iml +.idea +/lib/ +*.eml \ No newline at end of file diff --git a/group14/296933284/DataStructuresTest/.project b/group14/296933284/coding/.project similarity index 100% rename from group14/296933284/DataStructuresTest/.project rename to group14/296933284/coding/.project diff --git a/group14/296933284/coding/.settings/org.eclipse.core.resources.prefs b/group14/296933284/coding/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..9abffe377f --- /dev/null +++ b/group14/296933284/coding/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/litestruts/StrutsTest.java=UTF-8 diff --git a/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs b/group14/296933284/coding/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs rename to group14/296933284/coding/.settings/org.eclipse.jdt.core.prefs diff --git a/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java b/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7ff23cc368 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,199 @@ +package com.coderising.array; + +import com.coding.basic.ArrayList; + +import java.util.Arrays; + + +/** + * Created by Tennyson on 2017/3/1. + */ +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) { + for (int i = 0, j = origin.length - 1; i < origin.length / 2; i++, j--) { + int temp = origin[j]; + origin[j] = origin[i]; + origin[i] = temp; + } + } + + /** + * 现在有如下的一个数组: 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 i; + // 获取数组中第一个值为 0 的元素的位置 + for (i = 0; i < oldArray.length; i++) + if (oldArray[i] == 0) + break; + // 从第一个值为 0 的元素开始,用之后的非零元素覆盖之前值为 0 的元素,并记录非零元素的个数 + for (int j = i + 1; j < oldArray.length; j++) + if (oldArray[j] != 0) + oldArray[i++] = oldArray[j]; + + // 复制数组 + int[] newArray = new int[i]; + System.arraycopy(oldArray, 0, newArray, 0, i); + + 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 i = 0, j = 0; + ArrayList arrayList = new ArrayList(); + + // 比较两个数组的元素值,将较小的值存入arrayList + while (i < array1.length && j < array2.length) { + + if (array1[i] == array2[j]) { + arrayList.add(array1[i]); + i++; + j++; + } else if (array1[i] < array2[j]) { + arrayList.add(array1[i++]); + } else if (array1[i] > array2[j]) { + arrayList.add(array2[j++]); + } + } + + // 剩下一个数组还未比较完, 将其剩余元素存入arrayList + while (i < array1.length) arrayList.add(array1[i++]); + while (j < array2.length) arrayList.add(array2[j++]); + + return ArrayUtil.toArray(arrayList); + } + /** + * 把一个已经存满数据的数组 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]; + + return newArray = Arrays.copyOf(oldArray, newArray.length); + } + + /** + * 斐波那契数列为: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) { + + ArrayList arrayList = new ArrayList(); + arrayList.add(1); + arrayList.add(1); + + int i, num; + for (i = 2; (num = (int) (arrayList.get(i - 1)) + (int) (arrayList.get(i - 2))) < max; i++) + arrayList.add(num); + + return ArrayUtil.toArray(arrayList); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + + ArrayList arrayList = new ArrayList(); + + for (int i = 2; i < max; i++) { + int k = (int) Math.sqrt(i), j; + for (j = 2; j <= k; j++) + if (i % j == 0) break; + + // 若是 j >= k + 1 表示 i 没有被整除 即 i 为素数 + if (j >= k + 1) + arrayList.add(i); + } + + return ArrayUtil.toArray(arrayList); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + ArrayList arrayList = new ArrayList(); + + for (int i = 6; i < max; i++) { + int factorSum = 0; + + for (int j = 1; j < i; j++) + if (i % j == 0) + factorSum += j; + + if (factorSum == i) + arrayList.add(i); + + } + + return ArrayUtil.toArray(arrayList); + } + + // 将ArrayList对象转换为数组返回 + private static int[] toArray(ArrayList arrayList) { + int[] array = new int[arrayList.size()]; + + for (int i = 0; i < array.length; i++) + array[i] = (Integer) arrayList.get(i); + + return array; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + + StringBuffer stringBuffer = new StringBuffer(); + + for (int i = 0; i < array.length - 1; i++) + stringBuffer.append("" + array[i] + seperator); + + stringBuffer.append(array[array.length - 1]); + + return stringBuffer.toString(); + } + + +} diff --git a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..5605b5064f --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,105 @@ +package com.coderising.array; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by damocles on 2017/3/1. + */ +public class ArrayUtilTest { + + @Test + public void reverseArray() throws Exception { + int[] a = {7, 9 , 30, 3}; + ArrayUtil arrayUtil = new ArrayUtil(); + + arrayUtil.reverseArray(a); + + for (int i = 0; i < a.length; i++) + System.out.print(a[i] + "\t"); + + int[] b = {3, 30, 9, 7, 4}; + + System.out.println(); + + arrayUtil.reverseArray(b); + + for (int i = 0; i < b.length; i++) + System.out.print(b[i] + "\t"); + } + + @Test + public void removeZero() throws Exception { + int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + + ArrayUtil arrayUtil = new ArrayUtil(); + int[] newArr = arrayUtil.removeZero(oldArr); + + for (int i = 0; i < newArr.length; i++) + System.out.print(newArr[i] + "\t"); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}, a2 = {4, 5, 6,7}; + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a3 = arrayUtil.merge(a1, a2); + + for (int i = 0; i < a3.length; i++) + System.out.print(a3[i] + "\t"); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2, 3, 6}; + int size = 3; + + ArrayUtil arrayUtil = new ArrayUtil(); + int[] newArray = arrayUtil.grow(oldArray, size); + + for (int i = 0; i < newArray.length; i++) + System.out.print(newArray[i] + "\t"); + } + + @Test + public void fibonacci() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] newArray = arrayUtil.fibonacci(15); + + for (int i = 0; i < newArray.length; i++) + System.out.print(newArray[i] + "\t"); // 1 1 2 3 5 8 13 + } + + @Test + public void getPrimes() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] newArray = arrayUtil.getPrimes(23); + + for (int i = 0; i < newArray.length; i++) + System.out.print(newArray[i] + "\t"); // [2,3,5,7,11,13,17,19] + } + + @Test + public void getPerfectNumbers() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] newArray = arrayUtil.getPerfectNumbers(500); + + for (int i = 0; i < newArray.length; i++) + System.out.print(newArray[i] + "\t"); // [6 ,28 ,496] + + } + + @Test + public void join() throws Exception { + int[] array= {3, 8, 9, 10, 2}; + String seperator = "-"; + + ArrayUtil arrayUtil = new ArrayUtil(); + String str = arrayUtil.join(array, seperator); + + System.out.println(str); + } + +} \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java b/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +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; + } +} diff --git a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..1abc2889a3 --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷� + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group14/296933284/coding/src/com/coderising/litestruts/View.java b/group14/296933284/coding/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group14/296933284/coding/src/com/coderising/litestruts/struts.xml b/group14/296933284/coding/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group14/296933284/coding/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java b/group14/296933284/coding/src/com/coding/basic/ArrayList.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java rename to group14/296933284/coding/src/com/coding/basic/ArrayList.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java b/group14/296933284/coding/src/com/coding/basic/BinarySearchTree.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java rename to group14/296933284/coding/src/com/coding/basic/BinarySearchTree.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java b/group14/296933284/coding/src/com/coding/basic/Iterator.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java rename to group14/296933284/coding/src/com/coding/basic/Iterator.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java b/group14/296933284/coding/src/com/coding/basic/JavaTest.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java rename to group14/296933284/coding/src/com/coding/basic/JavaTest.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java b/group14/296933284/coding/src/com/coding/basic/LinkedList.java similarity index 99% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java rename to group14/296933284/coding/src/com/coding/basic/LinkedList.java index 2a397e9bec..cb5f1e99f8 100644 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java +++ b/group14/296933284/coding/src/com/coding/basic/LinkedList.java @@ -2,7 +2,6 @@ import java.util.Collection; -import org.junit.Test; /** * LinkedList (ͷĵ) ʵ 14С 296933284 diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java b/group14/296933284/coding/src/com/coding/basic/List.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/List.java rename to group14/296933284/coding/src/com/coding/basic/List.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java b/group14/296933284/coding/src/com/coding/basic/Queue.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java rename to group14/296933284/coding/src/com/coding/basic/Queue.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java b/group14/296933284/coding/src/com/coding/basic/Stack.java similarity index 100% rename from group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java rename to group14/296933284/coding/src/com/coding/basic/Stack.java From b01be9ca78d33042c240d2cf120d98de6aa3028e Mon Sep 17 00:00:00 2001 From: Tennyson Date: Wed, 1 Mar 2017 22:36:10 +0800 Subject: [PATCH 2/8] ArrayUtilTest --- .../com/coderising/array/ArrayUtilTest.java | 84 +++++++++---------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java index 5605b5064f..1e7d8c1991 100644 --- a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java +++ b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java @@ -1,5 +1,7 @@ package com.coderising.array; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; @@ -8,96 +10,88 @@ * Created by damocles on 2017/3/1. */ public class ArrayUtilTest { + private ArrayUtil arrayUtil; + private int[] oldArray = null; + private int[] newArray = null; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + if (newArray != null) + for (int i = 0; i < newArray.length; i++) + System.out.print(newArray[i] + "\t"); + + System.out.println(); + } + @Test public void reverseArray() throws Exception { - int[] a = {7, 9 , 30, 3}; - ArrayUtil arrayUtil = new ArrayUtil(); + oldArray = new int[]{7, 9, 30, 3}; - arrayUtil.reverseArray(a); + arrayUtil.reverseArray(oldArray); - for (int i = 0; i < a.length; i++) - System.out.print(a[i] + "\t"); + for (int i = 0; i < oldArray.length; i++) + System.out.print(oldArray[i] + "\t"); - int[] b = {3, 30, 9, 7, 4}; + oldArray = new int[]{3, 30, 9, 7, 4}; System.out.println(); - arrayUtil.reverseArray(b); + arrayUtil.reverseArray(oldArray); - for (int i = 0; i < b.length; i++) - System.out.print(b[i] + "\t"); + for (int i = 0; i < oldArray.length; i++) + System.out.print(oldArray[i] + "\t"); } @Test public void removeZero() throws Exception { - int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + oldArray = new int[]{1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; - ArrayUtil arrayUtil = new ArrayUtil(); - int[] newArr = arrayUtil.removeZero(oldArr); - - for (int i = 0; i < newArr.length; i++) - System.out.print(newArr[i] + "\t"); + newArray = arrayUtil.removeZero(oldArray); } @Test public void merge() throws Exception { - int[] a1 = {3, 5, 7,8}, a2 = {4, 5, 6,7}; - ArrayUtil arrayUtil = new ArrayUtil(); - - int[] a3 = arrayUtil.merge(a1, a2); + int[] a1 = {3, 5, 7, 8}, a2 = {4, 5, 6, 7}; - for (int i = 0; i < a3.length; i++) - System.out.print(a3[i] + "\t"); + newArray = arrayUtil.merge(a1, a2); } @Test public void grow() throws Exception { - int[] oldArray = {2, 3, 6}; + oldArray = new int[]{2, 3, 6}; int size = 3; - ArrayUtil arrayUtil = new ArrayUtil(); - int[] newArray = arrayUtil.grow(oldArray, size); + newArray = arrayUtil.grow(oldArray, size); - for (int i = 0; i < newArray.length; i++) - System.out.print(newArray[i] + "\t"); } @Test public void fibonacci() throws Exception { - ArrayUtil arrayUtil = new ArrayUtil(); - int[] newArray = arrayUtil.fibonacci(15); - - for (int i = 0; i < newArray.length; i++) - System.out.print(newArray[i] + "\t"); // 1 1 2 3 5 8 13 + newArray = arrayUtil.fibonacci(15); } @Test public void getPrimes() throws Exception { - ArrayUtil arrayUtil = new ArrayUtil(); - int[] newArray = arrayUtil.getPrimes(23); - - for (int i = 0; i < newArray.length; i++) - System.out.print(newArray[i] + "\t"); // [2,3,5,7,11,13,17,19] + newArray = arrayUtil.getPrimes(23); } @Test public void getPerfectNumbers() throws Exception { - ArrayUtil arrayUtil = new ArrayUtil(); - int[] newArray = arrayUtil.getPerfectNumbers(500); - - for (int i = 0; i < newArray.length; i++) - System.out.print(newArray[i] + "\t"); // [6 ,28 ,496] - + newArray = arrayUtil.getPerfectNumbers(500); } @Test public void join() throws Exception { - int[] array= {3, 8, 9, 10, 2}; + oldArray = new int[]{3, 8, 9, 10, 2}; String seperator = "-"; - ArrayUtil arrayUtil = new ArrayUtil(); - String str = arrayUtil.join(array, seperator); + String str = arrayUtil.join(oldArray, seperator); System.out.println(str); } From 569f0de9b9783427cff349adbbd54eae844354ce Mon Sep 17 00:00:00 2001 From: Tennyson Date: Sat, 4 Mar 2017 12:04:37 +0800 Subject: [PATCH 3/8] struts --- group14/296933284/coding/.classpath | 1 + .../coderising/litestruts/LoginAction.java | 21 ++-- .../src/com/coderising/litestruts/Struts.java | 114 +++++++++++++++++- .../com/coderising/litestruts/StrutsTest.java | 2 +- .../src/com/coderising/litestruts/struts.xml | 8 +- .../src/com/coding/basic/LinkedList.java | 2 + 6 files changed, 129 insertions(+), 19 deletions(-) diff --git a/group14/296933284/coding/.classpath b/group14/296933284/coding/.classpath index 83169ef5db..bb08ce9070 100644 --- a/group14/296933284/coding/.classpath +++ b/group14/296933284/coding/.classpath @@ -4,5 +4,6 @@ + diff --git a/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java b/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java index dcdbe226ed..b76d6548b0 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/LoginAction.java @@ -18,22 +18,27 @@ 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 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(){ + + public String getMessage() { return this.message; } } diff --git a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java index 85e2e22de3..a6794dd0fc 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java @@ -1,7 +1,19 @@ package com.coderising.litestruts; -import java.util.Map; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; public class Struts { @@ -10,8 +22,8 @@ public static View runAction(String actionName, Map parameters) { /* - 0. 读取配置文件struts.xml - + 0. 读取配置文件struts.xml + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") , @@ -27,8 +39,98 @@ public static View runAction(String actionName, Map parameters) { 放到View对象的jsp字段中。 */ - - return null; - } + + SAXReader reader = new SAXReader(); + Object obj = null; + View view = new View(); + + try { + // 加载配置文件 + File file = new File("src/com/coderising/litestruts/struts.xml"); + Document doucment = reader.read(file); + Element struts = doucment.getRootElement(); + Iterator strutsIterator = struts.elementIterator(); + Element action = null; + + while (strutsIterator.hasNext()) { + action = (Element) strutsIterator.next(); + Attribute actionAttribute = action.attribute("name"); + + // 通过反射获取实例化对象 + if (actionAttribute.getValue().equals(actionName)) { + obj = Class.forName(action.attribute("class").getValue()).newInstance(); + break; + } + } + + Iterator> iterator = parameters.entrySet().iterator(); + + while (iterator.hasNext()) { + + Map.Entry entry = iterator.next(); + + // 调用相应属性的setter方法 + Method method = obj.getClass().getMethod("set" + toUpperFisrtLetter(entry.getKey()), String.class); + method.invoke(obj, entry.getKey()); + } + + Method exectueMethod= obj.getClass().getMethod("execute"); + String exectueValue = (String) exectueMethod.invoke(obj); + + // 获取类中的所有属性 + Field[] fields = obj.getClass().getFields(); + HashMap hashMap = new HashMap<>(); + + for(int i = 0; i < fields.length; i++){ + + String fieldName = fields[i].getName(); + + // 获取对应的getter方法 + Method getterMethod = obj.getClass().getMethod("get" + toUpperFisrtLetter(fieldName)); + String fieldValue = (String) getterMethod.invoke(obj); + + hashMap.put(fieldName, fieldValue); + } + + view.setParameters(hashMap); + + Iterator actionIterator = action.elementIterator(); + + while (actionIterator.hasNext()) { + Element result = (Element) actionIterator.next(); + Attribute resuAttribute = result.attribute("name"); + + if (resuAttribute.getValue().equals(exectueValue)) { + view.setJsp(result.getStringValue()); + break; + } + } + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + // 将字符串首字母大写 + private static String toUpperFisrtLetter(String str) { + String string = str.toLowerCase(); + char[] cs = string.toCharArray(); + // 首字母大写 + cs[0] -= 32; + + return String.valueOf(cs); + } } diff --git a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java index 1abc2889a3..52cffffb73 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java @@ -31,7 +31,7 @@ public void testLoginActionFailed() { String actionName = "login"; Map params = new HashMap(); params.put("name","test"); - params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷� + params.put("password","123456"); //密码和预设的不一致 View view = Struts.runAction(actionName,params); diff --git a/group14/296933284/coding/src/com/coderising/litestruts/struts.xml b/group14/296933284/coding/src/com/coderising/litestruts/struts.xml index a6cfe43e6c..ea46090bc9 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/struts.xml +++ b/group14/296933284/coding/src/com/coderising/litestruts/struts.xml @@ -1,11 +1,11 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - - /jsp/welcome.jsp - /jsp/error.jsp + + /jsp/welcome.jsp + /jsp/error.jsp \ No newline at end of file diff --git a/group14/296933284/coding/src/com/coding/basic/LinkedList.java b/group14/296933284/coding/src/com/coding/basic/LinkedList.java index cb5f1e99f8..3c37904532 100644 --- a/group14/296933284/coding/src/com/coding/basic/LinkedList.java +++ b/group14/296933284/coding/src/com/coding/basic/LinkedList.java @@ -104,6 +104,7 @@ public Object get(int index) { * ɾָλýڵ㣬ؽڵֵ */ public Object remove(int index) { + rangCheck(index); Node r = getPreNode(index); @@ -135,6 +136,7 @@ public Object removeLast() { // ȡָλõǰ㲢 private Node getPreNode(int index) { + rangCheck(index); if (index == 0) { From b0751b6741f509e896bc113688ddd64b131ec06d Mon Sep 17 00:00:00 2001 From: Tennyson Date: Sat, 4 Mar 2017 12:51:37 +0800 Subject: [PATCH 4/8] Struts Test Success --- .../src/com/coderising/litestruts/Struts.java | 13 ++++++++----- .../src/com/coderising/litestruts/StrutsTest.java | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java index a6794dd0fc..f69ccc9ae2 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java @@ -41,7 +41,6 @@ public static View runAction(String actionName, Map parameters) { */ SAXReader reader = new SAXReader(); - Object obj = null; View view = new View(); try { @@ -51,6 +50,7 @@ public static View runAction(String actionName, Map parameters) { Element struts = doucment.getRootElement(); Iterator strutsIterator = struts.elementIterator(); Element action = null; + Object obj = null; while (strutsIterator.hasNext()) { action = (Element) strutsIterator.next(); @@ -70,15 +70,16 @@ public static View runAction(String actionName, Map parameters) { Map.Entry entry = iterator.next(); // 调用相应属性的setter方法 - Method method = obj.getClass().getMethod("set" + toUpperFisrtLetter(entry.getKey()), String.class); - method.invoke(obj, entry.getKey()); + Method setterMethod = obj.getClass().getMethod("set" + + toUpperFisrtLetter(entry.getKey()), String.class); + setterMethod.invoke(obj, entry.getValue()); } Method exectueMethod= obj.getClass().getMethod("execute"); String exectueValue = (String) exectueMethod.invoke(obj); // 获取类中的所有属性 - Field[] fields = obj.getClass().getFields(); + Field[] fields = obj.getClass().getDeclaredFields(); HashMap hashMap = new HashMap<>(); for(int i = 0; i < fields.length; i++){ @@ -86,12 +87,14 @@ public static View runAction(String actionName, Map parameters) { String fieldName = fields[i].getName(); // 获取对应的getter方法 - Method getterMethod = obj.getClass().getMethod("get" + toUpperFisrtLetter(fieldName)); + Method getterMethod = obj.getClass().getMethod("get" + + toUpperFisrtLetter(fieldName)); String fieldValue = (String) getterMethod.invoke(obj); hashMap.put(fieldName, fieldValue); } + view.setParameters(hashMap); Iterator actionIterator = action.elementIterator(); diff --git a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java index 52cffffb73..5e280c4e09 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java @@ -21,7 +21,8 @@ public void testLoginActionSuccess() { View view = Struts.runAction(actionName,params); - + System.out.println(view.getJsp()); + System.out.println(view.getParameters().get("message")); Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); Assert.assertEquals("login successful", view.getParameters().get("message")); } @@ -34,7 +35,8 @@ public void testLoginActionFailed() { params.put("password","123456"); //密码和预设的不一致 View view = Struts.runAction(actionName,params); - + System.out.println(view.getJsp()); + System.out.println(view.getParameters().get("message")); Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); } From ef590c86d1ecd4b800acd652ae47e5911b41b42c Mon Sep 17 00:00:00 2001 From: Tennyson Date: Sat, 4 Mar 2017 13:05:50 +0800 Subject: [PATCH 5/8] ArrayUtil Test --- .../com/coderising/array/ArrayUtilTest.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java index 1e7d8c1991..79c4228243 100644 --- a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java +++ b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java @@ -1,6 +1,7 @@ package com.coderising.array; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -21,79 +22,79 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { - if (newArray != null) - for (int i = 0; i < newArray.length; i++) - System.out.print(newArray[i] + "\t"); - - System.out.println(); } @Test public void reverseArray() throws Exception { oldArray = new int[]{7, 9, 30, 3}; - + newArray = new int[]{3, 30, 9, 7}; arrayUtil.reverseArray(oldArray); - for (int i = 0; i < oldArray.length; i++) - System.out.print(oldArray[i] + "\t"); + Assert.assertArrayEquals(newArray, oldArray); oldArray = new int[]{3, 30, 9, 7, 4}; - - System.out.println(); - + newArray = new int[]{4, 7, 9, 30, 3}; arrayUtil.reverseArray(oldArray); - for (int i = 0; i < oldArray.length; i++) - System.out.print(oldArray[i] + "\t"); + Assert.assertArrayEquals(newArray, oldArray); } @Test public void removeZero() throws Exception { oldArray = new int[]{1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + newArray = new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + + Assert.assertArrayEquals(newArray, arrayUtil.removeZero(oldArray)); - newArray = arrayUtil.removeZero(oldArray); } @Test public void merge() throws Exception { - int[] a1 = {3, 5, 7, 8}, a2 = {4, 5, 6, 7}; + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + newArray = new int[]{3, 4, 5, 6, 7, 8}; - newArray = arrayUtil.merge(a1, a2); + Assert.assertArrayEquals(newArray, arrayUtil.merge(a1, a2)); } @Test public void grow() throws Exception { oldArray = new int[]{2, 3, 6}; + newArray = new int[]{2, 3, 6, 0, 0, 0}; int size = 3; - newArray = arrayUtil.grow(oldArray, size); - + Assert.assertArrayEquals(newArray, arrayUtil.grow(oldArray, size)); } @Test public void fibonacci() throws Exception { - newArray = arrayUtil.fibonacci(15); + newArray = new int[]{1, 1, 2, 3, 5, 8, 13}; + + Assert.assertArrayEquals(newArray, arrayUtil.fibonacci(15)); } @Test public void getPrimes() throws Exception { - newArray = arrayUtil.getPrimes(23); + newArray = new int[]{2, 3, 5, 7, 11, 13, 17,19}; + + Assert.assertArrayEquals(newArray, arrayUtil.getPrimes(23)); } @Test public void getPerfectNumbers() throws Exception { - newArray = arrayUtil.getPerfectNumbers(500); + newArray = new int[]{6, 28, 496}; + + Assert.assertArrayEquals(newArray, arrayUtil.getPerfectNumbers(500)); } @Test public void join() throws Exception { oldArray = new int[]{3, 8, 9, 10, 2}; String seperator = "-"; + String exception = "3-8-9-10-2"; - String str = arrayUtil.join(oldArray, seperator); - - System.out.println(str); + Assert.assertEquals(exception, arrayUtil.join(oldArray, seperator)); } } \ No newline at end of file From 9d48aa4d21be3bc81f7ac21cb02813e39b4e85c0 Mon Sep 17 00:00:00 2001 From: Tennyson Date: Sat, 4 Mar 2017 21:48:43 +0800 Subject: [PATCH 6/8] note --- group14/296933284/Note/README.md | 3 ++- group14/296933284/Note/test.txt | 1 - group14/296933284/README.md | 2 +- group14/296933284/coding/.classpath | 1 + .../src/com/coderising/litestruts/Struts.java | 18 ++++++++++-------- .../com/coderising/litestruts/StrutsTest.java | 6 ++---- 6 files changed, 16 insertions(+), 15 deletions(-) delete mode 100644 group14/296933284/Note/test.txt diff --git a/group14/296933284/Note/README.md b/group14/296933284/Note/README.md index 3b92cb5667..0b08b8f0bb 100644 --- a/group14/296933284/Note/README.md +++ b/group14/296933284/Note/README.md @@ -1,4 +1,5 @@ # 2017编程提高(Java)学习系列笔记链接 --- -1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) \ No newline at end of file +1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) +2. [Java中解析XML的方法 2017-03-04](http://tennyson.ren/2017/03/04/Java%E4%B8%AD%E8%A7%A3%E6%9E%90XML%E7%9A%84%E6%96%B9%E6%B3%95/#more) \ No newline at end of file diff --git a/group14/296933284/Note/test.txt b/group14/296933284/Note/test.txt deleted file mode 100644 index 95d09f2b10..0000000000 --- a/group14/296933284/Note/test.txt +++ /dev/null @@ -1 +0,0 @@ -hello world \ No newline at end of file diff --git a/group14/296933284/README.md b/group14/296933284/README.md index 023abf1e46..fdbaba80a9 100644 --- a/group14/296933284/README.md +++ b/group14/296933284/README.md @@ -1,4 +1,4 @@ # 2017年编程提高(Java) 作业、练习、总结 -[DataStructuresTest --- 基本数据结构Java实现](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest) +[coding --- 代码练习](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest) [Note --- 2017编程提高(Java)学习系列笔记链接](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/Note) diff --git a/group14/296933284/coding/.classpath b/group14/296933284/coding/.classpath index bb08ce9070..11ea08c22c 100644 --- a/group14/296933284/coding/.classpath +++ b/group14/296933284/coding/.classpath @@ -5,5 +5,6 @@ + diff --git a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java index f69ccc9ae2..0c83cf4507 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/Struts.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/Struts.java @@ -5,6 +5,7 @@ import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.lang.reflect.Field; @@ -13,7 +14,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Objects; + public class Struts { @@ -41,11 +42,11 @@ public static View runAction(String actionName, Map parameters) { */ SAXReader reader = new SAXReader(); + File file = new File("src/com/coderising/litestruts/struts.xml"); View view = new View(); try { // 加载配置文件 - File file = new File("src/com/coderising/litestruts/struts.xml"); Document doucment = reader.read(file); Element struts = doucment.getRootElement(); Iterator strutsIterator = struts.elementIterator(); @@ -63,11 +64,11 @@ public static View runAction(String actionName, Map parameters) { } } - Iterator> iterator = parameters.entrySet().iterator(); + Iterator> parametersIterator = parameters.entrySet().iterator(); - while (iterator.hasNext()) { + while (parametersIterator.hasNext()) { - Map.Entry entry = iterator.next(); + Map.Entry entry = parametersIterator.next(); // 调用相应属性的setter方法 Method setterMethod = obj.getClass().getMethod("set" + @@ -80,7 +81,7 @@ public static View runAction(String actionName, Map parameters) { // 获取类中的所有属性 Field[] fields = obj.getClass().getDeclaredFields(); - HashMap hashMap = new HashMap<>(); + HashMap fieldHashMap = new HashMap<>(); for(int i = 0; i < fields.length; i++){ @@ -91,11 +92,11 @@ public static View runAction(String actionName, Map parameters) { toUpperFisrtLetter(fieldName)); String fieldValue = (String) getterMethod.invoke(obj); - hashMap.put(fieldName, fieldValue); + fieldHashMap.put(fieldName, fieldValue); } - view.setParameters(hashMap); + view.setParameters(fieldHashMap); Iterator actionIterator = action.elementIterator(); @@ -127,6 +128,7 @@ public static View runAction(String actionName, Map parameters) { } // 将字符串首字母大写 + @NotNull private static String toUpperFisrtLetter(String str) { String string = str.toLowerCase(); char[] cs = string.toCharArray(); diff --git a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java index 5e280c4e09..ab335a0ea4 100644 --- a/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java +++ b/group14/296933284/coding/src/com/coderising/litestruts/StrutsTest.java @@ -21,8 +21,7 @@ public void testLoginActionSuccess() { View view = Struts.runAction(actionName,params); - System.out.println(view.getJsp()); - System.out.println(view.getParameters().get("message")); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); Assert.assertEquals("login successful", view.getParameters().get("message")); } @@ -35,8 +34,7 @@ public void testLoginActionFailed() { params.put("password","123456"); //密码和预设的不一致 View view = Struts.runAction(actionName,params); - System.out.println(view.getJsp()); - System.out.println(view.getParameters().get("message")); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); } From f16f3d91d33c94159578dc66387f75391381cb04 Mon Sep 17 00:00:00 2001 From: Tennyson Date: Sat, 4 Mar 2017 21:57:35 +0800 Subject: [PATCH 7/8] ... --- group14/296933284/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group14/296933284/README.md b/group14/296933284/README.md index fdbaba80a9..5c3f27c267 100644 --- a/group14/296933284/README.md +++ b/group14/296933284/README.md @@ -1,4 +1,4 @@ # 2017年编程提高(Java) 作业、练习、总结 -[coding --- 代码练习](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest) +[coding --- 代码练习](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/coding) [Note --- 2017编程提高(Java)学习系列笔记链接](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/Note) From dcb5584ed68448ee32a7721bcb8fa8c184c1fc22 Mon Sep 17 00:00:00 2001 From: Tennyson Date: Sun, 5 Mar 2017 12:28:58 +0800 Subject: [PATCH 8/8] ... --- .../src/com/coderising/array/ArrayUtil.java | 39 ++++++++++++++++--- .../com/coderising/array/ArrayUtilTest.java | 9 ++--- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java b/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java index 7ff23cc368..557f917c37 100644 --- a/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java +++ b/group14/296933284/coding/src/com/coderising/array/ArrayUtil.java @@ -150,15 +150,44 @@ public int[] getPerfectNumbers(int max) { ArrayList arrayList = new ArrayList(); - for (int i = 6; i < max; i++) { + for (int i = 6; i < max; i += 2) { + + if (i == 6) { + arrayList.add(i); + continue; + } + int factorSum = 0; - for (int j = 1; j < i; j++) - if (i % j == 0) - factorSum += j; + if (i % 3 == 1 && i % 9 == 1) { + + boolean flag = false; + + if (i % 10 == 6 ) { + flag = true; + } + + if (i % 10 == 8) { + if (i % 100 == 28) { + flag = true; + } + } - if (factorSum == i) + if (flag) { + + for (int j = 1; j <= (i / 2); j++) { + + if (i % j == 0) { + factorSum += j; + } + } + } + + } + + if (factorSum == i) { arrayList.add(i); + } } diff --git a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java index 79c4228243..1b1e7005b4 100644 --- a/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java +++ b/group14/296933284/coding/src/com/coderising/array/ArrayUtilTest.java @@ -5,7 +5,6 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; /** * Created by damocles on 2017/3/1. @@ -76,16 +75,16 @@ public void fibonacci() throws Exception { @Test public void getPrimes() throws Exception { - newArray = new int[]{2, 3, 5, 7, 11, 13, 17,19}; + newArray = new int[]{2, 3, 5, 7, 11, 13, 17, 19}; Assert.assertArrayEquals(newArray, arrayUtil.getPrimes(23)); } @Test public void getPerfectNumbers() throws Exception { - newArray = new int[]{6, 28, 496}; - - Assert.assertArrayEquals(newArray, arrayUtil.getPerfectNumbers(500)); + newArray = new int[]{6, 28, 496, 8128}; + System.out.println(33550336 % 27); + Assert.assertArrayEquals(newArray, arrayUtil.getPerfectNumbers(10000)); } @Test