diff --git a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java index e287419dc0..f82d064e2a 100644 --- a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -1,63 +1,63 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - IncrementsCapacity(size + 1); - elementData[size++] = o; - - } - - public void add(int index, Object o) { - checkIndex(index); - IncrementsCapacity(size + 1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkIndex(index); - return elementData[index]; - } - - public Object remove(int index) { - checkIndex(index); - Object o = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[--size] = null; - return o; - } - - public int size() { - return size; - } - - public int length() { - return elementData.length; - } - - // ���� - private void IncrementsCapacity(int num) { - if (num > elementData.length) { - int oldCapacity = elementData.length; // ��ǰ���鳤�� - int newCapacity = ((num + oldCapacity) * 3) >> 2; // ��ǰ���鳤�ȵ�1.5�� - if (newCapacity - num < 0) { - newCapacity = num; // �����������Dz���,ֱ����Ϊ����ֵ - } - Object[] oldelements = elementData; - elementData = new Object[newCapacity]; - System.arraycopy(oldelements, 0, elementData, 0, size); - } - } - - // �±�Խ���ж� - private void checkIndex(int index) { - if (index >= size || index < 0) - throw new IndexOutOfBoundsException("�����±�Խ��"); - } -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // ���� + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // ��ǰ���鳤�� + int newCapacity = ((num + oldCapacity) * 3) >> 2; // ��ǰ���鳤�ȵ�1.5�� + if (newCapacity - num < 0) { + newCapacity = num; // �����������Dz���,ֱ����Ϊ����ֵ + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // �±�Խ���ж� + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("�����±�Խ��"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java index e7cbd474ec..ff93e30377 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Iterator.java +++ b/group16/1287642108/0226/src/com/coding/basic/Iterator.java @@ -1,6 +1,6 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java index 99c92fb9f1..fc206b4cec 100644 --- a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -1,101 +1,101 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size = 0 ; - - public void add(Object o){ - addLast(o); - } - - public void add(int index, Object o) { - if (index == 0) { - addFirst(o); - } else if (index >= size) { - addLast(o); - } else { - Node node = new Node(); - node.data = o; - node.next = getNode(index); - getNode(index - 1).next = node; - size++; - } - } - - public Object get(int index) { - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - Node currentNode = getNode(index); - Node prevNode = getNode(index - 1); - Node lastNode = getNode(index + 1); - prevNode.next = lastNode; - size--; - return currentNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node=new Node(); - node.data = o; - node.next = head; - head = node; - size++; - } - public void addLast(Object o){ - Node node=new Node(); - node.data = o; - node.next = null; - Node lastNode = getNode(size-1); - lastNode.next = node; - size++; - } - public Object removeFirst(){ - Object obj = getNode(0).data; - Node node = getNode(1); - node.next = head; - size--; - return obj; - } - public Object removeLast(){ - Object obj = getNode(size - 1).data; - Node node = getNode(size - 2); - node.next = null; - size--; - return obj; - } - - //��ȡ�ڵ� - public Node getNode(int index){ - checkIndex(index); - if(index == 0 ){ - return head; - }else if(index == size -1 ){ - return tail; - }else{ - Node node = head; - for(int i=0;i= size || index < 0) - throw new IndexOutOfBoundsException("�����±�Խ��"); - } - - private static class Node { - Object data; - Node next; - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //��ȡ�ڵ� + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("�����±�Խ��"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/group16/1287642108/0226/src/com/coding/basic/List.java +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java index 95dee3d81b..418e42826e 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Queue.java +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -1,28 +1,28 @@ -package com.coding.basic; - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - if (isEmpty()) { - return null; - }else{ - return elementData.removeFirst(); - } - } - - public boolean isEmpty(){ - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java index c0b7da89f8..6138bc6973 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Stack.java +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -1,38 +1,38 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - if (isEmpty()) { - elementData.add(elementData.length() - 1, o); - } - elementData.add(elementData.length() - elementData.size() - 1, o); - } - - public Object pop() { - if (isEmpty()) { - return null; - } - return elementData.remove(elementData.length() - elementData.size() - 1); - } - - public Object peek() { - if (isEmpty()) { - return null; - } - return elementData.get(elementData.length() - elementData.size() - 1); - } - - public boolean isEmpty() { - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size() { - return elementData.size(); - } - -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..78845d06d0 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +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 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){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + return null; + } + /** + * 把一个已经存满数据的数组 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){ + return null; + } + + /** + * 斐波那契数列为: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){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java b/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group16/1287642108/0305/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/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java b/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44cc35bf01 --- /dev/null +++ b/group16/1287642108/0305/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/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group16/1287642108/0305/src/com/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 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/group16/1287642108/0305/src/com/coderising/litestruts/View.java b/group16/1287642108/0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/1287642108/0305/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/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group16/1287642108/0305/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