From 836dc344fb65f02c6122153218e59c5cfa477288 Mon Sep 17 00:00:00 2001
From: FelixCJF <jianfengchen999@gmail.com>
Date: Wed, 1 Mar 2017 20:06:40 +0800
Subject: [PATCH] =?UTF-8?q?ArrayUtil=E6=8F=90=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../coderising/array/ArrayUtil.java           | 228 ++++++++++++++++++
 .../coderising/array/test/ArrayUtilTest.java  | 148 ++++++++++++
 .../coderising/litestruts/LoginAction.java    |  39 +++
 .../coderising/litestruts/Struts.java         |  34 +++
 .../coderising/litestruts/StrutsTest.java     |  43 ++++
 .../coderising/litestruts/View.java           |  23 ++
 .../coderising/litestruts/struts.xml          |  11 +
 7 files changed, 526 insertions(+)
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java
 create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml

diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java
new file mode 100644
index 000000000..f60be0597
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java
@@ -0,0 +1,228 @@
+package com.github.FelixCJF.coding2017.coderising.array;
+
+import java.util.ArrayList;
+
+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 array[] = new int[origin.length];
+		for (int i = 0; i<origin.length; i++) {
+			array[i] = origin[origin.length - 1 - i];
+		}
+		System.arraycopy(array, 0, origin, 0, origin.length);
+	}
+	
+	/**
+	 * 现在有如下的一个数组：   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 zero = 0;
+		//统计原来数组0的个数
+		for (int i = 0; i < oldArray.length; i++) {
+			if (oldArray[i] == 0) {
+				zero ++;
+			}
+		}
+		int[] newArr = new int[oldArray.length - zero];
+		int count = 0;
+		for (int i = 0; i<oldArray.length; i ++) {
+			if (oldArray[i] != 0) {
+				newArr[count] = oldArray[i];
+				count ++;
+			}
+		}
+		return newArr;
+	}
+	
+	/**
+	 * 给定两个已经排序好的整形数组， 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 length1 = array1.length;
+		int length2 = array2.length;
+		int length = length1 + length2;
+		int[] newArr = new int[length];
+		//插入
+		for (int i = 0; i < length1; i++) {
+			newArr[i] = array1[i];
+		}
+		for (int i = 0; i < length2; i++) {
+			newArr[length1 - 1 + i] = array2[i];
+		}
+		//排序
+		for (int i = 0; i < length-1; i ++){
+			if (newArr[i] > newArr[i+1]) {
+				int temp = newArr[i];
+				newArr[i] = newArr[i+1];
+				newArr[i + 1] = temp;
+			}
+		}
+		return  newArr;
+	}
+	/**
+	 * 把一个已经存满数据的数组 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 newArr[] = new int[oldArray.length + size];
+		System.arraycopy(oldArray, 0, newArr, 0, oldArray.length);
+		return newArr;
+	}
+	
+	/**
+	 * 斐波那契数列为：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[] newArr;
+		int f1 = 0;
+		int f2 = 1;
+		int f = 0;
+		if (max < 2) {
+			return newArr = new int[0];
+		}
+		ArrayList list = new ArrayList();
+		for (int i = 2; f < max; i++) {
+			list.add(f2);
+			f = f1 + f2;
+			f1 = f2;
+			f2 = f;
+		}
+		newArr = new int[list.size()];
+		for (int i = 0; i < newArr.length; i++) {
+			newArr[i] = (int) list.get(i);
+		}
+		return newArr;
+	}
+	
+	/**
+	 * 返回小于给定最大值max的所有素数数组
+	 * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
+	 * @param max
+	 * @return
+	 */
+	public int[] getPrimes(int max){
+	
+		ArrayList list = new ArrayList();
+
+		for (int i = 1; i < max; i++) {
+			if (isPrime(i)) {
+				list.add(i);
+			}
+		}
+		int[] newArr = new int[list.size()];
+		for (int i = 0; i < newArr.length; i++) {
+			newArr[i] = (int) list.get(i);
+		}
+		return newArr;
+	}
+	//判断是否为素数
+	private boolean isPrime(int a) {  
+		  
+        boolean flag = true;  
+  
+        if (a < 2) {// 素数不小于2  
+            return false;  
+        } else {  
+  
+            for (int i = 2; i <= Math.sqrt(a); i++) {  
+  
+                if (a % i == 0) {// 若能被整除，则说明不是素数，返回false  
+  
+                    flag = false;  
+                    break;// 跳出循环  
+                }  
+            }  
+        }  
+        return flag;  
+    }  
+	
+	/**
+	 * 所谓“完数”， 是指这个数恰好等于它的因子之和，例如6=1+2+3
+	 * 给定一个最大值max， 返回一个数组， 数组中是小于max 的所有完数
+	 * @param max
+	 * @return
+	 */
+	public int[] getPerfectNumbers(int max){
+		int[] newArr;
+		if (max == 0) {
+			return newArr = new int[0];
+		}
+		ArrayList list = new ArrayList();
+		for (int i = 1; i < max; i++) {
+			if (isWanshu(i)) {
+				list.add(i);
+			}
+		}
+		newArr = new int[list.size()];
+		for (int i = 0; i < newArr.length; i++) {
+			newArr[i] = (int) list.get(i);
+		}
+		return newArr;
+	}
+	//判断一个数是不是完数
+	private boolean isWanshu(int n)  
+	{  
+	    boolean flag=false;  
+	    int i,sum=0;  
+	    for(i=1;i<=n/2;i++)  
+	    {  
+	        if(n%i==0)  
+	        {  
+	            sum+=i;  
+	        }  
+	    }  
+	    if(sum==n)  
+	    {  
+	        flag=true;  
+	    }  
+	    return flag;  
+	}
+	/**
+	 * 用seperator 把数组 array给连接起来
+	 * 例如array= [3,8,9], seperator = "-"
+	 * 则返回值为"3-8-9"
+	 * @param array
+	 * @param s
+	 * @return
+	 */
+	public String join(int[] array, String seperator){
+		String string = "";
+		for (int i = 0; i < array.length; i++) {
+			if (i == array.length - 1) {
+				string += array[i];
+			} else {
+				string += array[i] + seperator;
+			}
+		}
+		return string;
+	}
+	
+
+}
diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java
new file mode 100644
index 000000000..378e05216
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java
@@ -0,0 +1,148 @@
+package com.github.FelixCJF.coding2017.coderising.array.test;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.github.FelixCJF.coding2017.coderising.array.ArrayUtil;
+
+
+public class ArrayUtilTest
+{
+	private ArrayUtil myArray;
+	@Before
+	public void setUp() throws Exception
+	{
+		myArray = new ArrayUtil();
+	}
+
+	@Test
+	public void testReverseArray()
+	{
+		int[] a = {1, 2, 1, 3, 5, 6};
+		int[] b = {6, 5, 3, 1, 2, 1};
+		int[] c = new int[0];
+
+		myArray.reverseArray(a);
+		assertArrayEquals(a, b);
+		
+		//对空数组进行反转
+		myArray.reverseArray(c);
+		assertArrayEquals(c, new int[0]);
+	}
+
+	@Test
+	public void testRemoveZero()
+	{
+		int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5};
+		int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5};  
+		int[] c = myArray.removeZero(oldArr);
+		assertArrayEquals(b, c);
+		
+		int[] d = new int[0];
+		assertArrayEquals(d, new int[0]);
+	}
+
+	@Test
+	public void testMerge()
+	{
+		int a1[] = {1, 2, 3, 4, 5};
+		int b1[] = {3, 4, 5, 6, 7, 8};
+		int c1[] = {1, 2, 3, 4, 5, 6, 7, 8};
+		int[] newArray1 = myArray.merge(a1, b1);
+		assertArrayEquals(c1, newArray1);
+		
+		int a2[] = new int[0];
+		int b2[] = {0, 2, 3, 6, 7, 8};
+		int c2[] = {0, 2, 3, 6, 7, 8};
+		int[] newArray2 = myArray.merge(a2, b2);
+		assertArrayEquals(c2, newArray2);
+		
+		int a3[] = {0, 2, 3, 6, 7, 8};
+		int b3[] = new int[0];
+		int c3[] = {0, 2, 3, 6, 7, 8};
+		int[] newArray3 = myArray.merge(a3, b3);
+		assertArrayEquals(c3, newArray3);
+		
+		int[] a4 = new int[0];
+		int[] b4 = new int[0];
+		int[] newArray4 = myArray.merge(a4, b4);
+		assertArrayEquals(new int[0], newArray4);
+	}
+
+	@Rule  
+	public ExpectedException expectedEx = ExpectedException.none(); 
+	@Test
+	public void testGrow()
+	{
+		int[] a = {3, 5, 7, 8, 9};
+		int[] b = {3, 5, 7, 8, 9, 0, 0, 0};
+		int[] newArray = myArray.grow(a, 3);
+		assertArrayEquals(b, newArray);
+		
+		// size < 0 抛出异常
+		expectedEx.expect(Exception.class);
+		int[] newArray1 = myArray.grow(a, -3);
+		assertArrayEquals(b, newArray1);
+
+	}
+	
+	@Test
+	public void testFibonacci()
+	{
+		//max == 1时返回空数组
+		int[] array1 =  myArray.fibonacci(1);
+		int[] b = new int[0];
+		assertArrayEquals(array1, b);
+		
+		
+		int[] array2=  myArray.fibonacci(35);
+		int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 };
+		assertArrayEquals(c, array2);
+	}
+
+	@Test
+	public void testGetPrimes()
+	{
+		int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
+		int[] array1 =  myArray.getPrimes(35);
+		assertArrayEquals(a, array1);
+		
+		//max <= 2的时候没有素数，数组为空数组 new int[0]
+		int[] array2 = myArray.getPrimes(1); 
+		int[] b = new int[0];
+		assertArrayEquals(array2, b);
+	}
+
+	@Test
+	public void testGetPerfectNumbers()
+	{
+		int[] array =  myArray.getPerfectNumbers(10000);
+		int[] a = {6, 28, 496, 8128 };
+		assertArrayEquals(a, array);
+	}
+
+	@Test
+	public void testJoin()
+	{
+		int[] Array0 = {3, 5, 7, 8, 9};
+		String s0 = myArray.join(Array0, "-");
+		String s1 = "3-5-7-8-9";
+		assertEquals(s1, s0);
+		
+		int[] Array1 = {3};
+		String s2 = myArray.join(Array1, "-");
+		String s3 = "3";
+		assertEquals(s2, s3);
+		
+		//传递空数组时，返回空数组
+		int[] Array2 = new int[0];
+		String s4 = myArray.join(Array2, "-");
+		String s5 = "";
+		assertEquals(s4, s5);
+	}
+
+}
diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java
new file mode 100644
index 000000000..dcdbe226e
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java
new file mode 100644
index 000000000..85e2e22de
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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<String,String> 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中的 <result> 配置,以及execute的返回值，  确定哪一个jsp，  
+		放到View对象的jsp字段中。
+        
+        */
+    	
+    	return null;
+    }    
+
+}
diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java
new file mode 100644
index 000000000..b8c81faf3
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java
@@ -0,0 +1,43 @@
+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<String,String> params = new HashMap<String,String>();
+        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<String,String> params = new HashMap<String,String>();
+        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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java
new file mode 100644
index 000000000..07df2a5da
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml
new file mode 100644
index 000000000..a6cfe43e6
--- /dev/null
+++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<struts>
+    <action name="login" class="com.coderising.action.LoginAction">
+        <result name="success">/jsp/homepage.jsp</result>
+        <result name="fail">/jsp/showLogin.jsp</result>
+    </action>
+    <action name="logout" class="com.coderising.action.LogoutAction">
+    	<result = "success">/jsp/welcome.jsp</result>
+    	<result = "error">/jsp/error.jsp</result>
+    </action>
+</struts>
\ No newline at end of file
