diff --git a/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java new file mode 100644 index 0000000000..85276ce4da --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java @@ -0,0 +1,232 @@ +package com.bruce.homework0305.array; + +import com.bruce.homework0226.LinkedListV00; + +import java.util.*; + +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 int[] reverseArray(int[] origin){ + if(origin != null && origin.length > 0) { + int temp; + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length-1 - i]; + origin[origin.length-1 - i] = temp; + } + } + return origin; + } + + /** + * 现在有如下的一个数组: 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[] result = null; + if(oldArray != null && oldArray.length >= 0){ + int index = 0; + LinkedList linkedList = new LinkedList(); + for(int i = 0;i list = new LinkedList<>(); + getElementFromArray(array1,list); + getElementFromArray(array2,list); + Collections.sort(list); + int[] result = new int[list.size()]; + for(int n = 0;n Integer.MAX_VALUE) { + return result;//抛出异常 + } else if (max == 1) { + result = new int[0]; + } else { + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + for (int i = 2;i max){ + break; + } + list.add(i,(list.get(i-2)+list.get(i-1))); + } + result = new int[list.size()]; + for (int j=0;j Integer.MAX_VALUE) { + return result;//抛出异常 + } else if(max == 1){ + result = new int[0]; + }else { + ArrayList list = new ArrayList<>(); + for (int i=2 ; i<=max ; i++) { + if(isPrimes(i)){ + list.add(i); + } + } + result = new int[list.size()]; + for(int m = 0 ; m Integer.MAX_VALUE){ + return result; + } else { + LinkedList list = new LinkedList<>(); + for(int i = 1 ; i < max ; i++){ + if (isPerfectNumber(i)) { + list.add(i); + } + } + result = new int[list.size()]; + for (int i = 0 ; i < list.size() ; i++) { + result[i] = list.get(i); + } + } + return result; + } + + //判断一个数是否是完数,true:是完数;false:不是完数。 + public boolean isPerfectNumber(int value){ + int sum = 0; + for(int i = 1 ; i < value ; i++){ + if(value % i == 0){ + sum += i; + } + } + return sum == value; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sb = new StringBuffer(); + if (array != null && array.length >= 1) { + for (int i = 0 ; i < array.length ; i++) { + sb.append(array[i]); + if (i < array.length-1) { + sb.append(seperator); + } + } + } + return sb.toString(); + } + + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java new file mode 100644 index 0000000000..eb5487e76d --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java @@ -0,0 +1,84 @@ +package com.bruce.homework0305.array; + +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Bruce.Jiao on 2017/3/2. + */ +public class JuintArrayUtil extends TestCase { + + @Test + public void testReverse(){ + ArrayUtil au = new ArrayUtil(); + int[] demo0 = {}; + int[] demo1 = {6}; + int[] demo = {7, 9, 30, 3, 4, 6}; + int[] nullArray = au.reverseArray(null); + int[] reverseArray0 = au.reverseArray(demo0); + int[] reverseArray1 = au.reverseArray(demo1); + int[] reverseArray = au.reverseArray(demo); + System.out.println(Arrays.toString(nullArray)); + System.out.println(Arrays.toString(reverseArray0)); + System.out.println(Arrays.toString(reverseArray1)); + System.out.println(Arrays.toString(reverseArray)); + } + + @Test + public void testRemoveZero(){ + ArrayUtil au = new ArrayUtil(); + int[] one = {0}; + int[] many = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println(Arrays.toString(au.removeZero(one))); + System.out.println(Arrays.toString(au.removeZero(many))); + System.out.println(Arrays.toString(au.removeZero(null))); + System.out.println(Arrays.toString(au.removeZero(new int[0]))); + } + + @Test + public void testMerge(){ + ArrayUtil au = new ArrayUtil(); + int[] arr1 = {3,4,5,6,7,8,9}; + int[] arr2 = {1,3,5,6,7,9,10,12,13}; + int[] arr3 = null; + int[] arr4 = new int[0]; + System.out.println(Arrays.toString(au.merge(arr1,arr2))); + System.out.println(Arrays.toString(au.merge(arr1,arr3))); + System.out.println(Arrays.toString(au.merge(arr2,arr4))); + } + + @Test + public void testGrow(){ + ArrayUtil au = new ArrayUtil(); + int[] arr = {3,4,5,6,7,8,9}; + System.out.println(Arrays.toString(au.grow(arr,5))); + } + + @Test + public void testFibonacci(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.fibonacci(15))); + } + + @Test + public void testPrimes(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.getPrimes(23))); + } + + @Test + public void testPerfectNumbers(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.getPerfectNumbers(23))); + } + + @Test + public void testJoin(){ + ArrayUtil au = new ArrayUtil(); + int[] array = {1,6,8,8,8,8,8,8,8,8,8,}; + System.out.println(au.join(array,"-")); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java new file mode 100644 index 0000000000..4b509b2102 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.bruce.homework0305.mystruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java new file mode 100644 index 0000000000..ab54fd0625 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java @@ -0,0 +1,81 @@ +package com.bruce.homework0305.mystruts; + +import com.sun.deploy.util.StringUtils; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException, + ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + + /* + 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字段中。 + */ + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("src/com/bruce/homework0305/mystruts/struts.xml")); + Element root = document.getRootElement(); + Iterator actions = root.elementIterator("action"); + HashMap map = new HashMap<>(); + View view = new View(); + while(actions.hasNext()){ + Element next = actions.next(); + List attributes = next.attributes(); + Attribute action_name = next.attribute("name"); + if(actionName.equals(action_name.getValue())){ + //找到name="login"的action,拿到其class路径 + Attribute aClass = next.attribute("class"); + //通过反射拿到LoginAction类 + Class clazz = Class.forName(aClass.getValue()); + LoginAction login = (LoginAction) clazz.newInstance(); + //从parameters中拿到所有的key,通过这些key拿到对应的值,并且传入LoginAction对应的setter方法 + Set keys = parameters.keySet();parameters.entrySet(); + for(String key : keys){ + //首字母大写,拿到setter方法,并将parameters中对应该key的value拿出来,反射传入相应方法 + String setter = "set"+ key.substring(0,1).toUpperCase()+key.substring(1,key.length()); + Method method = clazz.getMethod(setter,String.class); + method.invoke(login,parameters.get(key)); + } + //反射拿到execute方法,并拿到执行结果 + Method execute = clazz.getMethod("execute"); + String result = (String) execute.invoke(login); + //反射拿到getMessage方法,结果以map格式保存,并保存在view对象中 + Method getMessage = clazz.getMethod("getMessage"); + String message = (String)getMessage.invoke(login); + map.put("message",message); + view.setParameters(map); + //根据execute方法的执行结果,找到相关result的jsp路径,将该路径保存在view对象中 + Iterator results = next.elementIterator("result"); + while(results.hasNext()){ + Element element = results.next(); + if(result.equals(element.attribute("name").getValue())){ + view.setJsp(element.getText()); + break; + } + } + } + } + return view; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java new file mode 100644 index 0000000000..e54809e4b6 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java @@ -0,0 +1,61 @@ +package com.bruce.homework0305.mystruts; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + try { + 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")); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test + public void testLoginActionFailed() { + try { + 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")); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java new file mode 100644 index 0000000000..47abd77a37 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java @@ -0,0 +1,23 @@ +package com.bruce.homework0305.mystruts; + +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/group15/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml new file mode 100644 index 0000000000..41c7be3d2f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file