From 6bf56f4658b8a6532fc98f328349e28bc8dd15e4 Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Sun, 26 Feb 2017 16:44:05 +0800 Subject: [PATCH 01/28] Amend local --- .../src/TestCollection/StackTest.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/src/TestCollection/StackTest.java index 3784a9b972..8d2f85e1bf 100644 --- a/group12/382266293/src/TestCollection/StackTest.java +++ b/group12/382266293/src/TestCollection/StackTest.java @@ -104,13 +104,22 @@ public void testSize() { @Test public void testAdd() { - myStack.push(5); - myStack.push(10); - myStack.push(15); - println(myStack.get(0)); - println(myStack.get(1)); - println(myStack.get(2)); + + int size = getRandomNumber(); + int[] expected = new int[size]; + int actual; + for (int i = 0; i < size; i++) { + actual = getRandomNumber(); + expected[i] = actual; + myStack.add(actual); + } + int expectedInt; + for (int i = 0; i < size; i++) { + expectedInt = expected[size - i - 1]; + actual = myStack.pop(); + assertEquals(expectedInt, actual); + } } @Test From 5f23dd85c1bc6049c99ff076ce2cc03ab1bb0411 Mon Sep 17 00:00:00 2001 From: bate999999999 <282742732@qq.com> Date: Sun, 26 Feb 2017 22:48:52 +0800 Subject: [PATCH 02/28] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/com/coding/basic/ArrayList.java | 124 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++ .../src/com/coding/basic/Iterator.java | 7 + .../com/coding/basic/IteratorArrayList.java | 40 +++ .../com/coding/basic/IteratorLinkedList.java | 30 +++ .../src/com/coding/basic/LinkedList.java | 239 ++++++++++++++++++ .../learning_1/src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 22 ++ .../src/com/coding/basic/Stack.java | 42 +++ 10 files changed, 556 insertions(+) create mode 100644 group12/282742732/learning_1/.settings/org.eclipse.jdt.core.prefs create mode 100644 group12/282742732/learning_1/src/com/coding/basic/ArrayList.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/Iterator.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/IteratorArrayList.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/IteratorLinkedList.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/LinkedList.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/List.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/Queue.java create mode 100644 group12/282742732/learning_1/src/com/coding/basic/Stack.java diff --git a/group12/282742732/learning_1/.settings/org.eclipse.jdt.core.prefs b/group12/282742732/learning_1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group12/282742732/learning_1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group12/282742732/learning_1/src/com/coding/basic/ArrayList.java b/group12/282742732/learning_1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..ec3f7399e7 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (size+1>=elementData.length) { + this.groupLength(); + }else if (size==0){ + elementData[0]=o; + }else{ + elementData[size] = o; + } + size = size + 1; + } + public void add(int index, Object o){ + if (size+1>=elementData.length) { + this.groupLength(); + }else if (index == size ) { + elementData[size] = o; + }else if(index>=0 && index<=size ){ + Object[] elementDataNew = new Object[this.elementData.length]; + + System.arraycopy(elementData, 0, elementDataNew, 0, index); + elementDataNew[index]=o; + System.arraycopy(elementData, index, elementDataNew, index+1, size-index); + + this.elementData = elementDataNew; + }else{ + System.out.println("ָ��Խ��"); + return; + } + + size = size + 1; + } + + public Object get(int index){ + + if (index>=0||index<=size) { + return elementData[index]; + }else{ + System.out.println("ָ��Խ��"); + return null; + } + + } + + public Object remove(int index){ + + Object obj =this.elementData[index]; + Object[] elementDataNew = new Object[this.elementData.length]; + if (index<0 || index > size-1) { + System.out.println("ָ��Խ��"); + return null; + }else if(index==0){ + System.arraycopy(elementData, 1, elementDataNew, 0, size-1); + }else if(index==size){ + System.arraycopy(elementData, 0, elementDataNew, 0, size-1); + }else if(index>0 && index <=size){ + System.arraycopy(elementData, 0, elementDataNew, 0, index); + System.arraycopy(elementData, index+1, elementDataNew, index, size-index-1); + } + + this.elementData = elementDataNew; + size=size-1; + return obj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + + return new IteratorArrayList(elementData,size); + } + + private void groupLength(){ + this.elementData = new Object[this.elementData.length + 10]; + size=size+10; + } + + public static void main(String[] args) { + + + + ArrayList list = new ArrayList(); + System.out.println(list.size()); + + list.add("aaa"); +// System.out.println(list.remove(1)); + + list.add("bbb"); + + + list.add(1, "ccc"); + list.add(2, "ddd"); + +// System.out.println(list.size()); +// +// System.out.println( list.remove(0) ); +// +// list.add(0,"111"); +// list.add("xxxx"); + + System.out.println(list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)+","); + } + Iterator it = list.iterator(); + + int idex=0; + while (it.hasNext()) { + String str = (String) it.next(); + idex++; + System.out.println("str"+idex+"=="+str+";"); + + } + } +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/BinaryTreeNode.java b/group12/282742732/learning_1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/Iterator.java b/group12/282742732/learning_1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/IteratorArrayList.java b/group12/282742732/learning_1/src/com/coding/basic/IteratorArrayList.java new file mode 100644 index 0000000000..faefc0de06 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/IteratorArrayList.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +public class IteratorArrayList implements Iterator { + + + private Object[] elementData; + private int length; + + private int index = 0; + + public IteratorArrayList(Object[] elementData,int length){ + this.elementData = elementData; + this.length = length; + } + + @Override + public boolean hasNext() { + + if ((index+1)<=length) { + return true; + } + + return false; + } + + @Override + public Object next() { + if ((index+1)<=length) { + index = index + 1; + return elementData[index-1]; + }else{ + System.out.println("ָ�볬����Χ"); + return null; + } + + } + + + +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/IteratorLinkedList.java b/group12/282742732/learning_1/src/com/coding/basic/IteratorLinkedList.java new file mode 100644 index 0000000000..558b90f05a --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/IteratorLinkedList.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +import com.coding.basic.LinkedList.Node; + +public class IteratorLinkedList implements Iterator{ + + Node node ; + public IteratorLinkedList(Node node){ + this.node = node; + } + + @Override + public boolean hasNext() { + + if (node == null) { + return false; + } + return true; + + } + + @Override + public Object next() { + Object obj = this.node.data; + node = node.next; + return obj; + } + + +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/LinkedList.java b/group12/282742732/learning_1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..c5a28a35f8 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,239 @@ +package com.coding.basic; + + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size = 0; + + public void add(Object o){ + if (head==null) { + head=new Node(o); + last=head; + }else{ + Node node = new Node(o); + last.setNext(node); + last = node; + } + size = size + 1; + } + public void add(int index , Object o){ + + if (index >=0 && index<=this.size && size>0) { + + if (index == 0) { + this.addFirst(o); + }else if(index == size){ + this.addLast(o); + }else{ + int num = 0; + Node node = head; + Node node1 = null; + Node nodenew = new Node(o); + Node node3 = null; + while (num <= index){ + + if(index - 1 == num){ + node1 = node; + }else if(index == num){ + node3 = node; + } + node = node.next; + num = num +1; + }; + nodenew.setNext(node3); + node1.setNext(nodenew); + + + size = size + 1; + } + + }else{ + System.out.println("ָ��Խ��"); + } + + } + public Object get(int index){ + + + if (index >=0 && index<=this.size && size>0) { + int num = 0; + Node node = head; + while (num < size){ + if(index == num){ + return node.getData(); + }else{ + node = node.next; + num = num +1; + } + }; + }else{ + System.out.println("ָ��Խ��"); + return null; + } + return null; + } + public Object remove(int index){ + + if (index >=0 && index<=this.size && size>0) { + + if (index == 0) { + return this.removeFirst(); + }else if(index+1==size){ + return this.removeLast(); + }else{ + int num = 0; + Node node = head; + Node node1 = null; + Node node2 = null; + Node node3 = null; + while (num <= index+1){ + if(index - 1 == num){ + node1 = node; + }else if(index+1 == num){ + node3 = node; + }else if(index == num){ + node2 = node; + } + node = node.next; + num = num +1; + }; + node1.setNext(node3); + size = size - 1; + return node2.data; + } + + }else{ + System.out.println("ָ��Խ��"); + return null; + } + + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o); + node.setNext(head); + head = node; + size = size + 1; + } + public void addLast(Object o){ + Node node = new Node(o); + last.setNext(node); + last = node; + size = size + 1; + } + public Object removeFirst(){ + Object obj = null; + if(size > 0){ + obj = head.data; + head = head.next; + }else{ + System.out.println("ָ��Խ��"); + return null; + } + + size = size - 1; + return obj; + } + public Object removeLast(){ + Object obj = null; + if(this.size() > 0){ + if(this.size()==1){ + obj = head.data; + this.head = null; + }else if (this.size()==2) { + obj = head.next.data; + this.head.setNext(null); + }else{ + int num = 0; + Node node = head; + Node node1 = null; + Node node2 = null; + Node node3 = null; + while (num <= size-2){ + if(size - 2 == num){ + obj = node.next.data; + node.setNext(null); + last = node; + }else{ + node = node.next; + } + + num = num +1; + }; + } + }else{ + System.out.println("ָ��Խ��"); + return null; + } + + + + size = size - 1; + return obj; + } + public Iterator iterator(){ + return new IteratorLinkedList(this.head); + } + + + static class Node{ + Object data; + Node next; + + public Node(Object object) { + data = object; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + } + + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add("aaa"); + list.add("bbb"); + list.add("ccc"); +// System.out.println(list.removeLast()); +// System.out.println(list.removeFirst()); +// list.add(3,"xxxxxx"); +// System.out.println("======="+list.removeFirst()); +// System.out.println(list.size()); +// +// System.out.println("list.get0========"+list.get(0)); +// System.out.println("list.get1========"+list.get(1)); +// System.out.println("============="); + System.out.println(list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + + Iterator it = list.iterator(); + + while(it.hasNext()){ + String str = (String) it.next(); + System.out.println("str====="+str); + } + + } +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/List.java b/group12/282742732/learning_1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/List.java @@ -0,0 +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(); +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/Queue.java b/group12/282742732/learning_1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..d16a441644 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group12/282742732/learning_1/src/com/coding/basic/Stack.java b/group12/282742732/learning_1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..31b24fd136 --- /dev/null +++ b/group12/282742732/learning_1/src/com/coding/basic/Stack.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if (elementData.size()>0) { + return false; + } + return true; + } + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + + Stack st = new Stack(); + st.push("aaa"); + st.push("bbb"); + st.push("ccc"); + System.out.println(st.isEmpty()); + int length = st.size(); + + for (int i = 0; i < length; i++) { + System.out.println(st.peek()); + } + + System.out.println(st.isEmpty()); + } +} From 9c41d7429b2984fb25efd950c1eabd01811bdf01 Mon Sep 17 00:00:00 2001 From: ls00002 <2686360536@qq.com> Date: Sun, 26 Feb 2017 22:57:11 +0800 Subject: [PATCH 03/28] homework --- group12/2686360536/ArrayList.java | 34 +++++++++++++ group12/2686360536/LinkedList.java | 80 ++++++++++++++++++++++++++++++ group12/2686360536/List.java | 9 ++++ group12/2686360536/Node.java | 43 ++++++++++++++++ group12/2686360536/Queue.java | 28 +++++++++++ group12/2686360536/Stack.java | 38 ++++++++++++++ 6 files changed, 232 insertions(+) create mode 100644 group12/2686360536/ArrayList.java create mode 100644 group12/2686360536/LinkedList.java create mode 100644 group12/2686360536/List.java create mode 100644 group12/2686360536/Node.java create mode 100644 group12/2686360536/Queue.java create mode 100644 group12/2686360536/Stack.java diff --git a/group12/2686360536/ArrayList.java b/group12/2686360536/ArrayList.java new file mode 100644 index 0000000000..cf7eb423b0 --- /dev/null +++ b/group12/2686360536/ArrayList.java @@ -0,0 +1,34 @@ +package org.dul.CodingTask.homework_2017_2_25; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] data = new Object[100]; + + public void add(Object object) { + data[size] = object; + size++; + } + + public void add(int index, Object object) { + + } + + public Object get(int index) { + return data[index]; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group12/2686360536/LinkedList.java b/group12/2686360536/LinkedList.java new file mode 100644 index 0000000000..6a8d0b237f --- /dev/null +++ b/group12/2686360536/LinkedList.java @@ -0,0 +1,80 @@ +package org.dul.CodingTask.homework_2017_2_25; + +public class LinkedList implements List { + + private Node start; + private Node end; + private int size; + + public void add(Object object) { + if (start == null) { + start = new Node(object); + end = start; + } else { + Node node = new Node(object); + start.setNext(node); + end = node; + } + size++; + } + + public void add(int index, Object object) { + } + + + public Object get(int index) { + if(index > size -1) + return null; + + Node current = start; + for (int i = 0; i < index; i++) { + current = current.getNext(); + } + return current.getData(); + } + + public Object remove(int index) { + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object object) { + Node node = new Node(object, start); + start = node; + } + + public void addEnd(Object object) { + Node node = new Node(object); + end.setNext(node); + end = node; + } + + public Object removeFirst() { + Object data = start.getData(); + start = start.getNext(); + return data; + + } + + public Object removeEnd() { + Object data = end.getData(); + Node current = start; + while(true){ + if (current.getNext() == end){ + end = current; + current.setNext(null); + break; + }else { + current = current.getNext(); + } + } + return data; + } + + public Iterator iterator() { + return null; + } +} diff --git a/group12/2686360536/List.java b/group12/2686360536/List.java new file mode 100644 index 0000000000..7822b0e26c --- /dev/null +++ b/group12/2686360536/List.java @@ -0,0 +1,9 @@ +package org.dul.CodingTask.homework_2017_2_25; + +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/group12/2686360536/Node.java b/group12/2686360536/Node.java new file mode 100644 index 0000000000..999e3ee828 --- /dev/null +++ b/group12/2686360536/Node.java @@ -0,0 +1,43 @@ +package org.dul.CodingTask.homework_2017_2_25; + +public class Node { + private Object data; + private Node next; + private Node previous; + + public Node() { + } + + public Node(Object data){ + this.data = data; + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node getPrevious() { + return previous; + } + + public void setPrevious(Node previous) { + this.previous = previous; + } +} \ No newline at end of file diff --git a/group12/2686360536/Queue.java b/group12/2686360536/Queue.java new file mode 100644 index 0000000000..10adea7fc4 --- /dev/null +++ b/group12/2686360536/Queue.java @@ -0,0 +1,28 @@ +package org.dul.CodingTask.homework_2017_2_25; + +public class Queue { + private ArrayList elements = new ArrayList(); + + public void enQueue(Object object) { + elements.add(object); + } + + public Object deQueue() { + if (isEmpty()) + return null; + else { + return elements.remove(elements.size() - 1); + } + } + + public boolean isEmpty() { + if (elements.size() == 0) + return true; + else + return false; + } + + public int size() { + return elements.size(); + } +} diff --git a/group12/2686360536/Stack.java b/group12/2686360536/Stack.java new file mode 100644 index 0000000000..e2908b0fa5 --- /dev/null +++ b/group12/2686360536/Stack.java @@ -0,0 +1,38 @@ +package org.dul.CodingTask.homework_2017_2_25; + +public class Stack { + private ArrayList elements = new ArrayList(); + + public void push(Object object) { + elements.add(object); + } + + public Object pop() { + if (isEmpty()) + return null; + else { + return elements.remove(elements.size() - 1); + } + + } + + public Object peek() { + if (isEmpty()) + return null; + else { + return elements.get(elements.size() - 1); + } + } + + public boolean isEmpty() { + if (elements.size() == 0) + return true; + else + return false; + } + + public int size() { + return elements.size(); + } + +} From 2ef160ab17cb1d8d787417c77b1f90b77f10444a Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Mon, 27 Feb 2017 17:29:39 +0800 Subject: [PATCH 04/28] =?UTF-8?q?2-26=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/array/ArrayUtil.java | 219 ++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../src/com/coderising/litestruts/Struts.java | 32 +++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/struts.xml | 11 + .../src/com/coding/basic/ArrayList.java | 6 +- .../src/test/com/coderising/StrutsTest.java | 46 ++++ 7 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java create mode 100644 group12/2258659044/zj-2017/src/com/coderising/litestruts/LoginAction.java create mode 100644 group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java create mode 100644 group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java create mode 100644 group12/2258659044/zj-2017/src/com/coderising/litestruts/struts.xml create mode 100644 group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java diff --git a/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java b/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..fe8f130afb --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,219 @@ +package com.coderising.array; + +import com.coding.basic.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 length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) { + temp[i] = origin[length-1-i]; + } + System.arraycopy(temp, 0, origin, 0, 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 length = oldArray.length; + int[] tempArr = new int[length]; + int j = 0; + int zeroNum = 0;//储存0的个数 + for (int i = 0; i < length; i++) { + if(oldArray[i]!=0){ + tempArr[j] = oldArray[i]; + j ++; + }else{ + zeroNum ++; + } + } + //删除数组尾端的0 + int[] newArray = new int[length-zeroNum]; + System.arraycopy(tempArr, 0, newArray, 0, length-zeroNum); + 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 length1 = array1.length; + int length2 = array2.length; + int[] array3 = new int[length1 + length2]; + //将array1、array2的值加入array3中 + System.arraycopy(array1, 0, array3, 0, length1); + System.arraycopy(array2, 0, array3, length1, length2); + int length = array3.length; + int temp; + //将array3冒泡排序 + for (int i = 0; i < length; i++) { + for (int j = 0; j < length - i; j++) { + if(array3[i]>array3[j+i]){ + temp = array3[i]; + array3[i] = array3[j+i]; + array3[j+i] = temp; + } + } + } + return duplicate(array3); + } + + /** + *去重 + */ + private int[] duplicate(int[] array){ + + for (int i = 1; i < array.length; i++) { + if(array[i-1]==array[i]){ + array[i] = 0; + } + } + return removeZero(array); + } + + /** + * 把一个已经存满数据的数组 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]; + System.arraycopy(oldArray, 0,newArray , 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + + int[] array = {}; + if(max <= 1)return array; + //生成 斐波那契数列的ArrayList集合 + ArrayList ls = new ArrayList(); + ls.add(1);ls.add(1); + int next;int i = 1; + while(true){ + next = (int)ls.get(i) +(int)ls.get(i-1); + if(next >= max){ + break; + } + ls.add(next); + i ++; + } + return objList2int(ls); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + ArrayList primesList = new ArrayList(); + boolean flag; + for (int i = 2; i < max; i++) { + flag = false; + for (int j = 2; j <= Math.sqrt(i); j++) { + if (i % j == 0) { + flag =true; + break; + } + } + if(!flag){ + primesList.add(i); + } + } + return objList2int(primesList); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + int temp; + ArrayList perfectList = new ArrayList(); + for (int i = 6; i <= max; i++) { + temp = 0; + for (int j = 1; j <= (i/2); j++) { + if(i%j == 0){ + temp += j; + } + } + if(temp == i){ + perfectList.add(i); + } + } + return objList2int(perfectList); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + str.append(array[i]+seperator); + } + return str.substring(0, str.lastIndexOf(seperator)); + } + + /** + * 将存储int数据的ArrayList转换为int数组 + * @param ls + * @return + */ + public int[] objList2int(ArrayList ls){ + + Object[] objArr = ls.toArray(); + int[] array = new int[ls.size()]; + for (int i = 0; i < ls.size(); i++) { + array[i] = (int) objArr[i]; + } + return array; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/LoginAction.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..a3ce652047 --- /dev/null +++ b/group12/2258659044/zj-2017/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; + } +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..2d52453f26 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,32 @@ +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; + } + +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..9024d72158 --- /dev/null +++ b/group12/2258659044/zj-2017/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; + } +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/struts.xml b/group12/2258659044/zj-2017/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group12/2258659044/zj-2017/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/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index 5de89da950..c15e99848b 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -52,7 +52,11 @@ public Object remove(int index){ public int size(){ return size; } - + + public Object[] toArray(){ + return elementData.clone(); + } + /** * 扩容,扩容因子为10 */ diff --git a/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java b/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java new file mode 100644 index 0000000000..2cb51e3967 --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java @@ -0,0 +1,46 @@ +package test.com.coderising; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.Struts; +import com.coderising.litestruts.View; + + + + + +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")); + } +} \ No newline at end of file From 7e60614cb4bba9183a4014530db517460638832a Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Mon, 27 Feb 2017 18:00:03 +0800 Subject: [PATCH 05/28] =?UTF-8?q?=E8=AF=BB=E5=8F=96xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/litestruts/Struts.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index 2d52453f26..baa67f6aae 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -1,7 +1,18 @@ package com.coderising.litestruts; +import java.io.File; +import java.io.IOException; import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + public class Struts { public static View runAction(String actionName, Map parameters) { @@ -29,4 +40,27 @@ public static View runAction(String actionName, Map parameters) { return null; } + private void readStrutsXml(String filePath){ + + File xmlFile = new File("struts.xml"); + try { + DocumentBuilder documentBuilder = DocumentBuilderFactory + .newInstance().newDocumentBuilder(); + Document document = documentBuilder.parse(xmlFile); + //获取根节点 + Element element = document.getDocumentElement(); + //http://www.jb51.net/article/44338.htm + NodeList childNodes = element.getChildNodes(); + + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } \ No newline at end of file From 0a2e978d7547d6c207ded2044fe4deda2617b204 Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Mon, 27 Feb 2017 20:18:28 +0800 Subject: [PATCH 06/28] new Exs --- group12/382266293/.classpath | 16 +-- .../basic}/Collection/AbstractList.java | 0 .../basic}/Collection/Concrete/ArrayList.java | 0 .../Collection/Concrete/BinaryTreeNode.java | 0 .../Collection/Concrete/LinkedList.java | 0 .../basic}/Collection/Concrete/Queue.java | 0 .../basic}/Collection/Concrete/Stack.java | 0 .../basic}/Collection/Iterator.java | 0 .../basic}/Collection/List.java | 0 .../basic}/TestCollection/AllTests.java | 0 .../basic}/TestCollection/ArrayListTest.java | 0 .../TestCollection/BinaryTreeNodeTest.java | 0 .../basic}/TestCollection/LinkedListTest.java | 0 .../basic}/TestCollection/QueueTest.java | 0 .../basic}/TestCollection/StackTest.java | 0 .../{src => coding/basic}/util/Print.java | 0 .../{src => coding/basic}/util/TestUtil.java | 8 ++ group12/382266293/lib/.gitignore | 1 + group12/382266293/src/TestDom4J.java | 59 +++++++++ group12/382266293/src/array/ArrayUtil.java | 115 ++++++++++++++++++ .../382266293/src/array/ArrayUtilTest.java | 83 +++++++++++++ .../382266293/src/litestruts/LoginAction.java | 39 ++++++ group12/382266293/src/litestruts/Struts.java | 34 ++++++ .../382266293/src/litestruts/StrutsTest.java | 43 +++++++ group12/382266293/src/litestruts/View.java | 23 ++++ group12/382266293/src/litestruts/struts.xml | 11 ++ 26 files changed, 425 insertions(+), 7 deletions(-) rename group12/382266293/{src => coding/basic}/Collection/AbstractList.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/ArrayList.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/BinaryTreeNode.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/LinkedList.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/Queue.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/Stack.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Iterator.java (100%) rename group12/382266293/{src => coding/basic}/Collection/List.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/AllTests.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/ArrayListTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/BinaryTreeNodeTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/LinkedListTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/QueueTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/StackTest.java (100%) rename group12/382266293/{src => coding/basic}/util/Print.java (100%) rename group12/382266293/{src => coding/basic}/util/TestUtil.java (90%) create mode 100644 group12/382266293/lib/.gitignore create mode 100644 group12/382266293/src/TestDom4J.java create mode 100644 group12/382266293/src/array/ArrayUtil.java create mode 100644 group12/382266293/src/array/ArrayUtilTest.java create mode 100644 group12/382266293/src/litestruts/LoginAction.java create mode 100644 group12/382266293/src/litestruts/Struts.java create mode 100644 group12/382266293/src/litestruts/StrutsTest.java create mode 100644 group12/382266293/src/litestruts/View.java create mode 100644 group12/382266293/src/litestruts/struts.xml diff --git a/group12/382266293/.classpath b/group12/382266293/.classpath index 3e0fb272a8..97b7b051d0 100644 --- a/group12/382266293/.classpath +++ b/group12/382266293/.classpath @@ -1,7 +1,9 @@ - - - - - - - + + + + + + + + + diff --git a/group12/382266293/src/Collection/AbstractList.java b/group12/382266293/coding/basic/Collection/AbstractList.java similarity index 100% rename from group12/382266293/src/Collection/AbstractList.java rename to group12/382266293/coding/basic/Collection/AbstractList.java diff --git a/group12/382266293/src/Collection/Concrete/ArrayList.java b/group12/382266293/coding/basic/Collection/Concrete/ArrayList.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/ArrayList.java rename to group12/382266293/coding/basic/Collection/Concrete/ArrayList.java diff --git a/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java b/group12/382266293/coding/basic/Collection/Concrete/BinaryTreeNode.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/BinaryTreeNode.java rename to group12/382266293/coding/basic/Collection/Concrete/BinaryTreeNode.java diff --git a/group12/382266293/src/Collection/Concrete/LinkedList.java b/group12/382266293/coding/basic/Collection/Concrete/LinkedList.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/LinkedList.java rename to group12/382266293/coding/basic/Collection/Concrete/LinkedList.java diff --git a/group12/382266293/src/Collection/Concrete/Queue.java b/group12/382266293/coding/basic/Collection/Concrete/Queue.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/Queue.java rename to group12/382266293/coding/basic/Collection/Concrete/Queue.java diff --git a/group12/382266293/src/Collection/Concrete/Stack.java b/group12/382266293/coding/basic/Collection/Concrete/Stack.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/Stack.java rename to group12/382266293/coding/basic/Collection/Concrete/Stack.java diff --git a/group12/382266293/src/Collection/Iterator.java b/group12/382266293/coding/basic/Collection/Iterator.java similarity index 100% rename from group12/382266293/src/Collection/Iterator.java rename to group12/382266293/coding/basic/Collection/Iterator.java diff --git a/group12/382266293/src/Collection/List.java b/group12/382266293/coding/basic/Collection/List.java similarity index 100% rename from group12/382266293/src/Collection/List.java rename to group12/382266293/coding/basic/Collection/List.java diff --git a/group12/382266293/src/TestCollection/AllTests.java b/group12/382266293/coding/basic/TestCollection/AllTests.java similarity index 100% rename from group12/382266293/src/TestCollection/AllTests.java rename to group12/382266293/coding/basic/TestCollection/AllTests.java diff --git a/group12/382266293/src/TestCollection/ArrayListTest.java b/group12/382266293/coding/basic/TestCollection/ArrayListTest.java similarity index 100% rename from group12/382266293/src/TestCollection/ArrayListTest.java rename to group12/382266293/coding/basic/TestCollection/ArrayListTest.java diff --git a/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java b/group12/382266293/coding/basic/TestCollection/BinaryTreeNodeTest.java similarity index 100% rename from group12/382266293/src/TestCollection/BinaryTreeNodeTest.java rename to group12/382266293/coding/basic/TestCollection/BinaryTreeNodeTest.java diff --git a/group12/382266293/src/TestCollection/LinkedListTest.java b/group12/382266293/coding/basic/TestCollection/LinkedListTest.java similarity index 100% rename from group12/382266293/src/TestCollection/LinkedListTest.java rename to group12/382266293/coding/basic/TestCollection/LinkedListTest.java diff --git a/group12/382266293/src/TestCollection/QueueTest.java b/group12/382266293/coding/basic/TestCollection/QueueTest.java similarity index 100% rename from group12/382266293/src/TestCollection/QueueTest.java rename to group12/382266293/coding/basic/TestCollection/QueueTest.java diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/coding/basic/TestCollection/StackTest.java similarity index 100% rename from group12/382266293/src/TestCollection/StackTest.java rename to group12/382266293/coding/basic/TestCollection/StackTest.java diff --git a/group12/382266293/src/util/Print.java b/group12/382266293/coding/basic/util/Print.java similarity index 100% rename from group12/382266293/src/util/Print.java rename to group12/382266293/coding/basic/util/Print.java diff --git a/group12/382266293/src/util/TestUtil.java b/group12/382266293/coding/basic/util/TestUtil.java similarity index 90% rename from group12/382266293/src/util/TestUtil.java rename to group12/382266293/coding/basic/util/TestUtil.java index 2dfeeade7f..b9f996bc56 100644 --- a/group12/382266293/src/util/TestUtil.java +++ b/group12/382266293/coding/basic/util/TestUtil.java @@ -16,6 +16,14 @@ public class TestUtil extends TestCase { private static final int RANDOM_SIZE = 500; + public static int[] getRandomIntArray(int number) { + int[] arr = new int[number]; + for (int i = 0; i < arr.length; i++) { + arr[i] = getRandomNumber(); + } + return arr; + } + public static int getRandomNumber() { return random.nextInt(RANDOM_SIZE); } diff --git a/group12/382266293/lib/.gitignore b/group12/382266293/lib/.gitignore new file mode 100644 index 0000000000..3d08860a86 --- /dev/null +++ b/group12/382266293/lib/.gitignore @@ -0,0 +1 @@ +/dom4j-1.6.1.zip diff --git a/group12/382266293/src/TestDom4J.java b/group12/382266293/src/TestDom4J.java new file mode 100644 index 0000000000..b5dabfcc0c --- /dev/null +++ b/group12/382266293/src/TestDom4J.java @@ -0,0 +1,59 @@ +import java.net.URL; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; + +import static util.Print.*; + +public class TestDom4J { + + public Document parse(String add) throws DocumentException { + SAXReader reader = new SAXReader(); + Document document = reader.read(add); + return document; + } + + + + public void bar(Document document) { + List list = document.selectNodes( "/struts-config/action-mappings/action" ); + Node node = document.selectSingleNode( "/struts-config/action-mappings/action/forward" ); + + String name = node.valueOf( "@name" ); + println(name); + } + + public void listNodes(Element node){ + System.out.println("当前节点的名称:" + node.getName()); + //首先获取当前节点的所有属性节点 + List list = node.attributes(); + //遍历属性节点 + for(Attribute attribute : list){ + System.out.println("属性"+attribute.getName() +":" + attribute.getValue()); + } + //如果当前节点内容不为空,则输出 + if(!(node.getTextTrim().equals(""))){ + System.out.println( node.getName() + ":" + node.getText()); + } + //同时迭代当前节点下面的所有子节点 + //使用递归 + Iterator iterator = node.elementIterator(); + while(iterator.hasNext()){ + Element e = iterator.next(); + listNodes(e); + } + } + + public static void main(String args[]) throws DocumentException { + + + + } + +} diff --git a/group12/382266293/src/array/ArrayUtil.java b/group12/382266293/src/array/ArrayUtil.java new file mode 100644 index 0000000000..a9a4a66491 --- /dev/null +++ b/group12/382266293/src/array/ArrayUtil.java @@ -0,0 +1,115 @@ +package array; + +import static util.Print.*; + +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 len = origin.length; + int temp; + for (int i = 0; i < len/2; i++) { + temp = origin[i]; + origin[i] = origin[len-1-i]; + origin[len-1-i] = temp; + } + + } + + public static void main(String args[]) { + + ArrayUtil arrayUtil = new ArrayUtil(); + int[] my = new int[] {1,2,3,4,5}; + arrayUtil.reverseArray(my); + println(my); + } + + + /** + * 现在有如下的一个数组: 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; + } + + +} \ No newline at end of file diff --git a/group12/382266293/src/array/ArrayUtilTest.java b/group12/382266293/src/array/ArrayUtilTest.java new file mode 100644 index 0000000000..4e8df6912c --- /dev/null +++ b/group12/382266293/src/array/ArrayUtilTest.java @@ -0,0 +1,83 @@ +package array; + +import static org.junit.Assert.*; +import static util.TestUtil.*; + +import java.util.Arrays; + +import static util.Print.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + + private int[] myArr; + ArrayUtil au = new ArrayUtil(); + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + myArr = null; + } + + + @Test + public void testReverseArray() { + + int size = getRandomNumber(); + int[] expected = getRandomIntArray(size); + int[] myArr = Arrays.copyOf(expected, size); + + au.reverseArray(myArr); + + for (int i = 0; i < size; i++) { + assertEquals(expected[i], myArr[size-1-i]); + } + + } + + + + + @Test + public void testRemoveZero() { + + } + + @Test + public void testMerge() { + + } + + @Test + public void testGrow() { + + } + + @Test + public void testFibonacci() { + + } + + @Test + public void testGetPrimes() { + + } + + @Test + public void testGetPerfectNumbers() { + + } + + @Test + public void testJoin() { + + } + +} diff --git a/group12/382266293/src/litestruts/LoginAction.java b/group12/382266293/src/litestruts/LoginAction.java new file mode 100644 index 0000000000..c37c250014 --- /dev/null +++ b/group12/382266293/src/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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; + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/Struts.java b/group12/382266293/src/litestruts/Struts.java new file mode 100644 index 0000000000..f26fa8d8b7 --- /dev/null +++ b/group12/382266293/src/litestruts/Struts.java @@ -0,0 +1,34 @@ +package 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; + } + +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/StrutsTest.java b/group12/382266293/src/litestruts/StrutsTest.java new file mode 100644 index 0000000000..e08fab55c9 --- /dev/null +++ b/group12/382266293/src/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package 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")); + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/View.java b/group12/382266293/src/litestruts/View.java new file mode 100644 index 0000000000..5a7d948765 --- /dev/null +++ b/group12/382266293/src/litestruts/View.java @@ -0,0 +1,23 @@ +package 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; + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/struts.xml b/group12/382266293/src/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group12/382266293/src/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 From 73ca23170c4a738564b912091553a1d0d3549bda Mon Sep 17 00:00:00 2001 From: 2319021847 <2319021847@2319021847> Date: Mon, 27 Feb 2017 21:19:38 +0800 Subject: [PATCH 07/28] homework1 --- .../homework1/com/code/basic/ArrayList.java | 106 ++++++++++++++++++ .../com/code/basic/BinaryTreeNode.java | 53 +++++++++ .../homework1/com/code/basic/Iterator.java | 6 + .../homework1/com/code/basic/LinkList.java | 86 ++++++++++++++ .../homework1/com/code/basic/LinkNode.java | 46 ++++++++ .../homework1/com/code/basic/List.java | 14 +++ .../homework1/com/code/basic/Queue.java | 70 ++++++++++++ .../homework1/com/code/basic/Stack.java | 45 ++++++++ 8 files changed, 426 insertions(+) create mode 100644 group12/2319021847/homework1/com/code/basic/ArrayList.java create mode 100644 group12/2319021847/homework1/com/code/basic/BinaryTreeNode.java create mode 100644 group12/2319021847/homework1/com/code/basic/Iterator.java create mode 100644 group12/2319021847/homework1/com/code/basic/LinkList.java create mode 100644 group12/2319021847/homework1/com/code/basic/LinkNode.java create mode 100644 group12/2319021847/homework1/com/code/basic/List.java create mode 100644 group12/2319021847/homework1/com/code/basic/Queue.java create mode 100644 group12/2319021847/homework1/com/code/basic/Stack.java diff --git a/group12/2319021847/homework1/com/code/basic/ArrayList.java b/group12/2319021847/homework1/com/code/basic/ArrayList.java new file mode 100644 index 0000000000..942bce6c2a --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List{ + + private int size = 0; + private Object[] elements; + + public ArrayList() { + this.elements = new Object[5]; + } + + public ArrayList(int size) { + this.elements = new Object[size]; + } + public void add(Object o) { + // TODO Auto-generated method stub + if(isFull()) + { + resize(); + } + this.elements[this.size] = o; + this.size++; + } + public boolean isFull () + { + if(this.size == this.elements.length) + { + return true; + } + + return false; + } + + public void resize() + { + Object[] newElements = new Object[this.elements.length*2]; + System.arraycopy(elements, 0, newElements, 0, elements.length); + this.elements = newElements; + newElements = null; + } + public void add(int index, Object o) { + // TODO Auto-generated method stub + rangeCheck(index); + if(isFull()) + { + resize(); + } + System.arraycopy(elements, index, elements, index+1,size-index); + this.elements[index] = o; + size++; + } + void rangeCheck(int index) + { + if(index > size || index < 0) + { + throw new IndexOutOfBoundsException("�±�Խ��"); + } + } + public Object get(int index) { + // TODO Auto-generated method stub + rangeCheck(index); + return elements[index]; + } + + public Object remove(int index) { + // TODO Auto-generated method stub + rangeCheck(index); + Object elem = elements[index]; + System.arraycopy(elements, index+1, elements, index, size-index-1); + size--; + elements[size] = null; + return elem; + } + + public int size() { + // TODO Auto-generated method stub + return this.size; + } + public com.coding.basic.Iterator Iterator () + { + return new Itr(); + } + public class Itr implements com.coding.basic.Iterator{ + int cur = 0; + public boolean hasNext() { + // TODO Auto-generated method stub + if(size==cur) + { + return false; + } + return true; + } + + public Object next() { + // TODO Auto-generated method stub + int i = cur; + if(i < elements.length) + { + cur = i+1; + return elements[i]; + } + return null; + } + + } +} diff --git a/group12/2319021847/homework1/com/code/basic/BinaryTreeNode.java b/group12/2319021847/homework1/com/code/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..2e026b47b6 --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(Object o){ + this.data = o; + this.left = null; + this.right = null; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(BinaryTreeNode root, Object o){ + BinaryTreeNode current = root; + BinaryTreeNode node = new BinaryTreeNode(o); + if(current == null) + { + current = node; + } + else if(((Integer)current.getData()).intValue() < ((Integer)o).intValue()) + { + insert(current.getLeft(),o); + } + else if(((Integer)current.getData()).intValue() >= ((Integer)o).intValue()) + { + insert(current.getRight(),o); + } + + return node; + } + +} diff --git a/group12/2319021847/homework1/com/code/basic/Iterator.java b/group12/2319021847/homework1/com/code/basic/Iterator.java new file mode 100644 index 0000000000..f6ecde73bf --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group12/2319021847/homework1/com/code/basic/LinkList.java b/group12/2319021847/homework1/com/code/basic/LinkList.java new file mode 100644 index 0000000000..9f759f9088 --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/LinkList.java @@ -0,0 +1,86 @@ +package com.coding.basic; + +public class LinkList implements List{ + + private LinkNode head; + // TODO Auto-generated method stub + private int size; + + public LinkList() + { + this.head = null; + this.size = 0; + } + public void add(Object o) { + // TODO Auto-generated method stub + LinkNode currPtr = this.head; + if(this.head == null) + { + this.head = new LinkNode(o, this.head ,null); + } + while(currPtr != null) + { + currPtr = currPtr.getNext(); + } + currPtr.setNext(new LinkNode(o, currPtr, null)); + size++; + } + + public void add(int index, Object o) { + // TODO Auto-generated method stub + LinkNode currPtr = this.head; + if(index < 0 || index > size) + throw new IndexOutOfBoundsException("�±�Խ��"); + int i = 0; + if(index == 0) + { + LinkNode node = new LinkNode(o,currPtr,currPtr.getPrv()); + currPtr.getNext().setPrv(node); + currPtr = node; + } + while(i < index) + { + currPtr = currPtr.getNext(); + i++; + } + LinkNode node = new LinkNode(o,currPtr.getPrv(),currPtr); + currPtr.getPrv().setNext(node); + currPtr.setPrv(node); + size++; + } + + public Object get(int index) { + if(index < 0 || index > size) + throw new IndexOutOfBoundsException("�±�Խ��"); + int i = 0; + LinkNode currPtr = this.head; + while(i < index) + { + currPtr = currPtr.getNext(); + i++; + } + + return currPtr.getData(); + } + + public Object remove(int index) { + // TODO Auto-generated method stub + int i = 0; + LinkNode currPtr = this.head; + while(i < index) + { + currPtr = currPtr.getNext(); + i++; + } + currPtr.getNext().setPrv(currPtr.getPrv()); + currPtr.getPrv().setNext( currPtr.getNext()); + size--; + return currPtr.getData(); + } + + public int size() { + // TODO Auto-generated method stub + return size; + } + +} diff --git a/group12/2319021847/homework1/com/code/basic/LinkNode.java b/group12/2319021847/homework1/com/code/basic/LinkNode.java new file mode 100644 index 0000000000..1452dcc42b --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/LinkNode.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkNode { + private LinkNode pNextPtr; + private LinkNode pPrvPtr; + private Object ndata; + + public LinkNode () + { + this.pNextPtr=null; + this.pPrvPtr=null; + this.ndata=null; + } + + public LinkNode (Object o, LinkNode pPrvPtr, LinkNode pNextPtr) + { + this.pNextPtr=null; + this.pPrvPtr=null; + this.ndata=null; + } + + public void setNext(LinkNode node) + { + this.pNextPtr = node; + } + + public void setPrv(LinkNode node) + { + this.pPrvPtr = node; + } + + public LinkNode getNext() + { + return this.pNextPtr; + } + + public LinkNode getPrv() + { + return this.pPrvPtr; + } + public Object getData() + { + return this.ndata; + } + +} diff --git a/group12/2319021847/homework1/com/code/basic/List.java b/group12/2319021847/homework1/com/code/basic/List.java new file mode 100644 index 0000000000..8360496919 --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/List.java @@ -0,0 +1,14 @@ +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/group12/2319021847/homework1/com/code/basic/Queue.java b/group12/2319021847/homework1/com/code/basic/Queue.java new file mode 100644 index 0000000000..4dac2bfc22 --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/Queue.java @@ -0,0 +1,70 @@ +package com.coding.basic; + +public class Queue { + private int size; + + private int head; + + private int tail; + + private Object[] queue; + + public Queue() + { + this.size = 0; + this.head = 0; + this.tail = 0; + } + + public void enQueue(Object o) { + if(isFull()) + { + resize(); + } + int newtail = (head+size)%queue.length; + queue[newtail] = o; + size++; + } + + public Object deQueue() { + if(isEmpty()) + { + return null; + } + Object oldHead = queue[head]; + head = (head-1)%queue.length; + size--; + return oldHead; + } + + public int getHead(){ + return head; + } + + public int getTail(){ + return tail; + } + + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + int diff = tail - head; + return diff; + } + + // + public boolean isFull(){ + return size == queue.length; + } + + + public void resize(){ + Object[] newQueue = new Object[2*(queue.length)]; + System.arraycopy(queue, 0, newQueue, 0, size); + this.queue = newQueue; + newQueue = null; + } +} diff --git a/group12/2319021847/homework1/com/code/basic/Stack.java b/group12/2319021847/homework1/com/code/basic/Stack.java new file mode 100644 index 0000000000..370197cb32 --- /dev/null +++ b/group12/2319021847/homework1/com/code/basic/Stack.java @@ -0,0 +1,45 @@ +package com.coding.basic; + + + +public class Stack { + private Object[] elements; + private int size; + + + public Stack() + { + this.size = 0; + this.elements = new Object[10]; + } + public void push(Object o){ + if(o == null) + return; + size++; + elements[size-1] = o; + } + + public Object pop(){ + if(isEmpty()) + { + return null; + } + Object pop = elements[size-1]; + size--; + return pop; + } + + public Object peek(){ + if(isEmpty()) + { + return null; + } + return elements[size-1]; + } + public boolean isEmpty(){ + return size < 1; + } + public int size(){ + return size; + } +} \ No newline at end of file From 84e382274ea9cce7771a00abd36a8dd566274d92 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Mon, 27 Feb 2017 22:53:58 +0800 Subject: [PATCH 08/28] struts --- .../src/com/coderising/litestruts/Struts.java | 150 +++++++++++++----- .../src/com/coderising/litestruts/struts.xml | 8 +- 2 files changed, 115 insertions(+), 43 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index baa67f6aae..e75250f7a8 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -2,6 +2,9 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; @@ -10,57 +13,126 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Struts { - public static View runAction(String actionName, Map parameters) { + private final static String ACTION = "action"; + private final static String RESULT = "result"; + private final static String NAME = "name"; + private final static String CLASS = "class"; + private final static String EXECUTE = "execute"; - /* - - 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 + public static View runAction(String actionName, + Map parameters) { + + + View view = new View(); + Map viewMap = new HashMap(); - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - return null; - } - - private void readStrutsXml(String filePath){ - - File xmlFile = new File("struts.xml"); - try { - DocumentBuilder documentBuilder = DocumentBuilderFactory - .newInstance().newDocumentBuilder(); - Document document = documentBuilder.parse(xmlFile); - //获取根节点 - Element element = document.getDocumentElement(); - //http://www.jb51.net/article/44338.htm - NodeList childNodes = element.getChildNodes(); - + String path = "src/com/coderising/litestruts/struts.xml"; + Map xmlMap = readStrutsXml(path, actionName); + + try { + String calssPath = xmlMap.get(CLASS); + Class clazz = Class.forName(calssPath); + Object action = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + String fieldName; + String methodName; + for (int i = 0; i < fields.length; i++) { + fieldName = fields[i].getName(); + if(parameters.containsKey(fieldName)){ + methodName = "set" + fieldName.substring(0, 1).toUpperCase() + + fieldName.substring(1); + Method method = clazz.getMethod(methodName, fields[i].getType()); + method.invoke(action, parameters.get(fieldName)); + } + } + + Method successMethos = clazz.getMethod(EXECUTE); + Object result = successMethos.invoke(action); + + for (int i = 0; i < fields.length; i++) { + fieldName = fields[i].getName(); + methodName = "get" + fieldName.substring(0, 1).toUpperCase() + + fieldName.substring(1); + Method method = clazz.getMethod(methodName); + Object value = method.invoke(action); + viewMap.put(fieldName, value); + + } + view.setParameters(viewMap); + + String jsp = xmlMap.get(result.toString()); + view.setJsp(jsp); + + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + + /** + * 读取struts.xml文件 + * + * @param filePath + * :struts.xml路劲 + * @param actionName + * @return + */ + private static Map readStrutsXml(String filePath, + String actionName) { + + File xmlFile = new File(filePath); + Map xmlMap = new HashMap(); + try { + DocumentBuilder documentBuilder = DocumentBuilderFactory + .newInstance().newDocumentBuilder(); + Document document = documentBuilder.parse(xmlFile); + // 获取根节点 + Element element = document.getDocumentElement(); + // http://www.jb51.net/article/44338.htm + NodeList actionNodes = element.getChildNodes(); + for (int i = 0; i < actionNodes.getLength(); i++) { + Node actionNode = actionNodes.item(i); + if (ACTION.equals(actionNode.getNodeName())) { + NamedNodeMap actionNodeMap = actionNode.getAttributes(); + String atName = actionNodeMap.getNamedItem(NAME) + .getNodeValue(); + if (atName.equals(actionName)) { + String classPath = actionNodeMap.getNamedItem(CLASS) + .getNodeValue(); + xmlMap.put(CLASS, classPath); + NodeList resultNodes = actionNode.getChildNodes(); + for (int j = 0; j < resultNodes.getLength(); j++) { + Node resultNode = resultNodes.item(j); + if (RESULT.equals(resultNode.getNodeName())) { + NamedNodeMap resultNodeMap = resultNode + .getAttributes(); + String jspName = resultNodeMap.getNamedItem( + NAME).getNodeValue(); + String jspPath = resultNode.getTextContent(); + xmlMap.put(jspName, jspPath); + } + } + + } + } + + } + } catch (ParserConfigurationException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } - } + return xmlMap; + } } \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/struts.xml b/group12/2258659044/zj-2017/src/com/coderising/litestruts/struts.xml index 99063bcb0c..4c6eeabbd4 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/struts.xml +++ b/group12/2258659044/zj-2017/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 From 8eaa97a2f43fecb04b33462cbb09bc2f42199b43 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Mon, 27 Feb 2017 22:55:21 +0800 Subject: [PATCH 09/28] =?UTF-8?q?=E4=BF=AE=E6=94=B9View=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zj-2017/src/com/coderising/litestruts/View.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java index 9024d72158..0c0b9d3b26 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/View.java @@ -4,7 +4,7 @@ public class View { private String jsp; - private Map parameters; + private Map parameters; public String getJsp() { return jsp; @@ -13,10 +13,10 @@ public View setJsp(String jsp) { this.jsp = jsp; return this; } - public Map getParameters() { + public Map getParameters() { return parameters; } - public View setParameters(Map parameters) { + public View setParameters(Map parameters) { this.parameters = parameters; return this; } From cbf8ec80b4be4a2874627c503b94534c471c782b Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Tue, 28 Feb 2017 07:21:02 +0800 Subject: [PATCH 10/28] Array Ex & Test --- .../382266293/coding/basic/util/Print.java | 6 ++ .../382266293/coding/basic/util/TestUtil.java | 9 +- group12/382266293/src/array/ArrayUtil.java | 101 +++++++++++++++--- .../382266293/src/array/ArrayUtilTest.java | 82 ++++++++++++-- 4 files changed, 173 insertions(+), 25 deletions(-) diff --git a/group12/382266293/coding/basic/util/Print.java b/group12/382266293/coding/basic/util/Print.java index b2ae48552b..81b5cf539e 100644 --- a/group12/382266293/coding/basic/util/Print.java +++ b/group12/382266293/coding/basic/util/Print.java @@ -1,5 +1,6 @@ package util; +import java.util.Arrays; public class Print { @@ -10,5 +11,10 @@ public static void print(Object o){ public static void println(Object o){ System.out.println(o); } + + public static void printArr(int[] arr) { + println(Arrays.toString(arr)); + } + } diff --git a/group12/382266293/coding/basic/util/TestUtil.java b/group12/382266293/coding/basic/util/TestUtil.java index b9f996bc56..0820e0e684 100644 --- a/group12/382266293/coding/basic/util/TestUtil.java +++ b/group12/382266293/coding/basic/util/TestUtil.java @@ -1,5 +1,6 @@ package util; +import java.util.Arrays; import java.util.Random; import Collection.List; @@ -16,10 +17,12 @@ public class TestUtil extends TestCase { private static final int RANDOM_SIZE = 500; + + public static int[] getRandomIntArray(int number) { int[] arr = new int[number]; for (int i = 0; i < arr.length; i++) { - arr[i] = getRandomNumber(); + arr[i] = getRandomNumber() + 1; } return arr; } @@ -28,6 +31,10 @@ public static int getRandomNumber() { return random.nextInt(RANDOM_SIZE); } + public static int getRandomNumber(int bound) { + return random.nextInt(bound); + } + public static void addIntWithNatureOrder(List myList, int numbers) { for (int acutal = 0; acutal < numbers ; acutal++) { diff --git a/group12/382266293/src/array/ArrayUtil.java b/group12/382266293/src/array/ArrayUtil.java index a9a4a66491..aafa3c410c 100644 --- a/group12/382266293/src/array/ArrayUtil.java +++ b/group12/382266293/src/array/ArrayUtil.java @@ -2,6 +2,10 @@ import static util.Print.*; +import java.util.Arrays; +import java.util.BitSet; + + public class ArrayUtil { /** @@ -13,25 +17,18 @@ public class ArrayUtil { */ public void reverseArray(int[] origin){ - int len = origin.length; int temp; - for (int i = 0; i < len/2; i++) { + int index = origin.length - 1; + int numbersToReverse = origin.length/2; + for (int i = 0; i < numbersToReverse ; i++) { temp = origin[i]; - origin[i] = origin[len-1-i]; - origin[len-1-i] = temp; + origin[i] = origin[index - i]; + origin[index - i] = temp; } } - public static void main(String args[]) { - - ArrayUtil arrayUtil = new ArrayUtil(); - int[] my = new int[] {1,2,3,4,5}; - arrayUtil.reverseArray(my); - println(my); - } - /** * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: @@ -39,10 +36,29 @@ public static void main(String args[]) { * @param oldArray * @return */ - + public int[] removeZero(int[] oldArray){ - return null; + + BitSet check = new BitSet(oldArray.length); + boolean isZero; + for (int i = 0; i < oldArray.length; i++) { + isZero = (oldArray[i] == 0) ? true : false; + check.set(i, isZero); + } + + int newSize = oldArray.length-check.cardinality(); + int[] newArr = new int[newSize]; + + int nextIndex = check.nextClearBit(0); + for(int i = 0 ; i < newSize ; i++) { + newArr[i] = oldArray[nextIndex]; + nextIndex = check.nextClearBit(nextIndex+1); + } + + return newArr; } + + /** * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 @@ -53,8 +69,34 @@ public int[] removeZero(int[] oldArray){ */ public int[] merge(int[] array1, int[] array2){ - return null; + + int len1 = array1.length; + int len2 = array2.length; + int len3 = array1[len1-1] < array2[len2-1] ? array2[len2-1] + 1: array1[len1-1] + 1; + int[] newArr = new int[len3]; + initialArray(newArr, -1); + for (int i = 0; i < len1; i++) { + newArr[array1[i]] = 0; + } + for (int i = 0; i < len2; i++) { + newArr[array2[i]] = 0; + } + int mergedLength = 0; + for (int i = 0; i < len3; i++) { + if (newArr[i] != -1) + newArr[mergedLength++] = i; + } + return Arrays.copyOf(newArr, mergedLength); } + + public static void initialArray(int[] arr, int j) { + for (int i = 0; i < arr.length; i++) { + arr[i] = j; + } + } + + + /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size * 注意,老数组的元素在新数组中需要保持 @@ -65,9 +107,19 @@ public int[] merge(int[] array1, int[] array2){ * @return */ public int[] grow(int [] oldArray, int size){ - return null; + + int[] newArr = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + + return newArr; } + + + + /** * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] @@ -79,6 +131,15 @@ public int[] fibonacci(int max){ return null; } + + public static void main(String args[]) { + + ArrayUtil aUtil = new ArrayUtil(); + + + + } + /** * 返回小于给定最大值max的所有素数数组 * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] @@ -108,7 +169,13 @@ public int[] getPerfectNumbers(int max){ * @return */ public String join(int[] array, String seperator){ - return null; + StringBuilder sb = new StringBuilder(); + 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/group12/382266293/src/array/ArrayUtilTest.java b/group12/382266293/src/array/ArrayUtilTest.java index 4e8df6912c..b242e4b73b 100644 --- a/group12/382266293/src/array/ArrayUtilTest.java +++ b/group12/382266293/src/array/ArrayUtilTest.java @@ -2,18 +2,20 @@ import static org.junit.Assert.*; import static util.TestUtil.*; - import java.util.Arrays; - +import java.util.Iterator; +import java.util.TreeSet; import static util.Print.*; import org.junit.After; import org.junit.Before; import org.junit.Test; + + public class ArrayUtilTest { - private int[] myArr; + private int[] actual; ArrayUtil au = new ArrayUtil(); @Before @@ -23,7 +25,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { - myArr = null; + actual = null; } @@ -32,12 +34,12 @@ public void testReverseArray() { int size = getRandomNumber(); int[] expected = getRandomIntArray(size); - int[] myArr = Arrays.copyOf(expected, size); + actual = Arrays.copyOf(expected, size); - au.reverseArray(myArr); + au.reverseArray(actual); for (int i = 0; i < size; i++) { - assertEquals(expected[i], myArr[size-1-i]); + assertEquals(expected[i], actual[size-1-i]); } } @@ -47,16 +49,71 @@ public void testReverseArray() { @Test public void testRemoveZero() { + + int size = getRandomNumber(10000); + int[] expected = getRandomIntArray(size); + int zeros = getRandomNumber(size-1); + TreeSet t = new TreeSet(); + while (t.size() != zeros) { + t.add(getRandomNumber(size)); + } + + for (Integer i : t) { + expected[i] = 0; + } + + int expectedSize = size - zeros; + actual = au.removeZero(expected); + assertEquals(expectedSize, actual.length); + + for (int i = 0, j = 0; i < size; i++) { + if (expected[i] != 0) + assertEquals(expected[i], actual[j++]); + } + } @Test public void testMerge() { + int[] arr1 = getRandomIntArray(getRandomNumber()); + int[] arr2 = getRandomIntArray(getRandomNumber()); + Arrays.sort(arr1); + Arrays.sort(arr2); + TreeSet t = new TreeSet(); + for (int i = 0; i < arr1.length; i++) { + t.add(arr1[i]); + } + for (int i = 0; i < arr2.length; i++) { + t.add(arr2[i]); + } + int[] actual = new int[arr1.length + arr2.length]; + actual = au.merge(arr1, arr2); + + assertEquals(t.size(), actual.length); + + Iterator it = t.iterator(); + for(int i = 0; it.hasNext(); i++) { + assertEquals((int)it.next(), actual[i]); + } } @Test public void testGrow() { + int[] expected = getRandomIntArray(getRandomNumber()); + int growSize = getRandomNumber(); + int[] actual = au.grow(expected, growSize); + + assertEquals(expected.length + growSize, actual.length); + + for (int i = 0; i < actual.length; i++) { + if (i < expected.length) { + assertEquals(expected[i], actual[i]); + } else { + assertEquals(0, actual[i]); + } + } } @@ -78,6 +135,17 @@ public void testGetPerfectNumbers() { @Test public void testJoin() { + int[] expected = getRandomIntArray(getRandomNumber()); + String seperator = "-"; + String joinedString = au.join(expected, seperator); + + String[] actual = joinedString.split(seperator); + + assertEquals(expected.length, actual.length); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], Integer.parseInt(actual[i])); + } + } } From e043badb8ae24a1ffa664433fadc3f71eb850b56 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Tue, 28 Feb 2017 10:05:02 +0800 Subject: [PATCH 11/28] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zj-2017/src/test/com/coderising/StrutsTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java b/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java index 2cb51e3967..e11d532f7f 100644 --- a/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java @@ -8,11 +8,6 @@ import com.coderising.litestruts.Struts; import com.coderising.litestruts.View; - - - - - public class StrutsTest { @Test From 04142847ca20fe8c23397888abfb670cbb44abd9 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Tue, 28 Feb 2017 11:20:01 +0800 Subject: [PATCH 12/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=8D=95=E5=85=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/array/ArrayUtilTest.java | 119 ++++++++++++++++++ .../{ => litestruts}/StrutsTest.java | 2 +- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java rename group12/2258659044/zj-2017/src/test/com/coderising/{ => litestruts}/StrutsTest.java (93%) diff --git a/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java b/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..7eae6540bf --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,119 @@ +package test.com.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.array.ArrayUtil; +import com.coding.basic.ArrayList; + +public class ArrayUtilTest { + + ArrayUtil au = new ArrayUtil(); + + @Test + public void testReverseArray() { + + int[] a = {7, 9, 30, 3, 4}; + int[] assertArray = {4,3,30,9,7}; + au.reverseArray(a); + assertResult(assertArray,a); + } + + @Test + public void testRemoveZero() { + + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] assertArray = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] newArr = au.removeZero(oldArr); + assertResult(assertArray,newArr); + + } + + @Test + public void testMerge() { + + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] assertArray = {3,4,5,6,7,8}; + int[] a3 = au.merge(a1, a2); + assertResult(assertArray,a3); + } + + @Test + public void testGrow() { + + int[] oldArray = {2,3,6}; + int size = 3; + int[] assertArray = {2,3,6,0,0,0}; + int[] resultArr = au.grow(oldArray, size); + assertResult(assertArray,resultArr); + } + + @Test + public void testFibonacci() { + + int[] assertArray = {1,1,2,3,5,8,13}; + int max = 15; + int[] resultArr = au.fibonacci(max); + assertResult(assertArray,resultArr); + max = 0; + int[] assertArray1 ={}; + int[] resultArr1 = au.fibonacci(max); + assertResult(assertArray1,resultArr1); + } + + @Test + public void testGetPrimes() { + + int[] assertArray = {2,3,5,7,11,13,17,19}; + int max = 23; + int[] resultArr = au.getPrimes(max); + assertResult(assertArray,resultArr); + } + + @Test + public void testGetPerfectNumbers() { + + int[] assertArray = {6,28,496}; + int max = 496; + int[] resultArr = au.getPerfectNumbers(max); + assertResult(assertArray,resultArr); + } + + @Test + public void testJoin() { + + int[] array = {6,5,8,9}; + String seperator = "*"; + String resulStr = au.join(array, seperator); + String assertStr = "6*5*8*9"; + Assert.assertEquals(assertStr, resulStr); + } + + @Test + public void testObjList2int() { + + ArrayList ls = new ArrayList(); + for (int i = 0; i < 10; i++) { + ls.add(i); + } + int[] resulArr = au.objList2int(ls); + Assert.assertEquals(ls.size(), resulArr.length); + for (int i = 0; i < resulArr.length; i++) { + Assert.assertEquals(i, resulArr[i]); + } + } + + /** + * 断言方法 + * @param assertArr 断言集合 + * @param resultArr 实际集合 + */ + private void assertResult(int[] assertArr,int[] resultArr){ + + Assert.assertEquals(assertArr.length,resultArr.length); + for (int i = 0; i < resultArr.length; i++) { + Assert.assertEquals(assertArr[i],resultArr[i]); + } + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java b/group12/2258659044/zj-2017/src/test/com/coderising/litestruts/StrutsTest.java similarity index 93% rename from group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java rename to group12/2258659044/zj-2017/src/test/com/coderising/litestruts/StrutsTest.java index e11d532f7f..74d04878cc 100644 --- a/group12/2258659044/zj-2017/src/test/com/coderising/StrutsTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coderising/litestruts/StrutsTest.java @@ -1,4 +1,4 @@ -package test.com.coderising; +package test.com.coderising.litestruts; import java.util.HashMap; import java.util.Map; From 653558d4986731ea50f2f4c83436c8d2ff5801ba Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Tue, 28 Feb 2017 11:21:41 +0800 Subject: [PATCH 13/28] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2258659044/zj-2017/src/com/coderising/litestruts/Struts.java | 1 - 1 file changed, 1 deletion(-) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index e75250f7a8..82e61dbcf2 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -96,7 +96,6 @@ private static Map readStrutsXml(String filePath, Document document = documentBuilder.parse(xmlFile); // 获取根节点 Element element = document.getDocumentElement(); - // http://www.jb51.net/article/44338.htm NodeList actionNodes = element.getChildNodes(); for (int i = 0; i < actionNodes.getLength(); i++) { Node actionNode = actionNodes.item(i); From 8598b0e64d177bb178413b766a7dfe9fd0b1dd39 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Tue, 28 Feb 2017 17:39:24 +0800 Subject: [PATCH 14/28] =?UTF-8?q?=E4=BF=AE=E6=94=B9ArrayList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2258659044/zj-2017/src/com/coding/basic/ArrayList.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index c15e99848b..017763d17e 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -54,7 +54,9 @@ public int size(){ } public Object[] toArray(){ - return elementData.clone(); + Object[] objArr = new Object[size]; + System.arraycopy(elementData, 0, objArr, 0, size); + return objArr; } /** From 1e25f0a3ccfc798b5e2825a74948143d9c654332 Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Tue, 28 Feb 2017 17:44:40 +0800 Subject: [PATCH 15/28] ArrayUtil answer and it's Junit Test example --- group12/247565311/week1/ArrayList.java | 10 +- group12/247565311/week2/ArrayUtil.java | 219 +++++++++++++++++++++ group12/247565311/week2/ArrayUtilTest.java | 141 +++++++++++++ 3 files changed, 365 insertions(+), 5 deletions(-) create mode 100644 group12/247565311/week2/ArrayUtil.java create mode 100644 group12/247565311/week2/ArrayUtilTest.java diff --git a/group12/247565311/week1/ArrayList.java b/group12/247565311/week1/ArrayList.java index c2643af683..f15c04e289 100644 --- a/group12/247565311/week1/ArrayList.java +++ b/group12/247565311/week1/ArrayList.java @@ -30,7 +30,7 @@ public boolean add(E arg0) { } data = newdata; } - data[leng] = arg0; + data[size-1] = arg0; return true; } @@ -233,17 +233,17 @@ public Object[] toArray() { if(this.size == 0) return null; Object[] res = new Object[this.size]; for(int i=0;i T[] toArray(T[] arg0) { T[] res = (T[])(new Object[this.size]); for(int i=0;i data = new ArrayList(); + int leno = oldArray.length; + for(int i=0;i data = new ArrayList(); + int p1=0,p2=0,len1=array1.length,len2=array2.length; + int val1=0,val2=0; + if(len1 == 0) return array2; + if(len2 == 0) return array1; + while(p1=len1){ + data.add(array2[p2]); + p2 += 1; + continue; + } + if(p2>=len2){ + data.add(array1[p1]); + p1 += 1; + continue; + } + if(p1val2){ + data.add(val2); + p2 += 1; + }else if(val1 data = new ArrayList(); + data.add(llast); + data.add(last); + while(last+llast li = new ArrayList(); + int cur = 2; + while(cur li = null,resli = new ArrayList(); + for(int i=6;i getAllElem(int arg0){ + ArrayList res = new ArrayList(); + for(int i=1;i Date: Tue, 28 Feb 2017 20:15:18 +0800 Subject: [PATCH 16/28] Array Exs --- group12/382266293/src/array/ArrayUtil.java | 97 ++++++++++++++----- .../382266293/src/array/ArrayUtilTest.java | 20 ++-- 2 files changed, 89 insertions(+), 28 deletions(-) diff --git a/group12/382266293/src/array/ArrayUtil.java b/group12/382266293/src/array/ArrayUtil.java index aafa3c410c..f33f64150b 100644 --- a/group12/382266293/src/array/ArrayUtil.java +++ b/group12/382266293/src/array/ArrayUtil.java @@ -1,9 +1,11 @@ package array; import static util.Print.*; - import java.util.Arrays; import java.util.BitSet; +import Collection.Iterator; +import Collection.Concrete.ArrayList; + public class ArrayUtil { @@ -25,7 +27,6 @@ public void reverseArray(int[] origin){ origin[i] = origin[index - i]; origin[index - i] = temp; } - } @@ -59,7 +60,6 @@ public int[] removeZero(int[] oldArray){ } - /** * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 @@ -95,8 +95,6 @@ public static void initialArray(int[] arr, int j) { } } - - /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size * 注意,老数组的元素在新数组中需要保持 @@ -112,13 +110,9 @@ public int[] grow(int [] oldArray, int size){ for (int i = 0; i < oldArray.length; i++) { newArr[i] = oldArray[i]; } - return newArr; } - - - - + /** * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 @@ -128,16 +122,17 @@ public int[] grow(int [] oldArray, int size){ * @return */ public int[] fibonacci(int max){ - return null; - } - - - public static void main(String args[]) { - - ArrayUtil aUtil = new ArrayUtil(); - - - + if (max == 1) + return new int[0]; + int[] result = new int[max]; + result[0] = result[1] = 1; + int count = 0; + for (int i = 2, j = 0; j < max ; i++) { + result[i] = result[i-1] + result[i-2]; + j = result[i]; + count++; + } + return Arrays.copyOf(result, ++count); } /** @@ -147,9 +142,39 @@ public static void main(String args[]) { * @return */ public int[] getPrimes(int max){ - return null; + + String temp = ""; + for(int i = 0; i < max; i++) { + if(isPrime(i)) { + temp += i + " "; + } + } + String[] tempArr = temp.split(" "); + int[] result = new int[tempArr.length]; + for (int i = 0; i < result.length; i++) { + result[i] = Integer.parseInt(tempArr[i]); + } + + return result; } + public static boolean isPrime(int num) { + + if (num <= 1) + return false; + + if (num == 2) + return true; + + for(int i = 2; i <= Math.sqrt(num) + 1; i++) { + if (num % i == 0) + return false; + } + + return true; + } + + /** * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 @@ -157,8 +182,36 @@ public int[] getPrimes(int max){ * @return */ public int[] getPerfectNumbers(int max){ - return null; + + int count = 0; + ArrayList myList = new ArrayList(); + for(int i = 1; i < max; i++) { + if(isPerfectNum(i)) { + count++; + myList.add(i); + } + } + int[] result = new int[count]; + Iterator iterator = myList.iterator(); + for (int i = 0; i < count; i++) { + result[i] = iterator.next(); + } + return result; + } + + + public static boolean isPerfectNum(int num) { + + int sum = 0; + for (int i = 1; i <= num/2; i++) { + if (num % i == 0) + sum += i; + } + + return (num == sum) ? true : false; + } + /** * 用seperator 把数组 array给连接起来 diff --git a/group12/382266293/src/array/ArrayUtilTest.java b/group12/382266293/src/array/ArrayUtilTest.java index b242e4b73b..22a3250613 100644 --- a/group12/382266293/src/array/ArrayUtilTest.java +++ b/group12/382266293/src/array/ArrayUtilTest.java @@ -43,9 +43,6 @@ public void testReverseArray() { } } - - - @Test public void testRemoveZero() { @@ -119,17 +116,28 @@ public void testGrow() { @Test public void testFibonacci() { - + int[] expected = new int[] {1, 1, 2, 3, 5, 8, 13}; + int[] acutal = new int[expected.length]; + actual = au.fibonacci(15); + assertArrayEquals(expected,actual); } @Test public void testGetPrimes() { - + + int[] expected = new int[] {2,3,5,7,11,13,17,19}; + int[] acutal = new int[expected.length]; + actual = au.getPrimes(23); + assertArrayEquals(expected,actual); } @Test public void testGetPerfectNumbers() { - + + int[] expected = new int[] {6, 28, 496, 8128}; + int[] acutal = new int[expected.length]; + actual = au.getPerfectNumbers(10000); + assertArrayEquals(expected,actual); } @Test From b0bfb33cd3b56bc8efe0b271a406b3ed850e82b3 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Wed, 1 Mar 2017 12:57:51 +0800 Subject: [PATCH 17/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BD=8D=E5=9B=BE?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/array/ArrayUtil.java | 29 +++++++++++++++++++ .../com/coderising/array/ArrayUtilTest.java | 5 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java b/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java index fe8f130afb..3f41a350e8 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java +++ b/group12/2258659044/zj-2017/src/com/coderising/array/ArrayUtil.java @@ -93,6 +93,35 @@ private int[] duplicate(int[] array){ return removeZero(array); } + /** + * 位图法合并 + * @param array1 + * @param array2 + * @return + */ + public int[] merge2(int[] array1, int[] array2){ + + int bitSize = 0; + int a = array1[array1.length-1] ; + int b = array2[array2.length-1]; + bitSize =(a>b)?a:b; + boolean[] bitmap = new boolean[bitSize+1]; + for (int i = 0; i < array1.length; i++) { + bitmap[array1[i]]=true; + } + for (int i = 0; i < array2.length; i++) { + bitmap[array2[i]]=true; + } + + ArrayList ls = new ArrayList(); + for (int i = 0; i < bitmap.length; i++) { + if(bitmap[i]==true){ + ls.add(i); + } + } + return objList2int(ls); + } + /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size * 注意,老数组的元素在新数组中需要保持 diff --git a/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java b/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java index 7eae6540bf..ea04dbbf3b 100644 --- a/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coderising/array/ArrayUtilTest.java @@ -34,8 +34,9 @@ public void testMerge() { int[] a1 = {3, 5, 7,8}; int[] a2 = {4, 5, 6,7}; - int[] assertArray = {3,4,5,6,7,8}; - int[] a3 = au.merge(a1, a2); + int[] assertArray = {3,4,5,6,7,8}; + //int[] a3 = au.merge(a1, a2); + int[] a3 = au.merge2(a1, a2); assertResult(assertArray,a3); } From 69d3e2b1d6defe62de1fef6b7c16aa7b549bdb3c Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Wed, 1 Mar 2017 21:42:37 +0800 Subject: [PATCH 18/28] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coding/basic/ArrayList.java | 196 +++++++++--------- 1 file changed, 99 insertions(+), 97 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index 220e00459c..4c803377db 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -1,98 +1,100 @@ -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - /*扩容因子*/ - private static final int GENE = 10; - - private Object[] elementData = new Object[10]; - /*扩容引用*/ - private Object[] newElementData; - - public void add(Object o){ - grow(); - elementData[size] = o; - size ++; - } - public void add(int index, Object o){ - - if(index>size){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - grow(); - if(indexsize){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - return elementData[index]; - } - - public Object remove(int index){ - - Object o = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); - size --; - return o; - } - - public int size(){ - return size; - } - - public Object[] toArray(){ - Object[] objArr = new Object[size]; - System.arraycopy(elementData, 0, objArr, 0, size); - return objArr; - } - - /** - * 扩容,扩容因子为10 - */ - private void grow(){ - - if(size>=elementData.length){//长度不够需要扩容 - newElementData = new Object[size+GENE]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - elementData = newElementData; - } - } - - - public Iterator iterator(){ - - return new Itr(); - } - - private class Itr implements Iterator{ - - int cursor; - @Override - public boolean hasNext() { - return cursor != ArrayList.this.size; - } - - @Override - public Object next() { - - int i = this.cursor; - if (i >= ArrayList.this.size){ - throw new NoSuchElementException(); - } - this.cursor = (i + 1); - return ArrayList.this.elementData[i]; - } - - } +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + /*扩容因子*/ + private static final int GENE = 10; + + private Object[] elementData = new Object[10]; + /*扩容引用*/ + private Object[] newElementData; + + public void add(Object o){ + grow(); + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + grow(); + if(indexsize){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + return elementData[index]; + } + + public Object remove(int index){ + + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + size --; + return o; + } + + public int size(){ + return size; + } + + public Object[] toArray(){ + Object[] objArr = new Object[size]; + System.arraycopy(elementData, 0, objArr, 0, size); + return objArr; + } + + /** + * 扩容,扩容因子为10 + */ + private void grow(){ + + if(size>=elementData.length){//长度不够需要扩容 + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } + } + + + public Iterator iterator(){ + + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= ArrayList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return ArrayList.this.elementData[i]; + } + + } } \ No newline at end of file From e746e1b588a9aed822919698d1afb91fce273d37 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Wed, 1 Mar 2017 21:46:11 +0800 Subject: [PATCH 19/28] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2258659044/zj-2017/src/com/coding/basic/ArrayList.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index 4c803377db..f1fbf7e8b1 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -19,7 +19,7 @@ public void add(Object o){ } public void add(int index, Object o){ - if(index>size){ + if(index<0||index>size){ throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); } grow(); @@ -35,7 +35,7 @@ public void add(int index, Object o){ public Object get(int index){ - if(index>size){ + if(index<0||index>size){ throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); } return elementData[index]; From e29c4bb9a90ae677dd86eccfb003b82b3f9689e1 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Thu, 2 Mar 2017 18:03:45 +0800 Subject: [PATCH 20/28] cafa --- .../src/com/coderising/litestruts/Struts.java | 104 ++++++++++++------ 1 file changed, 72 insertions(+), 32 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index 82e61dbcf2..699c2e3a31 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -4,7 +4,9 @@ import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; @@ -18,28 +20,36 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import com.coderising.litestruts.bean.Action; +import com.coderising.litestruts.bean.Result; + public class Struts { private final static String ACTION = "action"; private final static String RESULT = "result"; private final static String NAME = "name"; private final static String CLASS = "class"; + private final static String TYPE = "type"; private final static String EXECUTE = "execute"; + private final static List actions; + static{ + String path = "src/com/coderising/litestruts/struts.xml"; + actions = readStrutsXml(path); + } + public static View runAction(String actionName, Map parameters) { - View view = new View(); Map viewMap = new HashMap(); - - String path = "src/com/coderising/litestruts/struts.xml"; - Map xmlMap = readStrutsXml(path, actionName); + + Action actionBean= getCurrentAction(actionName); try { - String calssPath = xmlMap.get(CLASS); + String calssPath = actionBean.getClaz(); Class clazz = Class.forName(calssPath); - Object action = clazz.newInstance(); + Object instance = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); String fieldName; String methodName; @@ -49,25 +59,31 @@ public static View runAction(String actionName, methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Method method = clazz.getMethod(methodName, fields[i].getType()); - method.invoke(action, parameters.get(fieldName)); + method.invoke(instance, parameters.get(fieldName)); } } Method successMethos = clazz.getMethod(EXECUTE); - Object result = successMethos.invoke(action); + Object result = successMethos.invoke(instance); for (int i = 0; i < fields.length; i++) { fieldName = fields[i].getName(); methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Method method = clazz.getMethod(methodName); - Object value = method.invoke(action); + Object value = method.invoke(instance); viewMap.put(fieldName, value); } view.setParameters(viewMap); - String jsp = xmlMap.get(result.toString()); + List results = actionBean.getResults(); + for (int i = 0; i < results.size(); i++) { + if(results.get(i).getName()){ + + } + } + String jsp = actionBean.get(result.toString()); view.setJsp(jsp); } catch (Exception e) { @@ -85,11 +101,15 @@ public static View runAction(String actionName, * @param actionName * @return */ - private static Map readStrutsXml(String filePath, - String actionName) { + + private static List readStrutsXml(String filePath) { File xmlFile = new File(filePath); - Map xmlMap = new HashMap(); + Action action = null; + Result result = null; + List results = null; + List actions = new ArrayList(); + try { DocumentBuilder documentBuilder = DocumentBuilderFactory .newInstance().newDocumentBuilder(); @@ -100,31 +120,41 @@ private static Map readStrutsXml(String filePath, for (int i = 0; i < actionNodes.getLength(); i++) { Node actionNode = actionNodes.item(i); if (ACTION.equals(actionNode.getNodeName())) { + action = new Action(); + // 解析action标签 NamedNodeMap actionNodeMap = actionNode.getAttributes(); - String atName = actionNodeMap.getNamedItem(NAME) + String actionName = actionNodeMap.getNamedItem(NAME) .getNodeValue(); - if (atName.equals(actionName)) { - String classPath = actionNodeMap.getNamedItem(CLASS) - .getNodeValue(); - xmlMap.put(CLASS, classPath); - NodeList resultNodes = actionNode.getChildNodes(); - for (int j = 0; j < resultNodes.getLength(); j++) { - Node resultNode = resultNodes.item(j); - if (RESULT.equals(resultNode.getNodeName())) { - NamedNodeMap resultNodeMap = resultNode - .getAttributes(); - String jspName = resultNodeMap.getNamedItem( - NAME).getNodeValue(); - String jspPath = resultNode.getTextContent(); - xmlMap.put(jspName, jspPath); - } + String claz = actionNodeMap.getNamedItem(CLASS) + .getNodeValue(); + action.setName(actionName); + action.setClaz(claz); + // 解析result标签 + NodeList resultNodes = actionNode.getChildNodes(); + for (int j = 0; j < resultNodes.getLength(); j++) { + results = new ArrayList(); + Node resultNode = resultNodes.item(j); + if (RESULT.equals(resultNode.getNodeName())) { + result = new Result(); + NamedNodeMap resultNodeMap = resultNode + .getAttributes(); + String resultName = resultNodeMap + .getNamedItem(NAME).getNodeValue(); + String resultType = resultNodeMap + .getNamedItem(TYPE).getNodeValue(); + String jspPath = resultNode.getTextContent(); + result.setName(resultName); + result.setType(resultType); + result.setJspPath(jspPath); + results.add(result); } - } + action.setResults(results); } - } + actions.add(action); + } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { @@ -132,6 +162,16 @@ private static Map readStrutsXml(String filePath, } catch (IOException e) { e.printStackTrace(); } - return xmlMap; + return actions; } + + private static Action getCurrentAction(String actionName){ + + for (int i = 0; i < actions.size(); i++) { + if(actions.get(i).getName().equals(actionName)){ + return actions.get(i); + } + } + return null; + } } \ No newline at end of file From a5722d9fa6bb9b2d7528b339f794b1119410f629 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Thu, 2 Mar 2017 20:52:14 +0800 Subject: [PATCH 21/28] =?UTF-8?q?=E4=BC=98=E5=8C=96struts=E8=AF=BB?= =?UTF-8?q?=E5=8F=96xml=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/litestruts/Struts.java | 72 ++++++++++++------- .../coderising/litestruts/bean/Action.java | 45 ++++++++++++ .../coderising/litestruts/bean/Result.java | 43 +++++++++++ 3 files changed, 134 insertions(+), 26 deletions(-) create mode 100644 group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Action.java create mode 100644 group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Result.java diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index 699c2e3a31..325bb2b1c5 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -32,7 +32,9 @@ public class Struts { private final static String TYPE = "type"; private final static String EXECUTE = "execute"; + //Struts.xml描述的所有action信息 private final static List actions; + //读取Struts.xml获取所有action相关信息 static{ String path = "src/com/coderising/litestruts/struts.xml"; actions = readStrutsXml(path); @@ -43,16 +45,21 @@ public static View runAction(String actionName, View view = new View(); Map viewMap = new HashMap(); - + + //获取当前请求的action信息 Action actionBean= getCurrentAction(actionName); - + if(actionBean == null){ + return view; + } try { - String calssPath = actionBean.getClaz(); + //创建实例获取属性 + String calssPath = actionBean.getClazz(); Class clazz = Class.forName(calssPath); Object instance = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); String fieldName; String methodName; + //调用set方法为属性赋值 for (int i = 0; i < fields.length; i++) { fieldName = fields[i].getName(); if(parameters.containsKey(fieldName)){ @@ -63,9 +70,10 @@ public static View runAction(String actionName, } } + //调用默认execute方法 Method successMethos = clazz.getMethod(EXECUTE); Object result = successMethos.invoke(instance); - + // 调用get方法获取属性值 for (int i = 0; i < fields.length; i++) { fieldName = fields[i].getName(); methodName = "get" + fieldName.substring(0, 1).toUpperCase() @@ -75,16 +83,14 @@ public static View runAction(String actionName, viewMap.put(fieldName, value); } + //封装view对象所需数据 view.setParameters(viewMap); - List results = actionBean.getResults(); for (int i = 0; i < results.size(); i++) { - if(results.get(i).getName()){ - + if(results.get(i).getName().equals(result)){ + view.setJsp(results.get(i).getRedirectUrl()); } } - String jsp = actionBean.get(result.toString()); - view.setJsp(jsp); } catch (Exception e) { e.printStackTrace(); @@ -123,38 +129,33 @@ private static List readStrutsXml(String filePath) { action = new Action(); // 解析action标签 NamedNodeMap actionNodeMap = actionNode.getAttributes(); - String actionName = actionNodeMap.getNamedItem(NAME) - .getNodeValue(); - String claz = actionNodeMap.getNamedItem(CLASS) - .getNodeValue(); + String actionName = getNodePropertyValue(actionNodeMap.getNamedItem(NAME)); + String claz = getNodePropertyValue(actionNodeMap.getNamedItem(CLASS)); action.setName(actionName); - action.setClaz(claz); + action.setClazz(claz); // 解析result标签 NodeList resultNodes = actionNode.getChildNodes(); - for (int j = 0; j < resultNodes.getLength(); j++) { - results = new ArrayList(); + results = new ArrayList(); + for (int j = 0; j < resultNodes.getLength(); j++) { Node resultNode = resultNodes.item(j); if (RESULT.equals(resultNode.getNodeName())) { result = new Result(); - NamedNodeMap resultNodeMap = resultNode - .getAttributes(); - String resultName = resultNodeMap - .getNamedItem(NAME).getNodeValue(); - String resultType = resultNodeMap - .getNamedItem(TYPE).getNodeValue(); + NamedNodeMap resultNodeMap = resultNode.getAttributes(); + String resultName = getNodePropertyValue(resultNodeMap.getNamedItem(NAME)); + String resultType = getNodePropertyValue(resultNodeMap.getNamedItem(TYPE)); String jspPath = resultNode.getTextContent(); result.setName(resultName); result.setType(resultType); - result.setJspPath(jspPath); + result.setRedirectUrl(jspPath); results.add(result); } + } action.setResults(results); + actions.add(action); } } - - actions.add(action); - + } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { @@ -165,6 +166,11 @@ private static List readStrutsXml(String filePath) { return actions; } + /** + * 获取当前action信息 + * @param actionName + * @return + */ private static Action getCurrentAction(String actionName){ for (int i = 0; i < actions.size(); i++) { @@ -174,4 +180,18 @@ private static Action getCurrentAction(String actionName){ } return null; } + + /** + * 获取节点属性值 + * @param node + * @return + */ + private static String getNodePropertyValue(Node node){ + + if(node!=null){ + return node.getNodeValue(); + } + return null; + } + } \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Action.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Action.java new file mode 100644 index 0000000000..1c8c26f6d0 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Action.java @@ -0,0 +1,45 @@ +package com.coderising.litestruts.bean; + +import java.util.List; + +/** + * struts.xml 对应action标签 + * @author zj + */ +public class Action { + + /*名称*/ + private String name; + + /*类全名名称*/ + private String clazz; + + /*result*/ + private List results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } + + +} diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Result.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Result.java new file mode 100644 index 0000000000..a595176c19 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/bean/Result.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts.bean; + +/** + * struts.xml 对应result标签 + * @author zj + */ +public class Result { + + /*名称*/ + private String name; + + /*跳转类型*/ + private String type; + + /*跳转路径*/ + private String redirectUrl; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getRedirectUrl() { + return redirectUrl; + } + + public void setRedirectUrl(String redirectUrl) { + this.redirectUrl = redirectUrl; + } + + +} From 1a328aa2e97fea2367e6c89e7315ffd144b65f6b Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Thu, 2 Mar 2017 21:53:19 +0800 Subject: [PATCH 22/28] 5commit 5 data struct homework --- group12/247565311/week1/LinkedList.java | 9 ++- group12/247565311/week2/ArrayUtil.java | 80 +++++++++------------- group12/247565311/week2/ArrayUtilTest.java | 7 +- 3 files changed, 44 insertions(+), 52 deletions(-) diff --git a/group12/247565311/week1/LinkedList.java b/group12/247565311/week1/LinkedList.java index c3f0ca2eb8..3acf90ac47 100644 --- a/group12/247565311/week1/LinkedList.java +++ b/group12/247565311/week1/LinkedList.java @@ -255,8 +255,13 @@ public Object[] toArray() { @Override public T[] toArray(T[] arg0) { - - return null; + T[]res = (T[]) new Object[size]; + Node n = head; + for(int i=0;i data = new ArrayList(); - int leno = oldArray.length; - for(int i=0;i data = new ArrayList(); - int p1=0,p2=0,len1=array1.length,len2=array2.length; - int val1=0,val2=0; - if(len1 == 0) return array2; - if(len2 == 0) return array1; - while(p1=len1){ - data.add(array2[p2]); + temparray[temp] = array2[p2]; p2 += 1; continue; } if(p2>=len2){ - data.add(array1[p1]); + temparray[temp] = array1[p1]; p1 += 1; continue; } - if(p1val2){ - data.add(val2); + if(array1[p1] > array2[p2]){ + temparray[temp] = array2[p2]; p2 += 1; - }else if(val1 li = null,resli = new ArrayList(); for(int i=6;i Date: Fri, 3 Mar 2017 09:33:23 +0800 Subject: [PATCH 23/28] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2258659044/zj-2017/src/com/coderising/litestruts/Struts.java | 1 + 1 file changed, 1 insertion(+) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index 325bb2b1c5..d7196e4ce5 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -89,6 +89,7 @@ public static View runAction(String actionName, for (int i = 0; i < results.size(); i++) { if(results.get(i).getName().equals(result)){ view.setJsp(results.get(i).getRedirectUrl()); + break; } } From 30451976494cc5513a923ddacda73f34e1f0820c Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Fri, 3 Mar 2017 11:57:30 +0800 Subject: [PATCH 24/28] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zj-2017/src/com/coderising/litestruts/Struts.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java index d7196e4ce5..57f4f6d55e 100644 --- a/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java +++ b/group12/2258659044/zj-2017/src/com/coderising/litestruts/Struts.java @@ -66,7 +66,9 @@ public static View runAction(String actionName, methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Method method = clazz.getMethod(methodName, fields[i].getType()); - method.invoke(instance, parameters.get(fieldName)); + if(method != null){ + method.invoke(instance, parameters.get(fieldName)); + } } } @@ -79,9 +81,10 @@ public static View runAction(String actionName, methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Method method = clazz.getMethod(methodName); - Object value = method.invoke(instance); - viewMap.put(fieldName, value); - + if(method != null){ + Object value = method.invoke(instance); + viewMap.put(fieldName, value); + } } //封装view对象所需数据 view.setParameters(viewMap); From 23e6c4261e1aafcb614dcd52c203f8383b8c4b2e Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Fri, 3 Mar 2017 12:55:44 +0800 Subject: [PATCH 25/28] =?UTF-8?q?=E4=BA=A4=E7=AC=AC=E4=BA=8C=E5=91=A8?= =?UTF-8?q?=E7=9A=84=E4=BD=9C=E4=B8=9A=EF=BC=9A=E6=A8=A1=E6=8B=9Fstrut?= =?UTF-8?q?=E8=AF=BB=E5=8F=96xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group12/247565311/week2/LoginAction.java | 30 +++++++ group12/247565311/week2/Struts.java | 105 +++++++++++++++++++++++ group12/247565311/week2/StrutsTest.java | 50 +++++++++++ group12/247565311/week2/View.java | 21 +++++ group12/247565311/week2/struts.xml | 11 +++ 5 files changed, 217 insertions(+) create mode 100644 group12/247565311/week2/LoginAction.java create mode 100644 group12/247565311/week2/Struts.java create mode 100644 group12/247565311/week2/StrutsTest.java create mode 100644 group12/247565311/week2/View.java create mode 100644 group12/247565311/week2/struts.xml diff --git a/group12/247565311/week2/LoginAction.java b/group12/247565311/week2/LoginAction.java new file mode 100644 index 0000000000..b0ae6ebf5b --- /dev/null +++ b/group12/247565311/week2/LoginAction.java @@ -0,0 +1,30 @@ +package week2; + +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/group12/247565311/week2/Struts.java b/group12/247565311/week2/Struts.java new file mode 100644 index 0000000000..b8a9f8a721 --- /dev/null +++ b/group12/247565311/week2/Struts.java @@ -0,0 +1,105 @@ +package week2; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + + + + + + + +import org.dom4j.*; +import org.dom4j.io.SAXReader; + +public class Struts { + @SuppressWarnings("unchecked") + public static View runAction(String actionName,Mapparameters){ + if(actionName == null || parameters == null) return null; + List actions = null; + try { + File xmlfile = new File("D:\\5Java\\coding2017\\group12\\247565311\\week2\\struts.xml"); + Document doc = new SAXReader().read(xmlfile); + Element root = doc.getRootElement(); + actions = root.elements(); + } catch (DocumentException e) { + e.printStackTrace(); + } + + String className=""; + Element curActNode = null; + for(int i=0;i attrs = actions.get(i).attributes(); + for(int j=0;j class1 = null; + try { + class1 = Class.forName(className); + class1Instance = class1.newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + for(String key : parameters.keySet()){ + String methodName = "set"+(new StringBuilder()).append(Character.toUpperCase(key.charAt(0))).append(key.substring(1)).toString(); + Object methodPara=parameters.get(key); + try { + Method method =class1.getMethod(methodName, String.class); + method.invoke(class1Instance, methodPara); + } catch (Exception e) { + e.printStackTrace(); + } + } + Object exeResult = null; + try { + Method method =class1.getMethod("execute"); + exeResult = method.invoke(class1Instance); + } catch (Exception e) { + e.printStackTrace(); + } + + String jsp = null; + List results = curActNode.elements(); + for(int i=0;i attrs = results.get(i).attributes(); + for(int j=0;j para = new HashMap(); + view.setParameters(para); + + Field [] fields = class1.getDeclaredFields(); + for(int i=0;i 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"); //�����Ԥ��IJ�һ�� + + 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/group12/247565311/week2/View.java b/group12/247565311/week2/View.java new file mode 100644 index 0000000000..ccf254d458 --- /dev/null +++ b/group12/247565311/week2/View.java @@ -0,0 +1,21 @@ +package week2; +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/group12/247565311/week2/struts.xml b/group12/247565311/week2/struts.xml new file mode 100644 index 0000000000..234232d0b6 --- /dev/null +++ b/group12/247565311/week2/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From 29954837d7626621b786df0c1c8d63b39e417e12 Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Fri, 3 Mar 2017 12:57:17 +0800 Subject: [PATCH 26/28] some changes to homework1 not important --- group12/247565311/week1/ArrayListTest.java | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 group12/247565311/week1/ArrayListTest.java diff --git a/group12/247565311/week1/ArrayListTest.java b/group12/247565311/week1/ArrayListTest.java new file mode 100644 index 0000000000..846dd152f5 --- /dev/null +++ b/group12/247565311/week1/ArrayListTest.java @@ -0,0 +1,113 @@ +package week1; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + // ִ�в��Է���Ǯִ�д˷��� + } + + @After + public void tearDown() throws Exception { + // ��������ִ������ִ�д˷��� + } + + @Test + public void testArrayList() { + fail("��δʵ��"); + } + + @Test + public void testAddE() { + fail("��δʵ��"); + } + + @Test + public void testAddIntE() { + fail("��δʵ��"); + } + + @Test + public void testAddAllCollectionOfQextendsE() { + fail("��δʵ��"); + } + + @Test + public void testAddAllIntCollectionOfQextendsE() { + fail("��δʵ��"); + } + + @Test + public void testClear() { + fail("��δʵ��"); + } + + @Test + public void testContains() { + fail("��δʵ��"); + } + + @Test + public void testContainsAll() { + fail("��δʵ��"); + } + + @Test + public void testGet() { + fail("��δʵ��"); + } + + @Test + public void testIndexOf() { + fail("��δʵ��"); + } + + @Test + public void testIsEmpty() { + fail("��δʵ��"); + } + + @Test + public void testRemoveObject() { + fail("��δʵ��"); + } + + @Test + public void testRemoveInt() { + fail("��δʵ��"); + } + + @Test + public void testRemoveAll() { + fail("��δʵ��"); + } + + @Test + public void testRetainAll() { + fail("��δʵ��"); + } + + @Test + public void testSet() { + fail("��δʵ��"); + } + + @Test + public void testSize() { + fail("��δʵ��"); + } + + @Test + public void testToArray() { + fail("��δʵ��"); + } + +} From 66c131f662cf93b807793a30d8360b19d65684d6 Mon Sep 17 00:00:00 2001 From: guodongym Date: Fri, 3 Mar 2017 21:58:14 +0800 Subject: [PATCH 27/28] del --- group12/377401843/learning_1/.gitignore | 3 - .../com/guodong/datastructure/ArrayList.java | 171 ----------- .../guodong/datastructure/BinaryTreeNode.java | 58 ---- .../com/guodong/datastructure/Iterator.java | 7 - .../com/guodong/datastructure/LinkedList.java | 285 ------------------ .../src/com/guodong/datastructure/List.java | 14 - .../src/com/guodong/datastructure/Queue.java | 21 -- .../src/com/guodong/datastructure/Stack.java | 25 -- .../datastructure/test/ArrayListTest.java | 135 --------- .../test/BinaryTreeNodeTest.java | 37 --- .../datastructure/test/LinkedListTest.java | 143 --------- .../guodong/datastructure/test/QueueTest.java | 62 ---- .../guodong/datastructure/test/StackTest.java | 46 --- 13 files changed, 1007 deletions(-) delete mode 100644 group12/377401843/learning_1/.gitignore delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/List.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java delete mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java diff --git a/group12/377401843/learning_1/.gitignore b/group12/377401843/learning_1/.gitignore deleted file mode 100644 index b12088665a..0000000000 --- a/group12/377401843/learning_1/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/bin/ -/.project -/.classpath diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java deleted file mode 100644 index 880af45da8..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.guodong.datastructure; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - /** - * 按下标顺序新增元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - ensureCapacityInternal(size + 1); - elementData[size] = o; - size++; - } - - /** - * 在指定下标位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkRangeForAdd(index); - - ensureCapacityInternal(size + 1); - - System.arraycopy(elementData, index, elementData, index + 1, size - index); - - elementData[index] = o; - size++; - } - - /** - * 根据下标获取列表数据 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkRangeForGetOrRemove(index); - - return elementData[index]; - } - - /** - * 根据下标移除元素,并返回移除的元素值 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkRangeForGetOrRemove(index); - - Object oldValue = elementData[index]; - - System.arraycopy(elementData, index + 1, elementData, index, size - index -1); - - size--; - elementData[size] = null; - - return oldValue; - } - - /** - * 获得列表长度 - * - * @Method size - * @return - * @see com.guodong.datastructure.List#size() - */ - public int size() { - return size; - } - - /** - * 获取ArrayList的迭代器 - * - * @MethodName iterator - * @author zhaogd - * @date 2017年2月21日 下午8:19:28 - * @return - */ - public Iterator iterator() { - return new ArrayListIterator(); - } - - /** - * 确保数组容量足够,如果不够则扩充数组 - * - * @MethodName ensureCapacityInternal - * @author zhaogd - * @date 2017年2月21日 下午5:06:46 - * @param minCapacity - */ - private void ensureCapacityInternal(int minCapacity) { - if(minCapacity > elementData.length){ - grow(minCapacity); - } - } - - /** - * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 - * - * @MethodName grow - * @author zhaogd - * @date 2017年2月21日 下午5:20:55 - * @param minCapacity - */ - private void grow(int minCapacity) { - minCapacity = elementData.length + elementData.length / 2; - elementData = Arrays.copyOf(elementData, minCapacity); - } - - /** - * 检查Add方法的下标范围是否合法 - * - * @MethodName checkRangeForAdd - * @author zhaogd - * @date 2017年2月21日 下午7:32:55 - * @param index - */ - private void checkRangeForAdd(int index) { - if(index > size || index < 0){ - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查Get或者Remove方法的下标范围是否合法 - * - * @MethodName checkRangeForGetOrRemove - * @author zhaogd - * @date 2017年2月21日 下午7:33:21 - * @param index - */ - private void checkRangeForGetOrRemove(int index) { - if(index >= size || index < 0){ - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - - private class ArrayListIterator implements Iterator{ - - private int lastIndex = 0; - - @Override - public boolean hasNext() { - return lastIndex < size; - } - - @Override - public Object next() { - return elementData[lastIndex++]; - } - } -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java deleted file mode 100644 index 2ced039d20..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.guodong.datastructure; - -public class BinaryTreeNode { - - private int data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(int data) { - this.data = data; - } - - public int getData() { - return data; - } - - public void setData(int data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(int o) { - - if (o < data) { - if (left != null) { - left.insert(o); - } else { - left = new BinaryTreeNode(o); - return left; - } - } else { - if (right != null) { - right.insert(o); - } else { - right = new BinaryTreeNode(o); - return right; - } - } - - return null; - } - -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java deleted file mode 100644 index 8d486220b0..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.guodong.datastructure; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java deleted file mode 100644 index 976129cc84..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ /dev/null @@ -1,285 +0,0 @@ -package com.guodong.datastructure; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private int size; - - private Node head; - - private Node last; - - /** - * 向 链表尾端插入元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - linkLast(o); - } - - /** - * 向链表指定位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkIndexForAdd(index); - - if (index == size) { - linkLast(o); - } else { - Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 - Node currentNode = getNodeByIndex(index); // 取到当前下标节点 - Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 - - if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 - head = newNode; - } else { - prevNode.next = newNode; - } - size++; - } - } - - /** - * 根据下标获取链表中元素 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkIndexForGet(index); - return getNodeByIndex(index).data; - } - - public Object getLast() { - return last.data; - } - - /** - * 根据下标移除链表元素 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkIndexForGet(index); - - Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 - Node currentNode = null; - if (prevNode == null) { - currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 - head = currentNode.next; - } else { - currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 - prevNode.next = currentNode.next; - } - Node nextNode = currentNode.next; - - if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 - last = prevNode; - } else { - currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 - } - Object data = currentNode.data; - currentNode.data = null; // 清空当前节点的值,等待垃圾回收 - - size--; - - return data; - } - - /** - * 返回List长度 - */ - public int size() { - return size; - } - - /** - * 向列表头部添加元素 - * - * @param o - */ - public void addFirst(Object o) { - Node n = head; - Node newNode = new Node(o, n); - - head = newNode; - if (n == null) { - last = newNode; - } - size++; - } - - /** - * 向列表尾部添加元素 - * - * @param o - */ - public void addLast(Object o) { - linkLast(o); - } - - /** - * 移除链表第一个元素 - * - * @return - */ - public Object removeFirst() { - Node n = head; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node nextNode = n.next; - - n.data = null; - n.next = null; - - head = nextNode; - if (nextNode == null) { - last = null; - } - - size--; - return data; - } - - public Object removeLast() { - Node n = last; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node prevNode = getNodeByIndex(size - 2); - n.data = null; - if (prevNode == null) { - head = null; - } else { - prevNode.next = null; - } - last = prevNode; - - size--; - return data; - } - - /** - * 根据下标获取对应的节点 - * - * @MethodName getNodeByIndex - * @author zhaogd - * @date 2017年2月23日 下午3:32:48 - * @param index - * @return - */ - private Node getNodeByIndex(int index) { - if (index < 0) { - return null; - } - Node n = head; - for (int i = 0; i < index; i++) { - n = n.next; - } - return n; - } - - /** - * 在链表尾端插入节点 - * - * @MethodName linkLast - * @author zhaogd - * @date 2017年2月23日 下午3:14:28 - * @param o - */ - private void linkLast(Object o) { - Node n = last; // 取出原尾端数据 - Node newNode = new Node(o, null); // 创建新节点 - last = newNode; // 把新节点放入链表尾端 - // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 - // 如果不为空,把原尾端节点指向新节点 - if (n == null) { - head = newNode; - } else { - n.next = newNode; - } - - size++; - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForAdd - * @author zhaogd - * @date 2017年2月23日 下午3:05:07 - * @param index - */ - private void checkIndexForAdd(int index) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForGet - * @author zhaogd - * @date 2017年2月23日 下午4:21:35 - * @param index - */ - private void checkIndexForGet(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - public Iterator iterator() { - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { - private Node current; - - private int index; - - @Override - public boolean hasNext() { - return index < size; - } - - @Override - public Object next() { - if (current == null) { - current = getNodeByIndex(index); - } - Object data = current.data; - current = current.next; - index++; - return data; - } - } -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java deleted file mode 100644 index 1a6f12da52..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.guodong.datastructure; - -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/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java deleted file mode 100644 index 6dc85d8ec0..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.guodong.datastructure; - -public class Queue { - private LinkedList element = new LinkedList(); - - public void enQueue(Object o) { - element.addLast(o); - } - - public Object deQueue() { - return element.removeFirst(); - } - - public boolean isEmpty() { - return element.size() == 0; - } - - public int size() { - return element.size(); - } -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java deleted file mode 100644 index c2b5049e73..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.guodong.datastructure; - -public class Stack { - private LinkedList elementData = new LinkedList(); - - public void push(Object o) { - elementData.addLast(o); - } - - public Object pop() { - return elementData.removeLast(); - } - - public Object peek() { - return elementData.getLast(); - } - - public boolean isEmpty() { - return elementData.size() == 0; - } - - public int size() { - return elementData.size(); - } -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java deleted file mode 100644 index f38f58614d..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.ArrayList; -import com.guodong.datastructure.Iterator; - -public class ArrayListTest { - - ArrayList arrayList; - - @Before - public void setUp() throws Exception { - arrayList = new ArrayList(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddObject() { - // 测试新增 - arrayList.add(0); - assertEquals(0, arrayList.get(0)); - assertEquals(1, arrayList.size()); - - // 测试扩充 - for (int i = 1; i < 101; i++) { - arrayList.add(i); - } - assertEquals(101, arrayList.size()); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd1() { - // 测试新增下标异常时,是否可以正确抛出异常 - arrayList.add(-1, 2); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd2() { - // 测试新增下标异常时,是否可以正确抛出异常 - arrayList.add(1, 3); - } - - @Test - public void testAddIntObject() { - // 测试下标新增 - arrayList.add(0, 1); - arrayList.add(1, 2); - arrayList.add(2, 3); - arrayList.add(3, 4); - assertEquals(4, arrayList.size()); - - // 测试中间插入 - arrayList.add(2, 5); - assertEquals(5, arrayList.size()); // 测试插入之后长度 - assertEquals(5, arrayList.get(2)); - assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 - assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 - - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet1() { - // 测试Get时,下标异常,是否可以正确抛出异常 - arrayList.get(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet2() { - // 测试Get时,下标异常,是否可以正确抛出异常 - arrayList.get(0); - } - - @Test - public void testGet() { - arrayList.add(1); - arrayList.add(2); - arrayList.add(3); - assertEquals(1, arrayList.get(0)); - assertEquals(2, arrayList.get(1)); - assertEquals(3, arrayList.get(2)); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove1() { - arrayList.remove(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove2() { - arrayList.remove(0); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove3() { - arrayList.remove(1); - } - - @Test - public void testRemove() { - arrayList.add(1); - arrayList.remove(0); - assertEquals(0, arrayList.size()); - - arrayList.add(1); - arrayList.add(2); - arrayList.remove(0); - assertEquals(1, arrayList.size()); - assertEquals(2, arrayList.get(0)); - } - - @Test - public void testSize() { - arrayList.add(1); - assertEquals(1, arrayList.size()); - } - - @Test - public void testIterator() { - Iterator iterator = arrayList.iterator(); - assertFalse(iterator.hasNext()); - - arrayList.add(1); - assertTrue(iterator.hasNext()); - assertEquals(1, iterator.next()); - assertFalse(iterator.hasNext()); - } -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java deleted file mode 100644 index 537cd5961f..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.BinaryTreeNode; - -public class BinaryTreeNodeTest { - - private BinaryTreeNode binaryTreeNode; - - @Before - public void setUp() throws Exception { - binaryTreeNode = new BinaryTreeNode(50); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testInsert() { - binaryTreeNode.insert(20); - binaryTreeNode.insert(30); - binaryTreeNode.insert(60); - binaryTreeNode.insert(80); - - assertEquals(20, binaryTreeNode.getLeft().getData()); - assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); - assertEquals(60, binaryTreeNode.getRight().getData()); - assertEquals(80, binaryTreeNode.getRight().getRight().getData()); - } - -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java deleted file mode 100644 index 52d42b5aa7..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Iterator; -import com.guodong.datastructure.LinkedList; - -public class LinkedListTest { - - private LinkedList linkedList; - - @Before - public void setUp() throws Exception { - linkedList = new LinkedList(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddObject() { - linkedList.add(1); - assertEquals(1, linkedList.size()); - assertEquals(1, linkedList.get(0)); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd1() { - linkedList.add(-1, 1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd2() { - linkedList.add(1, 1); - } - - @Test - public void testAddIntObject() { - linkedList.add(0, 1); - linkedList.add(1, 2); - assertEquals(1, linkedList.get(0)); - - linkedList.add(1,3); - assertEquals(2, linkedList.get(2)); - assertEquals(3, linkedList.get(1)); - assertEquals(3, linkedList.size()); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet1() { - linkedList.get(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet2() { - linkedList.get(0); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet3() { - linkedList.get(1); - } - - @Test - public void testGet() { - linkedList.add(0, 1); - linkedList.add(1, 2); - assertEquals(2, linkedList.get(1)); - } - - @Test - public void testGetLast() { - linkedList.add(1); - assertEquals(1, linkedList.getLast()); - - linkedList.add(2); - assertEquals(2, linkedList.getLast()); - } - - @Test - public void testRemove() { - linkedList.add(1); - assertEquals(1, linkedList.remove(0)); - assertEquals(0, linkedList.size()); - } - - @Test - public void testSize() { - linkedList.add(1); - linkedList.add(1); - linkedList.add(1); - assertEquals(3, linkedList.size()); - } - - @Test - public void testAddFirst() { - linkedList.addFirst(1); - assertEquals(1, linkedList.get(0)); - - linkedList.addFirst(2); - linkedList.addFirst(3); - assertEquals(3, linkedList.get(0)); - assertEquals(1, linkedList.getLast()); - } - - @Test - public void testAddLast() { - linkedList.addLast(1); - assertEquals(1, linkedList.getLast()); - assertEquals(1, linkedList.get(0)); - } - - @Test - public void testRemoveFirst() { - linkedList.addFirst(1); - assertEquals(1, linkedList.removeFirst()); - assertEquals(0, linkedList.size()); - } - - @Test - public void testRemoveLast() { - linkedList.addLast(2); - assertEquals(2, linkedList.removeLast()); - assertEquals(0, linkedList.size()); - } - - @Test - public void testIterator() { - Iterator iterator = linkedList.iterator(); - assertFalse(iterator.hasNext()); - - linkedList.add(1); - assertTrue(iterator.hasNext()); - assertEquals(1, iterator.next()); - assertFalse(iterator.hasNext()); - } - -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java deleted file mode 100644 index 1773b5027c..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import java.util.NoSuchElementException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Queue; - -public class QueueTest { - - private Queue queue; - - @Before - public void setUp() throws Exception { - queue = new Queue(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEnQueue() { - queue.enQueue(1); - assertFalse(queue.isEmpty()); - } - - @Test(expected = NoSuchElementException.class) - public void testDeQueueExecption() { - queue.deQueue(); - } - - @Test - public void testDeQueue() { - queue.enQueue(1); - assertEquals(1, queue.deQueue()); - assertTrue(queue.isEmpty()); - } - - @Test - public void testIsEmpty() { - queue.enQueue(1); - assertFalse(queue.isEmpty()); - - queue.deQueue(); - assertTrue(queue.isEmpty()); - } - - @Test - public void testSize() { - queue.enQueue(1); - queue.enQueue(1); - queue.enQueue(1); - - assertEquals(3, queue.size()); - } - -} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java deleted file mode 100644 index 74ac8e7cc7..0000000000 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Stack; - -public class StackTest { - - private Stack stack; - - @Before - public void setUp() throws Exception { - stack = new Stack(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPush() { - stack.push(11); - assertEquals(11, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - public void testPop() { - stack.push(11); - assertEquals(11, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - public void testPeek() { - stack.push(11); - assertEquals(11, stack.peek()); - assertFalse(stack.isEmpty()); - assertEquals(1, stack.size()); - } - -} From 9e6f0cb292b6e9d9622df42a1bc74d906a0b362e Mon Sep 17 00:00:00 2001 From: guodongym Date: Fri, 3 Mar 2017 22:07:02 +0800 Subject: [PATCH 28/28] maven --- group12/377401843/learning/.gitignore | 4 + group12/377401843/learning/pom.xml | 71 +++++ .../main/java/com/zhaogd/array/ArrayUtil.java | 114 +++++++ .../java/com/zhaogd/collection/ArrayList.java | 170 +++++++++++ .../com/zhaogd/collection/BinaryTreeNode.java | 58 ++++ .../java/com/zhaogd/collection/Iterator.java | 9 + .../com/zhaogd/collection/LinkedList.java | 285 ++++++++++++++++++ .../main/java/com/zhaogd/collection/List.java | 14 + .../java/com/zhaogd/collection/Queue.java | 21 ++ .../java/com/zhaogd/collection/Stack.java | 25 ++ .../com/zhaogd/litestruts/LoginAction.java | 39 +++ .../java/com/zhaogd/litestruts/Struts.java | 34 +++ .../com/zhaogd/litestruts/StrutsTest.java | 43 +++ .../main/java/com/zhaogd/litestruts/View.java | 23 ++ .../java/com/zhaogd/litestruts/struts.xml | 11 + .../com/zhaogd/collection/ArrayListTest.java | 134 ++++++++ .../zhaogd/collection/BinaryTreeNodeTest.java | 35 +++ .../com/zhaogd/collection/LinkedListTest.java | 142 +++++++++ .../java/com/zhaogd/collection/QueueTest.java | 62 ++++ .../java/com/zhaogd/collection/StackTest.java | 46 +++ 20 files changed, 1340 insertions(+) create mode 100644 group12/377401843/learning/.gitignore create mode 100644 group12/377401843/learning/pom.xml create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayUtil.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/BinaryTreeNode.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/Iterator.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/List.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/litestruts/LoginAction.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/litestruts/Struts.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/litestruts/StrutsTest.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/litestruts/View.java create mode 100644 group12/377401843/learning/src/main/java/com/zhaogd/litestruts/struts.xml create mode 100644 group12/377401843/learning/src/test/java/com/zhaogd/collection/ArrayListTest.java create mode 100644 group12/377401843/learning/src/test/java/com/zhaogd/collection/BinaryTreeNodeTest.java create mode 100644 group12/377401843/learning/src/test/java/com/zhaogd/collection/LinkedListTest.java create mode 100644 group12/377401843/learning/src/test/java/com/zhaogd/collection/QueueTest.java create mode 100644 group12/377401843/learning/src/test/java/com/zhaogd/collection/StackTest.java diff --git a/group12/377401843/learning/.gitignore b/group12/377401843/learning/.gitignore new file mode 100644 index 0000000000..8bd3a05882 --- /dev/null +++ b/group12/377401843/learning/.gitignore @@ -0,0 +1,4 @@ +/target/ +/.settings/ +/.classpath +/.project diff --git a/group12/377401843/learning/pom.xml b/group12/377401843/learning/pom.xml new file mode 100644 index 0000000000..1d11205656 --- /dev/null +++ b/group12/377401843/learning/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + com.zhaogd.projects + learning + 1.0.0 + jar + + + + junit + junit + 4.12 + + + + + + + ${project.basedir}/src/main/resources + true + + + ${project.basedir}/src/main/java + + **/*.java + + + + + + + ${project.basedir}/src/test/java + + **/*.java + + + + ${project.basedir}/src/test/resources + + + ${project.basedir}/src/main/java + + **/*.java + + + + ${project.basedir}/src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + 1.8 + UTF-8 + true + 128m + 512m + + + + + diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayUtil.java b/group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayUtil.java new file mode 100644 index 0000000000..f62f0eceef --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/array/ArrayUtil.java @@ -0,0 +1,114 @@ +package com.zhaogd.array; + +import java.util.ArrayList; +import java.util.Arrays; + + +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 static void reverseArray(int[] origin) { + for (int i = 0; i < origin.length / 2; i++) { + int tmp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tmp; + } + System.out.println(Arrays.toString(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 Integer[] removeZero(int[] oldArray) { + ArrayList arrayList = new ArrayList(); + for (int i : oldArray) { + if (i != 0) { + arrayList.add(i); + } + } + return arrayList.toArray(new Integer[arrayList.size()]); + } + + /** + * 给定两个已经排序好的整形数组, 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[] array3 = new int[array1.length + array2.length]; + + System.arraycopy(array1, 0, array3, 0, array1.length); + System.arraycopy(array2, 0, array3, array1.length, array2.length); + + Arrays.sort(array3); + return array3; + } + + /** + * 把一个已经存满数据的数组 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/group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java new file mode 100644 index 0000000000..d3afc5e01a --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/ArrayList.java @@ -0,0 +1,170 @@ +package com.zhaogd.collection; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * 按下标顺序新增元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size] = o; + size++; + } + + /** + * 在指定下标位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkRangeForAdd(index); + + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + /** + * 根据下标获取列表数据 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkRangeForGetOrRemove(index); + + return elementData[index]; + } + + /** + * 根据下标移除元素,并返回移除的元素值 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkRangeForGetOrRemove(index); + + Object oldValue = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + + size--; + elementData[size] = null; + + return oldValue; + } + + /** + * 获得列表长度 + * + * @Method size + * @return + * @see com.guodong.datastructure.List#size() + */ + public int size() { + return size; + } + + /** + * 获取ArrayList的迭代器 + * + * @MethodName iterator + * @author zhaogd + * @date 2017年2月21日 下午8:19:28 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + /** + * 确保数组容量足够,如果不够则扩充数组 + * + * @MethodName ensureCapacityInternal + * @author zhaogd + * @date 2017年2月21日 下午5:06:46 + * @param minCapacity + */ + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity > elementData.length) { + grow(minCapacity); + } + } + + /** + * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 + * + * @MethodName grow + * @author zhaogd + * @date 2017年2月21日 下午5:20:55 + * @param minCapacity + */ + private void grow(int minCapacity) { + minCapacity = elementData.length + elementData.length / 2; + elementData = Arrays.copyOf(elementData, minCapacity); + } + + /** + * 检查Add方法的下标范围是否合法 + * + * @MethodName checkRangeForAdd + * @author zhaogd + * @date 2017年2月21日 下午7:32:55 + * @param index + */ + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查Get或者Remove方法的下标范围是否合法 + * + * @MethodName checkRangeForGetOrRemove + * @author zhaogd + * @date 2017年2月21日 下午7:33:21 + * @param index + */ + private void checkRangeForGetOrRemove(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private class ArrayListIterator implements Iterator { + + private int lastIndex = 0; + + @Override + public boolean hasNext() { + return lastIndex < size; + } + + @Override + public Object next() { + return elementData[lastIndex++]; + } + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/BinaryTreeNode.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/BinaryTreeNode.java new file mode 100644 index 0000000000..ee256391e7 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/BinaryTreeNode.java @@ -0,0 +1,58 @@ +package com.zhaogd.collection; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(int o) { + + if (o < data) { + if (left != null) { + left.insert(o); + } else { + left = new BinaryTreeNode(o); + return left; + } + } else { + if (right != null) { + right.insert(o); + } else { + right = new BinaryTreeNode(o); + return right; + } + } + + return null; + } + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/Iterator.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Iterator.java new file mode 100644 index 0000000000..0d9e6013d6 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Iterator.java @@ -0,0 +1,9 @@ +package com.zhaogd.collection; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); + +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java new file mode 100644 index 0000000000..1cd0c2e2b3 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/LinkedList.java @@ -0,0 +1,285 @@ +package com.zhaogd.collection; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size; + + private Node head; + + private Node last; + + /** + * 向 链表尾端插入元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + linkLast(o); + } + + /** + * 向链表指定位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkIndexForAdd(index); + + if (index == size) { + linkLast(o); + } else { + Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 + + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 + head = newNode; + } else { + prevNode.next = newNode; + } + size++; + } + } + + /** + * 根据下标获取链表中元素 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkIndexForGet(index); + return getNodeByIndex(index).data; + } + + public Object getLast() { + return last.data; + } + + /** + * 根据下标移除链表元素 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkIndexForGet(index); + + Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 + Node currentNode = null; + if (prevNode == null) { + currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 + head = currentNode.next; + } else { + currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 + prevNode.next = currentNode.next; + } + Node nextNode = currentNode.next; + + if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 + last = prevNode; + } else { + currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 + } + Object data = currentNode.data; + currentNode.data = null; // 清空当前节点的值,等待垃圾回收 + + size--; + + return data; + } + + /** + * 返回List长度 + */ + public int size() { + return size; + } + + /** + * 向列表头部添加元素 + * + * @param o + */ + public void addFirst(Object o) { + Node n = head; + Node newNode = new Node(o, n); + + head = newNode; + if (n == null) { + last = newNode; + } + size++; + } + + /** + * 向列表尾部添加元素 + * + * @param o + */ + public void addLast(Object o) { + linkLast(o); + } + + /** + * 移除链表第一个元素 + * + * @return + */ + public Object removeFirst() { + Node n = head; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node nextNode = n.next; + + n.data = null; + n.next = null; + + head = nextNode; + if (nextNode == null) { + last = null; + } + + size--; + return data; + } + + public Object removeLast() { + Node n = last; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node prevNode = getNodeByIndex(size - 2); + n.data = null; + if (prevNode == null) { + head = null; + } else { + prevNode.next = null; + } + last = prevNode; + + size--; + return data; + } + + /** + * 根据下标获取对应的节点 + * + * @MethodName getNodeByIndex + * @author zhaogd + * @date 2017年2月23日 下午3:32:48 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + if (index < 0) { + return null; + } + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + /** + * 在链表尾端插入节点 + * + * @MethodName linkLast + * @author zhaogd + * @date 2017年2月23日 下午3:14:28 + * @param o + */ + private void linkLast(Object o) { + Node n = last; // 取出原尾端数据 + Node newNode = new Node(o, null); // 创建新节点 + last = newNode; // 把新节点放入链表尾端 + // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 + // 如果不为空,把原尾端节点指向新节点 + if (n == null) { + head = newNode; + } else { + n.next = newNode; + } + + size++; + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForAdd + * @author zhaogd + * @date 2017年2月23日 下午3:05:07 + * @param index + */ + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForGet + * @author zhaogd + * @date 2017年2月23日 下午4:21:35 + * @param index + */ + private void checkIndexForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current; + + private int index; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (current == null) { + current = getNodeByIndex(index); + } + Object data = current.data; + current = current.next; + index++; + return data; + } + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/List.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/List.java new file mode 100644 index 0000000000..fb2f1bb78e --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/List.java @@ -0,0 +1,14 @@ +package com.zhaogd.collection; + +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/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java new file mode 100644 index 0000000000..d4a0647ab6 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Queue.java @@ -0,0 +1,21 @@ +package com.zhaogd.collection; + +public class Queue { + private LinkedList element = new LinkedList(); + + public void enQueue(Object o) { + element.addLast(o); + } + + public Object deQueue() { + return element.removeFirst(); + } + + public boolean isEmpty() { + return element.size() == 0; + } + + public int size() { + return element.size(); + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java new file mode 100644 index 0000000000..afb01f5f92 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/collection/Stack.java @@ -0,0 +1,25 @@ +package com.zhaogd.collection; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public void push(Object o) { + elementData.addLast(o); + } + + public Object pop() { + return elementData.removeLast(); + } + + public Object peek() { + return elementData.getLast(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/LoginAction.java b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/LoginAction.java new file mode 100644 index 0000000000..17edaa63b1 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.zhaogd.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/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/Struts.java b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/Struts.java new file mode 100644 index 0000000000..fa2f5eea7c --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.zhaogd.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/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/StrutsTest.java b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/StrutsTest.java new file mode 100644 index 0000000000..41e8313502 --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.zhaogd.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/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/View.java b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/View.java new file mode 100644 index 0000000000..a790924f7d --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/View.java @@ -0,0 +1,23 @@ +package com.zhaogd.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/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/struts.xml b/group12/377401843/learning/src/main/java/com/zhaogd/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group12/377401843/learning/src/main/java/com/zhaogd/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/group12/377401843/learning/src/test/java/com/zhaogd/collection/ArrayListTest.java b/group12/377401843/learning/src/test/java/com/zhaogd/collection/ArrayListTest.java new file mode 100644 index 0000000000..c54e99b4db --- /dev/null +++ b/group12/377401843/learning/src/test/java/com/zhaogd/collection/ArrayListTest.java @@ -0,0 +1,134 @@ +package com.zhaogd.collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + ArrayList arrayList; + + @Before + public void setUp() throws Exception { + arrayList = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + // 测试新增 + arrayList.add(0); + assertEquals(0, arrayList.get(0)); + assertEquals(1, arrayList.size()); + + // 测试扩充 + for (int i = 1; i < 101; i++) { + arrayList.add(i); + } + assertEquals(101, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(-1, 2); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(1, 3); + } + + @Test + public void testAddIntObject() { + // 测试下标新增 + arrayList.add(0, 1); + arrayList.add(1, 2); + arrayList.add(2, 3); + arrayList.add(3, 4); + assertEquals(4, arrayList.size()); + + // 测试中间插入 + arrayList.add(2, 5); + assertEquals(5, arrayList.size()); // 测试插入之后长度 + assertEquals(5, arrayList.get(2)); + assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 + assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 + + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(0); + } + + @Test + public void testGet() { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + assertEquals(1, arrayList.get(0)); + assertEquals(2, arrayList.get(1)); + assertEquals(3, arrayList.get(2)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove1() { + arrayList.remove(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove2() { + arrayList.remove(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove3() { + arrayList.remove(1); + } + + @Test + public void testRemove() { + arrayList.add(1); + arrayList.remove(0); + assertEquals(0, arrayList.size()); + + arrayList.add(1); + arrayList.add(2); + arrayList.remove(0); + assertEquals(1, arrayList.size()); + assertEquals(2, arrayList.get(0)); + } + + @Test + public void testSize() { + arrayList.add(1); + assertEquals(1, arrayList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + assertFalse(iterator.hasNext()); + + arrayList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } +} diff --git a/group12/377401843/learning/src/test/java/com/zhaogd/collection/BinaryTreeNodeTest.java b/group12/377401843/learning/src/test/java/com/zhaogd/collection/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..797d457c21 --- /dev/null +++ b/group12/377401843/learning/src/test/java/com/zhaogd/collection/BinaryTreeNodeTest.java @@ -0,0 +1,35 @@ +package com.zhaogd.collection; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUp() throws Exception { + binaryTreeNode = new BinaryTreeNode(50); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInsert() { + binaryTreeNode.insert(20); + binaryTreeNode.insert(30); + binaryTreeNode.insert(60); + binaryTreeNode.insert(80); + + assertEquals(20, binaryTreeNode.getLeft().getData()); + assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); + assertEquals(60, binaryTreeNode.getRight().getData()); + assertEquals(80, binaryTreeNode.getRight().getRight().getData()); + } + +} diff --git a/group12/377401843/learning/src/test/java/com/zhaogd/collection/LinkedListTest.java b/group12/377401843/learning/src/test/java/com/zhaogd/collection/LinkedListTest.java new file mode 100644 index 0000000000..32cac4bc6d --- /dev/null +++ b/group12/377401843/learning/src/test/java/com/zhaogd/collection/LinkedListTest.java @@ -0,0 +1,142 @@ +package com.zhaogd.collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + private LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add(1); + assertEquals(1, linkedList.size()); + assertEquals(1, linkedList.get(0)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + linkedList.add(-1, 1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + linkedList.add(1, 1); + } + + @Test + public void testAddIntObject() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(1, linkedList.get(0)); + + linkedList.add(1, 3); + assertEquals(2, linkedList.get(2)); + assertEquals(3, linkedList.get(1)); + assertEquals(3, linkedList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + linkedList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + linkedList.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet3() { + linkedList.get(1); + } + + @Test + public void testGet() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(2, linkedList.get(1)); + } + + @Test + public void testGetLast() { + linkedList.add(1); + assertEquals(1, linkedList.getLast()); + + linkedList.add(2); + assertEquals(2, linkedList.getLast()); + } + + @Test + public void testRemove() { + linkedList.add(1); + assertEquals(1, linkedList.remove(0)); + assertEquals(0, linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add(1); + linkedList.add(1); + linkedList.add(1); + assertEquals(3, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.get(0)); + + linkedList.addFirst(2); + linkedList.addFirst(3); + assertEquals(3, linkedList.get(0)); + assertEquals(1, linkedList.getLast()); + } + + @Test + public void testAddLast() { + linkedList.addLast(1); + assertEquals(1, linkedList.getLast()); + assertEquals(1, linkedList.get(0)); + } + + @Test + public void testRemoveFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testRemoveLast() { + linkedList.addLast(2); + assertEquals(2, linkedList.removeLast()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + assertFalse(iterator.hasNext()); + + linkedList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } + +} diff --git a/group12/377401843/learning/src/test/java/com/zhaogd/collection/QueueTest.java b/group12/377401843/learning/src/test/java/com/zhaogd/collection/QueueTest.java new file mode 100644 index 0000000000..29c5e15752 --- /dev/null +++ b/group12/377401843/learning/src/test/java/com/zhaogd/collection/QueueTest.java @@ -0,0 +1,62 @@ +package com.zhaogd.collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + } + + @Test(expected = NoSuchElementException.class) + public void testDeQueueExecption() { + queue.deQueue(); + } + + @Test + public void testDeQueue() { + queue.enQueue(1); + assertEquals(1, queue.deQueue()); + assertTrue(queue.isEmpty()); + } + + @Test + public void testIsEmpty() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + + queue.deQueue(); + assertTrue(queue.isEmpty()); + } + + @Test + public void testSize() { + queue.enQueue(1); + queue.enQueue(1); + queue.enQueue(1); + + assertEquals(3, queue.size()); + } + +} diff --git a/group12/377401843/learning/src/test/java/com/zhaogd/collection/StackTest.java b/group12/377401843/learning/src/test/java/com/zhaogd/collection/StackTest.java new file mode 100644 index 0000000000..510e4c45f6 --- /dev/null +++ b/group12/377401843/learning/src/test/java/com/zhaogd/collection/StackTest.java @@ -0,0 +1,46 @@ +package com.zhaogd.collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPush() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPop() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPeek() { + stack.push(11); + assertEquals(11, stack.peek()); + assertFalse(stack.isEmpty()); + assertEquals(1, stack.size()); + } + +}