From 17f5a61dd4bd4cd8bf948fd36fbede1d8cb3dd94 Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:04:51 +0800 Subject: [PATCH 1/6] finish first week and second week homework --- group24/1525619747/.gitignore | 5 + .../homework_20170311/src/Test.java | 29 ++ .../coding2017/basic/ArrayList.java | 92 ++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++ .../dengliotng/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 124 ++++++++ .../dengliotng/coding2017/basic/List.java | 9 + .../dengliotng/coding2017/basic/Queue.java | 19 ++ .../dengliotng/coding2017/basic/Stack.java | 22 ++ .../src/com/basic/ArrayList.java | 94 ++++++ .../src/com/basic/BinaryTreeNode.java | 45 +++ .../src/com/basic/Iterator.java | 9 + .../src/com/basic/LinkedList.java | 286 +++++++++++++++++ .../homework_20170312/src/com/basic/List.java | 14 + .../src/com/basic/Queue.java | 24 ++ .../src/com/basic/Stack.java | 30 ++ .../src/testcase/TestArrayList.java | 189 +++++++++++ .../src/testcase/TestLinkedList.java | 190 ++++++++++++ .../src/com/basic/ArrayUtil.java | 293 ++++++++++++++++++ .../src/com/struts/DomXmlHelper.java | 105 +++++++ .../src/com/struts/LoginAction.java | 40 +++ .../src/com/struts/Struts.java | 91 ++++++ .../src/com/struts/View.java | 23 ++ .../src/com/struts/struts.xml | 11 + .../src/testcase/StrutsTest.java | 57 ++++ .../src/testcase/TestArrayUtil.java | 147 +++++++++ 26 files changed, 1987 insertions(+) create mode 100644 group24/1525619747/.gitignore create mode 100644 group24/1525619747/homework_20170311/src/Test.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/ArrayList.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/Iterator.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/LinkedList.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/List.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/Queue.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/Stack.java create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestArrayList.java create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java create mode 100644 group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/LoginAction.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/Struts.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/View.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/struts.xml create mode 100644 group24/1525619747/homework_20170319/src/testcase/StrutsTest.java create mode 100644 group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java diff --git a/group24/1525619747/.gitignore b/group24/1525619747/.gitignore new file mode 100644 index 0000000000..5b4edb7bcb --- /dev/null +++ b/group24/1525619747/.gitignore @@ -0,0 +1,5 @@ +*bin/ + +.project +.classpath +.settings diff --git a/group24/1525619747/homework_20170311/src/Test.java b/group24/1525619747/homework_20170311/src/Test.java new file mode 100644 index 0000000000..9e40820653 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/Test.java @@ -0,0 +1,29 @@ +import com.github.dengliotng.coding2017.basic.ArrayList; +import com.github.dengliotng.coding2017.basic.Iterator; + +/** + * Created by LeonDeng on 2017/3/11. + */ +public class Test { + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + for (int i =0; i<10; i++) { + a.add("hello" + i); + } + a.add(20.56); + System.out.println(a.size()); + a.add(5, 20.56); + System.out.println(a.size()); + System.out.println(a.get(5)); +// a.remove(5); +// a.remove(200); + + Iterator i = a.iterator(); + for ( ; i.hasNext(); ) { + String str = i.next().toString(); + System.out.println(str); + } + } + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..a588d2845e --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.github.dengliotng.coding2017.basic; + +public class ArrayList implements List { + + private int size = 0; + private static final int maxLength = 100; + + private Object[] elementData = new Object[maxLength]; + + public void add(Object o){ + if (size > maxLength) { + return; + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + if (size > maxLength) { + return; + } + if (elementData[index] == null) { + elementData[index] = o; + ++size; + return; + } else { + int emptyIndex = index + 1; + boolean hasEmpty = false; + for (int i = index; i < maxLength; i++) { + if (elementData[i] == null) { + hasEmpty = true; + emptyIndex = i; + } + } + if (!hasEmpty) { + return; + } + //shift + for (int i = emptyIndex; i > index; --i) { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + ++size; + } + } + + public Object get(int index){ + if (index > maxLength || index < 0) { + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if (index > maxLength || index < 0) { + return null; + } + Object o = elementData[index]; + for (int i = index; i < size - 1; ++i) { + elementData[i] = elementData[i+1]; + } + --size; + return o; + } + + public int size(){ + return size; + } + + public Iterator iterator() { + return new Iterator() { + private int index=0; + @Override + public boolean hasNext() { + if (index < size) { + return true; + } + return false; + } + + @Override + public Object next() { + if (hasNext()) { + return elementData[index++]; + } + return null; + } + }; + } + + + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..fd3e827346 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.dengliotng.coding2017.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/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..b40a21f6c4 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.dengliotng.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..e8f8244d46 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.github.dengliotng.coding2017.basic; + + + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java new file mode 100644 index 0000000000..fe5f523eea --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.dengliotng.coding2017.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/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java new file mode 100644 index 0000000000..556e3b4293 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java @@ -0,0 +1,19 @@ +package com.github.dengliotng.coding2017.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java new file mode 100644 index 0000000000..ae0a194504 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java @@ -0,0 +1,22 @@ +package com.github.dengliotng.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java new file mode 100644 index 0000000000..6c02e8d4f7 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -0,0 +1,94 @@ +package com.basic; + +public class ArrayList implements List +{ + + private int size = 0; + + private final int MAXNSIZE = 100; + + private Object[] elementData = new Object[MAXNSIZE]; + + public void add(Object o) + { + if (size > MAXNSIZE) + { + String errorInfo = "Out of max size" + MAXNSIZE; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + elementData[size++] = o; + } + + public void add(int index, Object o) + { + if (index > size) + { + String errorInfo = "Index to add: " + index + + " is out of current size: " + size; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + for (int i = size + 1; i > index + 1; i--) + { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + ++size; + } + + public Object get(int index) + { + if (index < 0 || index >= size) + { + String errorInfo = "Index to get: " + index + + " is invalid, current range: 0 - " + (size - 1); + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + return elementData[index]; + } + + public Object remove(int index) + { + if (index < 0 || index >= size) + { + String errorInfo = "Index to remove: " + index + + " is invalid, current range: 0 - " + (size - 1); + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + for (int i = index; i < size - 1; i++) + { + elementData[i] = elementData[i + 1]; + } + Object o = elementData[size]; + elementData[size--] = null; + return o; + } + + public int size() + { + return size; + } + + public Iterator iterator() + { + return new Iterator() + { + + private int index = 0; + + public boolean hasNext() + { + return (index < size); + } + + public Object next() + { + if (hasNext()) + { + return elementData[index++]; + } + return null; + } + }; + } + +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..9cf6755972 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -0,0 +1,45 @@ +package com.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/group24/1525619747/homework_20170312/src/com/basic/Iterator.java b/group24/1525619747/homework_20170312/src/com/basic/Iterator.java new file mode 100644 index 0000000000..402b5346a5 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.basic; + +public interface Iterator +{ + public boolean hasNext(); + + public Object next(); + +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java new file mode 100644 index 0000000000..1e692521e2 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -0,0 +1,286 @@ +package com.basic; + +public class LinkedList implements List +{ + + private Node head; + + public void add(Object o) + { + if (head == null) { + head = new Node(o, null); + } else { + Node nodePointer = head; + while (nodePointer.next != null) { + nodePointer = nodePointer.next; + } + nodePointer.next = new Node(o, null); + } + + } + + public void add(int index, Object o) + { + int size = size(); + if (index < 0 || index > size) { + String errorInfo = "Invalid index to add:" + index+ " out of range: [0," + size + "]"; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + int step = 0; + Node nodePointer = head; + while (step < index) { + nodePointer = nodePointer.next; + ++step; + } + nodePointer.next = new Node(o, nodePointer.next); + } + + public Object get(int index) + { + int size = size(); + if (index < 0 || index > size) { + String errorInfo = "Invalid index to get:" + index+ " out of range: [0," + size + "]"; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + + int step = 0; + Node nodePointer = head; + while (step < index) { + nodePointer = nodePointer.next; + ++step; + } + + return nodePointer.data; + } + + public Object remove(int index) + { + int size = size(); + if (index < 0 || index > size) { + String errorInfo = "Invalid index to remove:" + index+ " out of range: [0," + size + "]"; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + + int step = 0; + Node nodePointer = head; + Node lastPointer = head; + + while (step < index) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + ++step; + } + + Object o = null; + + if (lastPointer == nodePointer) { + Node toDelete = head; + o = toDelete.data; + head = head.next; + toDelete = null; + } else { + o = nodePointer.data; + lastPointer.next = nodePointer.next; + nodePointer = null; + } + + return o; + } + + public int size() + { + int size = 0; + if (head != null) { + ++size; + Node nodePointer = head; + while (nodePointer.next != null) { + ++size; + nodePointer = nodePointer.next; + } + } + return size; + } + + public void addFirst(Object o) + { + if (head == null) { + head = new Node(o, null); + return; + } + head = new Node(o, head); + } + + public void addLast(Object o) + { + if (head == null) { + head = new Node(o, null); + return; + } + + Node nodePointer = head; + while (nodePointer.next != null) { + nodePointer = nodePointer.next; + } + nodePointer.next = new Node(o, null); + } + + public Object removeFirst() + { + if (head == null) { + return null; + } + + Node toDelete = head; + Object o = head.data; + head = head.next; + toDelete = null; + + return o; + } + + public Object removeLast() + { + if (head == null) { + return null; + } + + Node nodePointer = head; + Node lastPointer = head; + + while (nodePointer.next != null) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + } + lastPointer.next = null; + Object o = nodePointer.data; + nodePointer = null; + + return o; + } + + public Iterator iterator() + { + return new Iterator() { + + private Node nodePointer = head; + + public boolean hasNext() + { + // TODO Auto-generated method stub + return (nodePointer != null); + } + + public Object next() + { + // TODO Auto-generated method stub + if (hasNext()) { + Object o = nodePointer.data; + nodePointer = nodePointer.next; + return o; + } + return null; + } + }; + } + + private static class Node + { + Object data; + Node next; + public Node(Object o, Node n) { + this.data = o; + this.next = n; + } + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() + { + if (head == null) { + return; + } + + Node reverse = null; + while (size() > 0) { + reverse = new Node(removeFirst(), reverse); + } + + head = reverse; + } + + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() + { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) + { + + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public static int[] getElements(LinkedList list) + { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) + { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() + { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) + { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) + { + return null; + } +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/List.java b/group24/1525619747/homework_20170312/src/com/basic/List.java new file mode 100644 index 0000000000..8892e795e9 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/List.java @@ -0,0 +1,14 @@ +package com.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/group24/1525619747/homework_20170312/src/com/basic/Queue.java b/group24/1525619747/homework_20170312/src/com/basic/Queue.java new file mode 100644 index 0000000000..786b95a3a4 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Queue.java @@ -0,0 +1,24 @@ +package com.basic; + +public class Queue +{ + + public void enQueue(Object o) + { + } + + public Object deQueue() + { + return null; + } + + public boolean isEmpty() + { + return false; + } + + public int size() + { + return -1; + } +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/Stack.java b/group24/1525619747/homework_20170312/src/com/basic/Stack.java new file mode 100644 index 0000000000..0226551dcc --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Stack.java @@ -0,0 +1,30 @@ +package com.basic; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + } + + public Object pop() + { + return null; + } + + public Object peek() + { + return null; + } + + public boolean isEmpty() + { + return false; + } + + public int size() + { + return -1; + } +} diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java new file mode 100644 index 0000000000..471243a83a --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -0,0 +1,189 @@ +package testcase; + +import static org.junit.Assert.*; + +import com.basic.ArrayList; +import com.basic.Iterator; + +import org.junit.Test; + +public class TestArrayList +{ + + @Test + public void testAdd() + { + ArrayList list = new ArrayList(); + try + { + list.add(3); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + try + { + for (int i = 0; i < 100; i++) + { + list.add(i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + assertEquals("100", e.getMessage()); + } + + } + + /* + * test add(index, o) + */ + @Test + public void testIndexAdd() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + list.add(3, 20); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + assertFalse(list.get(3).equals(3)); + assertTrue(list.get(3).equals(20)); + assertTrue(list.get(4).equals(4)); + + try + { + for (int i = 0; i < 100; i++) + { + list.add(i, i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + assertEquals("100", e.getMessage()); + } + } + + /* + * test get(index) + */ + @Test + public void testGet() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + try + { + list.get(0); + list.get(5); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + try + { + list.get(10); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + System.out.println(list.size()); + String errorInfo = "Index to get: 10 is invalid, current range: 0 - " + + (list.size() - 1); + assertEquals(errorInfo, e.getMessage()); + } + } + + /* + * test remove(index) and size + */ + @Test + public void testRemove() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + assertTrue(list.size() == 10); + list.remove(3); + assertTrue(list.size() == 9); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + assertFalse(list.get(3).equals(3)); + assertTrue(list.get(3).equals(4)); + + try + { + list.remove(-3); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + } + + try + { + list.remove(20); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + } + } + + @Test + public void testInterator() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + + Iterator it = list.iterator(); + assertTrue(it.hasNext()); + assertTrue(it.next().equals(0)); + assertTrue(it.next().equals(1)); + assertTrue(it.next().equals(2)); + } + +} diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java new file mode 100644 index 0000000000..1f855e873c --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -0,0 +1,190 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.Iterator; +import com.basic.LinkedList; + +public class TestLinkedList +{ + + /* + * test add() and size() + * */ + @Test + public void testAdd() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + assertTrue(linkedList.size() == 2); + } + + + @Test + public void testGet() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + assertTrue(linkedList.get(0).equals(3)); + assertFalse(linkedList.get(0).equals("hello world")); + assertTrue(linkedList.get(1).equals("hello world")); + + try { + linkedList.get(-1); + } catch (ArrayIndexOutOfBoundsException e) { + assertNotNull(e); + String errorInfo = "Invalid index to get:" + -1 + " out of range: [0," + linkedList.size() + "]"; + assertTrue(e.getMessage().equals(errorInfo)); +// System.out.println(e.getMessage()); + } + } + + + @Test + public void testRemove() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + linkedList.add(4); + linkedList.add("Leon Deng"); + + try { + linkedList.remove(-1); + } catch (ArrayIndexOutOfBoundsException e) { + assertNotNull(e); + String errorInfo = "Invalid index to remove:" + -1 + " out of range: [0," + linkedList.size() + "]"; + assertTrue(e.getMessage().equals(errorInfo)); + } + + try { + linkedList.remove(10); + } catch (ArrayIndexOutOfBoundsException e) { + assertNotNull(e); + String errorInfo = "Invalid index to remove:" + 10 + " out of range: [0," + linkedList.size() + "]"; + assertTrue(e.getMessage().equals(errorInfo)); + } + + Object o = null; + try { + o = linkedList.remove(0); + } catch (ArrayIndexOutOfBoundsException e) { + assertNull(e); + } + assertTrue(o.equals(3)); + + try { + o = linkedList.remove(2); + } catch (ArrayIndexOutOfBoundsException e) { + assertNull(e); + } +// System.out.println(o); + assertTrue(o.equals("Leon Deng")); + } + + + @Test + public void testAddFirst() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + + linkedList.addFirst("Leon Deng"); + assertTrue(linkedList.get(0).equals("Leon Deng")); + assertTrue(linkedList.get(1).equals(3)); + } + + @Test + public void testAddLast() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + + linkedList.addLast("Leon Deng"); + assertTrue(linkedList.get(linkedList.size() - 1).equals("Leon Deng")); + } + + @Test + public void testRemoveFirst() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + + Object o = linkedList.removeFirst(); + assertTrue(o.equals(3)); + o = linkedList.removeFirst(); + assertTrue(o.equals("hello world")); + } + + @Test + public void testRemoveLast() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + linkedList.add("Leon Deng"); + + Object o = linkedList.removeLast(); + assertTrue(o.equals("Leon Deng")); + o = linkedList.removeLast(); + assertTrue(o.equals("hello world")); + } + + @Test + public void testInterator() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("Leon Deng"); + + Iterator it = linkedList.iterator(); + assertTrue(it.hasNext()); + assertTrue(it.next().equals(3)); + assertTrue(it.hasNext()); + assertTrue(it.next().equals("Leon Deng")); + assertFalse(it.hasNext()); + } + + @Test + public void testReverse() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); +// +// linkedList.reverse(); +// +// it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + Object o1 = linkedList.get(0); + Object o2 = linkedList.get(linkedList.size() - 1); + + linkedList.reverse(); + + Object o3 = linkedList.get(0); + Object o4 = linkedList.get(linkedList.size() - 1); + + assertEquals(o1, o4); + assertEquals(o2, o3); + + linkedList.reverse(); + Object o5 = linkedList.get(0); + Object o6 = linkedList.get(linkedList.size() - 1); + + assertEquals(o1, o5); + assertEquals(o2, o6); + } + + + +} diff --git a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java new file mode 100644 index 0000000000..86ba46d8cd --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java @@ -0,0 +1,293 @@ +package com.basic; + +import java.util.Collections; + +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 left = 0; + int right = length - 1; + int a = 0; + int b = 0; + while (left < right) + { + swap(origin, left++, right--); + } + } + + private void swap(int[] origin, int i, int j) + { + // TODO Auto-generated method stub + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + } + + /** + * 现在有如下的一个数组: 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[] newArray = new int[length]; + int[] zeroArray = new int[length]; + + int zIndex = 0; + int nzIndex = 0; + for (int i = 0; i < length; i++) + { + if (oldArray[i] == 0) + { + zeroArray[zIndex++] = oldArray[i]; + } + else + { + newArray[nzIndex++] = oldArray[i]; + } + } + + int[] newArray2 = new int[nzIndex]; + for (int i = 0; i < nzIndex; i++) + { + newArray2[i] = newArray[i]; + } + + return newArray2; + } + + /** + * 给定两个已经排序好的整形数组, 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]; + + int index1 = 0; + int index2 = 0; + int index3 = 0; + + while (index1 < length1 && index2 < length2) + { + + + if (index3 > 0) + { + if (array3[index3 - 1] == array1[index1]) + { + ++index1; + continue; + } + if (array3[index3 - 1] == array2[index2]) + { + ++index2; + continue; + } + } + + if (array1[index1] == array2[index2]) + { + array3[index3++] = array1[index1]; + ++index1; + ++index2; + continue; + } + if (array1[index1] < array2[index2]) + { + array3[index3++] = array1[index1]; + ++index1; + continue; + } + if (array1[index1] > array2[index2]) + { + array3[index3++] = array2[index2]; + ++index2; + continue; + } + } + + while (index1 < length1) { + array3[index3++] = array1[index1++]; + } + while (index2 < length2) { + array3[index3++] = array1[index2++]; + } + + int[] newArray = new int[index3]; + for (int i = 0; i < index3; i++) + { + newArray[i] = array3[i]; + } + + return newArray; + } + + /** + * 把一个已经存满数据的数组 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 length = oldArray.length; + int[] newArr = new int[length + size]; + for (int i = 0; i < length; i++) { + newArr[i] = oldArray[i]; + } + for (int i = length; i < length + size; i++) { + newArr[i] = 0; + } + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) + { + if (max == 1) { + return null; + } + int[] arr = new int[max / 2]; + int a = 1; + int b = 1; + int c = 0; + + arr[0] = 1; + arr[1] = 1; + + int index = 2; + while (a + b < max) { + arr[index++] = a + b; + c = b; + b = a + b; + a = c; + } + + int[] newArr = new int[index]; + for (int i = 0; i < index; i++) { + newArr[i] = arr[i]; + } + + return newArr; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) + { + int size = max / 2 + 1; + int[] arr = new int[size]; + int index = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + arr[index++] = i; + } + } + + int[] newArr = new int[index]; + for (int i = 0; i < index; i++) { + newArr[i] = arr[i]; + } + + return newArr; + } + + public boolean isPrime(int i) { + for (int j = 2; j < i / 2; j++) { + if (i % j == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) + { + int size = max / 2 + 1; + int[] arr = new int[size]; + int index = 0; + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + arr[index++] = i; + } + } + + int[] newArr = new int[index]; + for (int i = 0; i < index; i++) { + newArr[i] = arr[i]; + } + + return newArr; + } + + public boolean isPerfectNumber(int num) { + int sum = 0; + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0) { + sum += i; + } + } +// System.out.println("num: " + num + ", sum:" + sum); + return (num == sum); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) + { + String str = ""; + int length = array.length; + for (int a : array) { + str += a + seperator; + } + str = str.substring(0, str.length()-1); + return str; + } + +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java new file mode 100644 index 0000000000..507a974297 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java @@ -0,0 +1,105 @@ +package com.struts; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * DOM方式解析xml + */ +public class DomXmlHelper { + + private Map kv = new HashMap(); + + public DomXmlHelper() throws DocumentException { + fetchAttributes(); + } + + //遍历当前节点下的所有节点 + private void listNodes(Element node, String loop, Map kv){ + +// System.out.println("当前节点的名称:" + node.getName()); + if (loop.equals("")) { + loop += node.getName(); + } else { + kv.put(loop, node.getName()); + loop += "-" + node.getName(); + } + + //首先获取当前节点的所有属性节点 + List list = node.attributes(); + //遍历属性节点 + for(Attribute attribute : list){ +// System.out.println("属性 "+attribute.getName() +":" + attribute.getValue()); + kv.put(loop, attribute.getValue()); + loop += "-" + attribute.getValue(); +// System.out.println("loop: " + loop); + } + + //如果当前节点内容不为空,则输出 + if(!(node.getTextTrim().equals(""))){ +// System.out.println("内容 " + node.getName() + ":" + node.getText()); + kv.put(loop, node.getText()); + loop += "-" + node.getText(); +// System.out.println("loop: " + loop); + } + + + //同时迭代当前节点下面的所有子节点 + //使用递归 + Iterator iterator = node.elementIterator(); + while(iterator.hasNext()){ + Element e = iterator.next(); + listNodes(e, loop, kv); + } + } + + private void fetchAttributes() throws DocumentException { + //创建SAXReader对象 + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + Document document = (Document) reader.read(new File("./src/com/struts/struts.xml")); + + //获取根节点元素对象 + Element root = ((org.dom4j.Document) document).getRootElement(); + + listNodes(root, "", kv); + + for (Map.Entry entity : kv.entrySet()) { +// System.out.println("key: " + entity.getKey() + " , value: " + entity.getValue()); + } + } + + public String getActionView(String action, String method) { + String key = "struts-action-" + action; + String className = kv.get(key); + key += "-" + className + "-result-" + method; + return kv.get(key); + } + + public String getActionClassByName(String action) { + String key = "struts-action-" + action; + return kv.get(key); + } + +// public static void main(String[] args) throws DocumentException { +// DomXmlHelper dm = new DomXmlHelper(); +// System.out.println(dm.getActionClassByName("login")); +// System.out.println(dm.getActionView("login", "success")); +// System.out.println(dm.getActionView("login", "fail")); +// +// System.out.println(dm.getActionClassByName("logout")); +// System.out.println(dm.getActionView("logout", "success")); +// System.out.println(dm.getActionView("logout", "error")); +// } + +} \ No newline at end of file diff --git a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java new file mode 100644 index 0000000000..24e01dab79 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java @@ -0,0 +1,40 @@ +package com.struts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author + * + */ +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(){ + System.out.println("name: " + name + " , password: " + password); + 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/group24/1525619747/homework_20170319/src/com/struts/Struts.java b/group24/1525619747/homework_20170319/src/com/struts/Struts.java new file mode 100644 index 0000000000..1750ce2018 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/Struts.java @@ -0,0 +1,91 @@ +package com.struts; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; + +public class Struts +{ + + public static View runAction(String actionName, + Map parameters) throws DocumentException, + ClassNotFoundException, NoSuchMethodException, SecurityException, + InstantiationException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, NoSuchFieldException + { + + /* + * + * 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字段中。 + */ + + DomXmlHelper dx = new DomXmlHelper(); + String action = "login"; + String className = dx.getActionClassByName(action); +// System.out.println(className); + + Class class1 = null; + class1 = Class.forName(className); +// System.out.println("类名称 " + class1.getName()); + + if (class1 != null) { + // 调用class1的setName方法, 待参数 + //根据.class反射出来的类实例 + Object instance = class1.newInstance(); + + Method method = class1.getMethod("setName", String.class); + Object res1 =method.invoke(instance, "test"); + + method = class1.getMethod("setPassword", String.class); + Object res2 = method.invoke(instance, "1234"); + + // set attr + for (Map.Entry entity : parameters.entrySet()) { + String attrName = entity.getKey(); + String attrValue = entity.getValue(); + + Field idF = class1.getDeclaredField(attrName); // 获取属性 + idF.setAccessible(true); // 使用反射机制可以打破封装性,导致了java对象的属性不安全。 + idF.set(instance, attrValue); // set + } + + View view = new View(); + + method = class1.getMethod("execute"); + Object res3 = method.invoke(instance); +// System.out.println(res3); + String jsp = dx.getActionView(action, res3.toString()); + view.setJsp(jsp); + + method = class1.getMethod("getMessage"); + Object res4 = method.invoke(instance); +// System.out.println(res4); + + Map map = new HashMap(); + map.put("message", res4.toString()); + + view.setParameters(map); + + return view; + } + + return null; + } + +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/View.java b/group24/1525619747/homework_20170319/src/com/struts/View.java new file mode 100644 index 0000000000..5d6a6d9d1f --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/View.java @@ -0,0 +1,23 @@ +package com.struts; + +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/group24/1525619747/homework_20170319/src/com/struts/struts.xml b/group24/1525619747/homework_20170319/src/com/struts/struts.xml new file mode 100644 index 0000000000..a33fbd92bb --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java new file mode 100644 index 0000000000..8fd79a3e4e --- /dev/null +++ b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java @@ -0,0 +1,57 @@ +package testcase; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + + + + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import com.struts.DomXmlHelper; +import com.struts.Struts; +import com.struts.View; + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { + + 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() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { + 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/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java new file mode 100644 index 0000000000..6579308478 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java @@ -0,0 +1,147 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.ArrayUtil; + +public class TestArrayUtil +{ + private void print_r(int[] a) + { + for (int i = 0; i < a.length; i++) + { + System.out.print(a[i] + " "); + } + System.out.println(); + + // int index = 0; + // while (a[index] != '\0') { + // System.out.print(a[index] + " "); + // ++index; + // } + // System.out.println(); + } + + @Test + public void testReverseArray() + { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a = { 7, 9, 30, 3 }; + // print_r(a); + + arrayUtil.reverseArray(a); + // print_r(a); + assertTrue(a[0] == 3); + assertTrue(a[1] == 30); + assertTrue(a[3] == 7); + + int[] b = { 7, 9, 30, 3, 4 }; + // print_r(b); + + arrayUtil.reverseArray(b); + // print_r(b); + assertTrue(b[0] == 4); + assertTrue(b[1] == 3); + assertTrue(b[3] == 9); + assertTrue(b[2] == 30); + } + + @Test + public void testRemoveZero() + { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; + int[] newArr = arrayUtil.removeZero(oldArr); + // print_r(newArr); + assertFalse(newArr[4] == 0); + assertTrue(newArr[4] == 6); + + } + + @Test + public void testMerge() + { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a1 = { 3, 5, 7, 8 }; + int[] a2 = { 4, 5, 6, 7 }; + + int[] newArr = arrayUtil.merge(a1, a2); +// print_r(newArr); + assertTrue(newArr[0] == 3); + assertTrue(newArr[2] == 5); + assertTrue(newArr[3] == 6); + assertTrue(newArr[5] == 8); + } + + @Test + public void testGrow() { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a1 = { 3, 5, 7, 8 }; + a1 = arrayUtil.grow(a1, 3); +// print_r(a1); + assertTrue(a1[0] == 3); + assertTrue(a1[2] == 7); + assertTrue(a1[3] == 8); + assertTrue(a1[4] == 0); + assertTrue(a1[5] == 0); + assertTrue(a1[6] == 0); + + } + + @Test + public void testFibonacci() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 100; + int[] arr = arrayUtil.fibonacci(max); +// print_r(arr); + + assertNotNull(arr); + int index = (int) (Math.random() * arr.length); + assertTrue(arr[index] < max); + + arr = arrayUtil.fibonacci(1); + assertNull(arr); + } + + @Test + public void testGetPrimes() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 23; + int[] arr = arrayUtil.getPrimes(max); +// print_r(arr); + + int index = (int) (Math.random() * arr.length); + assertTrue(arr[index] < max); + assertTrue(arrayUtil.isPrime(arr[index])); + + } + + @Test + public void testGetPerfectNumbers() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 300; + int[] arr = arrayUtil.getPerfectNumbers(max); +// print_r(arr); + + int index = (int) (Math.random() * arr.length); + assertTrue(arr[index] < max); + assertTrue(arrayUtil.isPerfectNumber(arr[index])); + + } + + @Test + public void testJoin() { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] a = {3,8,9}; + String str = arrayUtil.join(a, "-"); +// System.out.println(str); + assertTrue(str.equals("3-8-9")); + } + +} From 477d9fe42402fe2e8fbad61901c48d1432c370ba Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:07:00 +0800 Subject: [PATCH 2/6] add first week homework untrancked files --- .../src/com/basic/ArrayList.java | 7 +- .../src/com/basic/LinkedList.java | 161 ++++++++++++++- .../src/com/basic/Queue.java | 8 +- .../src/com/basic/Stack.java | 9 +- .../src/testcase/TestArrayList.java | 28 ++- .../src/testcase/TestLinkedList.java | 194 ++++++++++++++++++ .../src/testcase/TestQueue.java | 31 +++ .../src/testcase/TestStack.java | 31 +++ 8 files changed, 442 insertions(+), 27 deletions(-) create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestQueue.java create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestStack.java diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java index 6c02e8d4f7..bb540052cd 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -21,13 +21,13 @@ public void add(Object o) public void add(int index, Object o) { - if (index > size) + if (index >= size && size > 0) { String errorInfo = "Index to add: " + index + " is out of current size: " + size; throw new ArrayIndexOutOfBoundsException(errorInfo); } - for (int i = size + 1; i > index + 1; i--) + for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } @@ -54,11 +54,12 @@ public Object remove(int index) + " is invalid, current range: 0 - " + (size - 1); throw new ArrayIndexOutOfBoundsException(errorInfo); } + + Object o = elementData[index]; for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } - Object o = elementData[size]; elementData[size--] = null; return o; } diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java index 1e692521e2..2d9943e89b 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -124,6 +124,7 @@ public void addLast(Object o) nodePointer.next = new Node(o, null); } + @SuppressWarnings ("unused") public Object removeFirst() { if (head == null) { @@ -215,9 +216,18 @@ public void reverse() * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 * ,删除以后的值为7,8,10 */ + @SuppressWarnings ("unused") public void removeFirstHalf() { - + int removeLength = size() / 2; + int step = 0; + Node toDelete = null; + while (step < removeLength) { + toDelete = head; + head = head.next; + toDelete = null; + ++step; + } } /** @@ -228,7 +238,38 @@ public void removeFirstHalf() */ public void remove(int i, int length) { - + int size = size(); + if (i >= size) { + return; + } + Node nodePointer = head; + Node lastPointer = head; + int step = 0; + while (step < i) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + ++step; + } + + step = 0; + Node toDelete = null; + if (lastPointer == head) { + while (step < length) { + toDelete = head; + head = head.next; + toDelete = null; + ++step; + } + } else { + while (step < length) { + toDelete = nodePointer; + nodePointer = nodePointer.next; + toDelete = null; + ++step; + } + lastPointer.next = nodePointer; + } + } /** @@ -238,9 +279,15 @@ public void remove(int i, int length) * * @param list */ - public static int[] getElements(LinkedList list) + public int[] getElements(LinkedList list) { - return null; + int[] elements = new int[list.size()]; + Iterator it = list.iterator(); + int index = 0; + for ( ; it.hasNext(); ) { + elements[index++] = (Integer) get((Integer) (it.next())); + } + return elements; } /** @@ -251,7 +298,41 @@ public static int[] getElements(LinkedList list) public void subtract(LinkedList list) { - + if (head == null) { + return; + } + Node nodePointer = head; + Node lastPointer = head; + Node toDelte = null; + while (nodePointer != null) { + if (list.contain(nodePointer.data)) { + if (nodePointer == head) { + toDelte = head; + head = head.next; + toDelte = null; + nodePointer = head; + lastPointer = head; + } else { + toDelte = nodePointer; + lastPointer.next = nodePointer.next; + toDelte = null; + } + } + lastPointer = nodePointer; + nodePointer = nodePointer.next; + } + + } + + private boolean contain(Object o) { + Node nodePointer = head; + while (nodePointer != null) { + if (nodePointer.data.equals(o)) { + return true; + } + nodePointer = nodePointer.next; + } + return false; } /** @@ -259,7 +340,20 @@ public void subtract(LinkedList list) */ public void removeDuplicateValues() { - + Node nodePointer = head; + if (nodePointer.next == null) { + return; + } + + Node toDelete = null; + while (nodePointer.next != null) { + while (nodePointer.next != null && nodePointer.data.equals(nodePointer.next.data)) { // delete nodePointer.next + toDelete = nodePointer.next; + nodePointer.next = nodePointer.next.next; + toDelete = null; + } + nodePointer = nodePointer.next; + } } /** @@ -270,7 +364,30 @@ public void removeDuplicateValues() */ public void removeRange(int min, int max) { - + Node nodePointer = head; + Node lastPointer = head; + if (nodePointer == null) { + return; + } + while (nodePointer != null && ((Integer) nodePointer.data) <= (new Integer(min))) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + } + Node toDelete = null; + while (nodePointer != null && ((Integer) nodePointer.data) < (new Integer(max))) { + if (nodePointer == head) { + toDelete = head; + head = head.next; + toDelete = null; + nodePointer = head; + lastPointer = head; + } else { + toDelete = nodePointer; + lastPointer.next = nodePointer.next; + nodePointer = nodePointer.next; + toDelete = null; + } + } } /** @@ -281,6 +398,34 @@ public void removeRange(int min, int max) */ public LinkedList intersection(LinkedList list) { - return null; + LinkedList linkedList = new LinkedList(); + Iterator it1 = iterator(); + Iterator it2 = list.iterator(); + Object o1 = null; + Object o2 = null; + + if (size() == 0 || list.size() == 0) { + return null; + } + + o1 = it1.next(); + o2 = it2.next(); + + while (o1 != null && o2 != null) { +// System.out.println(o1 + " " + o2); + if (((Integer) o1) == ((Integer) o2)) { + linkedList.add(o1); + o1 = it1.next(); + o2 = it2.next(); + } else { + if (((Integer) o1) > ((Integer) o2)) { + o2 = it2.next(); + } else { + o1 = it1.next(); + } + } + } + + return linkedList; } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/Queue.java b/group24/1525619747/homework_20170312/src/com/basic/Queue.java index 786b95a3a4..b040c85722 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/Queue.java +++ b/group24/1525619747/homework_20170312/src/com/basic/Queue.java @@ -2,23 +2,25 @@ public class Queue { + private LinkedList list = new LinkedList(); public void enQueue(Object o) { + list.addLast(o); } public Object deQueue() { - return null; + return list.removeFirst(); } public boolean isEmpty() { - return false; + return (list.size() == 0); } public int size() { - return -1; + return list.size(); } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/Stack.java b/group24/1525619747/homework_20170312/src/com/basic/Stack.java index 0226551dcc..db7ada4c53 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/Stack.java +++ b/group24/1525619747/homework_20170312/src/com/basic/Stack.java @@ -6,25 +6,26 @@ public class Stack public void push(Object o) { + elementData.add(0, o); } public Object pop() { - return null; + return elementData.remove(0); } public Object peek() { - return null; + return elementData.get(0); } public boolean isEmpty() { - return false; + return (elementData.size() == 0); } public int size() { - return -1; + return elementData.size(); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java index 471243a83a..4cf2bf1a62 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -22,6 +22,7 @@ public void testAdd() { assertNull(e); } + assertTrue(list.get(0).equals(3)); try { for (int i = 0; i < 100; i++) @@ -32,7 +33,7 @@ public void testAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } @@ -52,14 +53,23 @@ public void testIndexAdd() list.add(i); } list.add(3, 20); + list.add(0, 30); + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } - assertFalse(list.get(3).equals(3)); - assertTrue(list.get(3).equals(20)); - assertTrue(list.get(4).equals(4)); + +// Iterator it = list.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + assertTrue(list.get(0).equals(30)); + assertTrue(list.get(4).equals(20)); + assertTrue(list.get(5).equals(3)); try { @@ -71,7 +81,7 @@ public void testIndexAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } } @@ -110,8 +120,8 @@ public void testGet() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); - System.out.println(list.size()); +// System.out.println(e.getMessage()); +// System.out.println(list.size()); String errorInfo = "Index to get: 10 is invalid, current range: 0 - " + (list.size() - 1); assertEquals(errorInfo, e.getMessage()); @@ -149,7 +159,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); } try @@ -159,7 +169,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java index 1f855e873c..54cf229718 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -185,6 +185,200 @@ public void testReverse() { assertEquals(o2, o6); } + @Test + public void testRemoveFirstHalf() { + LinkedList linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + linkedList.removeFirstHalf(); + assertTrue(linkedList.get(0).equals(7)); + + linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + + linkedList.removeFirstHalf(); + assertTrue(linkedList.get(2).equals(10)); + } + + @Test + public void testRemoveIndexLength() { + LinkedList linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + + linkedList.remove(0,2); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + assertTrue(linkedList.get(0).equals(7)); + + linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + + linkedList.remove(2,2); + assertTrue(linkedList.get(0).equals(2)); + assertTrue(linkedList.get(2).equals(10)); + + } + + @Test + public void testGetElements() { +// 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + + LinkedList indexLinkedList = new LinkedList(); + indexLinkedList.add(1); + indexLinkedList.add(3); + indexLinkedList.add(4); + indexLinkedList.add(6); + + int[] elements = linkedList.getElements(indexLinkedList); + +// for (int i = 0; i < elements.length; i++) { +// System.out.print(elements[i] + " "); +// } +// System.out.println(); + + assertEquals(elements[0], linkedList.get(1)); + assertEquals(elements[1], linkedList.get(3)); + assertEquals(elements[2], linkedList.get(4)); + assertEquals(elements[3], linkedList.get(6)); + } + + + @Test + public void testSubstract() { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + + + LinkedList substractList = new LinkedList(); + substractList.add(11); + substractList.add(301); + substractList.add(404); + substractList.add(501); + substractList.add(701); + + linkedList.subtract(substractList); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + assertFalse(linkedList.get(0).equals(11)); + assertTrue(linkedList.get(0).equals(101)); + } + @Test + public void testRemoveDuplicateValues() { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(3); + linkedList.add(4); + linkedList.add(4); + linkedList.add(5); + + linkedList.removeDuplicateValues(); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + assertTrue(linkedList.get(0).equals(1)); + assertTrue(linkedList.get(1).equals(2)); + assertTrue(linkedList.get(4).equals(5)); + + } + + @Test + public void testRemoveRange() { + LinkedList linkedList = new LinkedList(); + for (int i = 0; i < 10; i++) { + linkedList.add(i); + } + + linkedList.removeRange(3, 7); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + assertTrue(linkedList.get(3).equals(3)); + assertTrue(linkedList.get(4).equals(7)); + } + + @Test + public void testIntersection() { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + + LinkedList secondList = new LinkedList(); + secondList.add(1); + secondList.add(3); + secondList.add(5); + secondList.add(6); + + LinkedList intersection = linkedList.intersection(secondList); +// Iterator it = intersection.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + assertTrue(intersection.get(0).equals(1)); + assertTrue(intersection.get(1).equals(3)); + assertTrue(intersection.get(2).equals(5)); + assertTrue(intersection.get(3).equals(6)); + } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java new file mode 100644 index 0000000000..a08ee80951 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java @@ -0,0 +1,31 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.Queue; + +public class TestQueue +{ + /* + * test enQueue() isEmpty() and size() deQueue + * */ + @Test + public void testQueue() { + Queue queue = new Queue(); + + assertTrue(queue.isEmpty()); + + for (int i = 10; i < 20; i++) { + queue.enQueue(i); + } + + assertFalse(queue.isEmpty()); + assertTrue(queue.size() == 10); + + assertTrue(queue.deQueue().equals(10)); + assertTrue(queue.deQueue().equals(11)); + } + +} diff --git a/group24/1525619747/homework_20170312/src/testcase/TestStack.java b/group24/1525619747/homework_20170312/src/testcase/TestStack.java new file mode 100644 index 0000000000..58c7206fea --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestStack.java @@ -0,0 +1,31 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.Stack; + +public class TestStack +{ + @Test + public void testStack() { + Stack stack = new Stack(); + + assertTrue(stack.isEmpty()); + + for (int i = 10; i < 20; i++) { + stack.push(i); + } + + assertFalse(stack.isEmpty()); + assertTrue(stack.size() == 10); + + assertTrue(stack.peek().equals(19)); + assertFalse(stack.peek().equals(10)); + + assertTrue(stack.pop().equals(19)); + assertTrue(stack.pop().equals(18)); + } + +} From 118d6c54c44ae1803ae171e3c75e6acf96c087f0 Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:14:07 +0800 Subject: [PATCH 3/6] delete unneed dir --- .../homework_20170311/src/Test.java | 29 ---- .../coding2017/basic/ArrayList.java | 92 ------------- .../coding2017/basic/BinaryTreeNode.java | 32 ----- .../dengliotng/coding2017/basic/Iterator.java | 7 - .../coding2017/basic/LinkedList.java | 124 ------------------ .../dengliotng/coding2017/basic/List.java | 9 -- .../dengliotng/coding2017/basic/Queue.java | 19 --- .../dengliotng/coding2017/basic/Stack.java | 22 ---- 8 files changed, 334 deletions(-) delete mode 100644 group24/1525619747/homework_20170311/src/Test.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java diff --git a/group24/1525619747/homework_20170311/src/Test.java b/group24/1525619747/homework_20170311/src/Test.java deleted file mode 100644 index 9e40820653..0000000000 --- a/group24/1525619747/homework_20170311/src/Test.java +++ /dev/null @@ -1,29 +0,0 @@ -import com.github.dengliotng.coding2017.basic.ArrayList; -import com.github.dengliotng.coding2017.basic.Iterator; - -/** - * Created by LeonDeng on 2017/3/11. - */ -public class Test { - - public static void main(String[] args) { - ArrayList a = new ArrayList(); - for (int i =0; i<10; i++) { - a.add("hello" + i); - } - a.add(20.56); - System.out.println(a.size()); - a.add(5, 20.56); - System.out.println(a.size()); - System.out.println(a.get(5)); -// a.remove(5); -// a.remove(200); - - Iterator i = a.iterator(); - for ( ; i.hasNext(); ) { - String str = i.next().toString(); - System.out.println(str); - } - } - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java deleted file mode 100644 index a588d2845e..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class ArrayList implements List { - - private int size = 0; - private static final int maxLength = 100; - - private Object[] elementData = new Object[maxLength]; - - public void add(Object o){ - if (size > maxLength) { - return; - } - elementData[size++] = o; - } - - public void add(int index, Object o){ - if (size > maxLength) { - return; - } - if (elementData[index] == null) { - elementData[index] = o; - ++size; - return; - } else { - int emptyIndex = index + 1; - boolean hasEmpty = false; - for (int i = index; i < maxLength; i++) { - if (elementData[i] == null) { - hasEmpty = true; - emptyIndex = i; - } - } - if (!hasEmpty) { - return; - } - //shift - for (int i = emptyIndex; i > index; --i) { - elementData[i] = elementData[i-1]; - } - elementData[index] = o; - ++size; - } - } - - public Object get(int index){ - if (index > maxLength || index < 0) { - return null; - } - return elementData[index]; - } - - public Object remove(int index){ - if (index > maxLength || index < 0) { - return null; - } - Object o = elementData[index]; - for (int i = index; i < size - 1; ++i) { - elementData[i] = elementData[i+1]; - } - --size; - return o; - } - - public int size(){ - return size; - } - - public Iterator iterator() { - return new Iterator() { - private int index=0; - @Override - public boolean hasNext() { - if (index < size) { - return true; - } - return false; - } - - @Override - public Object next() { - if (hasNext()) { - return elementData[index++]; - } - return null; - } - }; - } - - - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index fd3e827346..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.dengliotng.coding2017.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/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java deleted file mode 100644 index b40a21f6c4..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java deleted file mode 100644 index e8f8244d46..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - - - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java deleted file mode 100644 index fe5f523eea..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.dengliotng.coding2017.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/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java deleted file mode 100644 index 556e3b4293..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java deleted file mode 100644 index ae0a194504..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} From 47d4fe2ded043b43b1af76ae81d0fc884cee1813 Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:18:02 +0800 Subject: [PATCH 4/6] format codes --- .../src/com/basic/ArrayUtil.java | 93 +++++---- .../src/com/struts/DomXmlHelper.java | 196 ++++++++++-------- .../src/com/struts/LoginAction.java | 71 ++++--- .../src/com/struts/Struts.java | 91 ++++---- .../src/com/struts/View.java | 20 +- .../src/testcase/StrutsTest.java | 66 +++--- .../src/testcase/TestArrayUtil.java | 55 ++--- 7 files changed, 326 insertions(+), 266 deletions(-) diff --git a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java index 86ba46d8cd..9b4b81c12d 100644 --- a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java +++ b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java @@ -88,11 +88,10 @@ public int[] merge(int[] array1, int[] array2) int index1 = 0; int index2 = 0; int index3 = 0; - + while (index1 < length1 && index2 < length2) { - - + if (index3 > 0) { if (array3[index3 - 1] == array1[index1]) @@ -106,7 +105,7 @@ public int[] merge(int[] array1, int[] array2) continue; } } - + if (array1[index1] == array2[index2]) { array3[index3++] = array1[index1]; @@ -128,13 +127,15 @@ public int[] merge(int[] array1, int[] array2) } } - while (index1 < length1) { + while (index1 < length1) + { array3[index3++] = array1[index1++]; } - while (index2 < length2) { + while (index2 < length2) + { array3[index3++] = array1[index2++]; } - + int[] newArray = new int[index3]; for (int i = 0; i < index3; i++) { @@ -157,10 +158,12 @@ public int[] grow(int[] oldArray, int size) { int length = oldArray.length; int[] newArr = new int[length + size]; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) + { newArr[i] = oldArray[i]; } - for (int i = length; i < length + size; i++) { + for (int i = length; i < length + size; i++) + { newArr[i] = 0; } return newArr; @@ -175,30 +178,33 @@ public int[] grow(int[] oldArray, int size) */ public int[] fibonacci(int max) { - if (max == 1) { + if (max == 1) + { return null; } int[] arr = new int[max / 2]; int a = 1; int b = 1; int c = 0; - + arr[0] = 1; arr[1] = 1; - + int index = 2; - while (a + b < max) { + while (a + b < max) + { arr[index++] = a + b; c = b; b = a + b; a = c; } - + int[] newArr = new int[index]; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index; i++) + { newArr[i] = arr[i]; } - + return newArr; } @@ -213,23 +219,29 @@ public int[] getPrimes(int max) int size = max / 2 + 1; int[] arr = new int[size]; int index = 0; - for (int i = 2; i < max; i++) { - if (isPrime(i)) { + for (int i = 2; i < max; i++) + { + if (isPrime(i)) + { arr[index++] = i; } } - + int[] newArr = new int[index]; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index; i++) + { newArr[i] = arr[i]; } - + return newArr; } - - public boolean isPrime(int i) { - for (int j = 2; j < i / 2; j++) { - if (i % j == 0) { + + public boolean isPrime(int i) + { + for (int j = 2; j < i / 2; j++) + { + if (i % j == 0) + { return false; } } @@ -247,28 +259,34 @@ public int[] getPerfectNumbers(int max) int size = max / 2 + 1; int[] arr = new int[size]; int index = 0; - for (int i = 2; i < max; i++) { - if (isPerfectNumber(i)) { + for (int i = 2; i < max; i++) + { + if (isPerfectNumber(i)) + { arr[index++] = i; } } - + int[] newArr = new int[index]; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index; i++) + { newArr[i] = arr[i]; } - + return newArr; } - - public boolean isPerfectNumber(int num) { + + public boolean isPerfectNumber(int num) + { int sum = 0; - for (int i = 1; i <= num / 2; i++) { - if (num % i == 0) { + for (int i = 1; i <= num / 2; i++) + { + if (num % i == 0) + { sum += i; } } -// System.out.println("num: " + num + ", sum:" + sum); + // System.out.println("num: " + num + ", sum:" + sum); return (num == sum); } @@ -283,10 +301,11 @@ public String join(int[] array, String seperator) { String str = ""; int length = array.length; - for (int a : array) { + for (int a : array) + { str += a + seperator; } - str = str.substring(0, str.length()-1); + str = str.substring(0, str.length() - 1); return str; } diff --git a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java index 507a974297..31d27b592c 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java +++ b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java @@ -7,99 +7,115 @@ import java.util.List; import java.util.Map; -import org.dom4j.Attribute; -import org.dom4j.Document; +import org.dom4j.Attribute; +import org.dom4j.Document; import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; /** * DOM方式解析xml */ -public class DomXmlHelper { - - private Map kv = new HashMap(); - - public DomXmlHelper() throws DocumentException { +public class DomXmlHelper +{ + + private Map kv = new HashMap(); + + public DomXmlHelper () throws DocumentException + { fetchAttributes(); - } - - //遍历当前节点下的所有节点 - private void listNodes(Element node, String loop, Map kv){ - -// System.out.println("当前节点的名称:" + node.getName()); - if (loop.equals("")) { - loop += node.getName(); - } else { - kv.put(loop, node.getName()); - loop += "-" + node.getName(); - } - - //首先获取当前节点的所有属性节点 - List list = node.attributes(); - //遍历属性节点 - for(Attribute attribute : list){ -// System.out.println("属性 "+attribute.getName() +":" + attribute.getValue()); - kv.put(loop, attribute.getValue()); - loop += "-" + attribute.getValue(); -// System.out.println("loop: " + loop); - } - - //如果当前节点内容不为空,则输出 - if(!(node.getTextTrim().equals(""))){ -// System.out.println("内容 " + node.getName() + ":" + node.getText()); - kv.put(loop, node.getText()); - loop += "-" + node.getText(); -// System.out.println("loop: " + loop); - } - - - //同时迭代当前节点下面的所有子节点 - //使用递归 - Iterator iterator = node.elementIterator(); - while(iterator.hasNext()){ - Element e = iterator.next(); - listNodes(e, loop, kv); - } - } - - private void fetchAttributes() throws DocumentException { - //创建SAXReader对象 - SAXReader reader = new SAXReader(); - //读取文件 转换成Document - Document document = (Document) reader.read(new File("./src/com/struts/struts.xml")); - - //获取根节点元素对象 - Element root = ((org.dom4j.Document) document).getRootElement(); - - listNodes(root, "", kv); - - for (Map.Entry entity : kv.entrySet()) { -// System.out.println("key: " + entity.getKey() + " , value: " + entity.getValue()); - } - } - - public String getActionView(String action, String method) { - String key = "struts-action-" + action; - String className = kv.get(key); - key += "-" + className + "-result-" + method; - return kv.get(key); - } - - public String getActionClassByName(String action) { - String key = "struts-action-" + action; - return kv.get(key); - } - -// public static void main(String[] args) throws DocumentException { -// DomXmlHelper dm = new DomXmlHelper(); -// System.out.println(dm.getActionClassByName("login")); -// System.out.println(dm.getActionView("login", "success")); -// System.out.println(dm.getActionView("login", "fail")); -// -// System.out.println(dm.getActionClassByName("logout")); -// System.out.println(dm.getActionView("logout", "success")); -// System.out.println(dm.getActionView("logout", "error")); -// } - -} \ No newline at end of file + } + + // 遍历当前节点下的所有节点 + private void listNodes(Element node, String loop, Map kv) + { + + // System.out.println("当前节点的名称:" + node.getName()); + if (loop.equals("")) + { + loop += node.getName(); + } + else + { + kv.put(loop, node.getName()); + loop += "-" + node.getName(); + } + + // 首先获取当前节点的所有属性节点 + List list = node.attributes(); + // 遍历属性节点 + for (Attribute attribute : list) + { + // System.out.println("属性 "+attribute.getName() +":" + + // attribute.getValue()); + kv.put(loop, attribute.getValue()); + loop += "-" + attribute.getValue(); + // System.out.println("loop: " + loop); + } + + // 如果当前节点内容不为空,则输出 + if (!(node.getTextTrim().equals(""))) + { + // System.out.println("内容 " + node.getName() + ":" + + // node.getText()); + kv.put(loop, node.getText()); + loop += "-" + node.getText(); + // System.out.println("loop: " + loop); + } + + // 同时迭代当前节点下面的所有子节点 + // 使用递归 + Iterator iterator = node.elementIterator(); + while (iterator.hasNext()) + { + Element e = iterator.next(); + listNodes(e, loop, kv); + } + } + + private void fetchAttributes() throws DocumentException + { + // 创建SAXReader对象 + SAXReader reader = new SAXReader(); + // 读取文件 转换成Document + Document document = (Document) reader.read(new File( + "./src/com/struts/struts.xml")); + + // 获取根节点元素对象 + Element root = ((org.dom4j.Document) document).getRootElement(); + + listNodes(root, "", kv); + + for (Map.Entry entity : kv.entrySet()) + { + // System.out.println("key: " + entity.getKey() + " , value: " + + // entity.getValue()); + } + } + + public String getActionView(String action, String method) + { + String key = "struts-action-" + action; + String className = kv.get(key); + key += "-" + className + "-result-" + method; + return kv.get(key); + } + + public String getActionClassByName(String action) + { + String key = "struts-action-" + action; + return kv.get(key); + } + + // public static void main(String[] args) throws DocumentException { + // DomXmlHelper dm = new DomXmlHelper(); + // System.out.println(dm.getActionClassByName("login")); + // System.out.println(dm.getActionView("login", "success")); + // System.out.println(dm.getActionView("login", "fail")); + // + // System.out.println(dm.getActionClassByName("logout")); + // System.out.println(dm.getActionView("logout", "success")); + // System.out.println(dm.getActionView("logout", "error")); + // } + +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java index 24e01dab79..6d1d5a2ca6 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java +++ b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java @@ -2,39 +2,50 @@ /** * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author - * + * + * @author + * */ -public class LoginAction{ - private String name ; - private String password; - private String message; +public class LoginAction +{ + private String name; + private String password; + private String message; - public String getName() { - return name; - } + public String getName() + { + return name; + } - public String getPassword() { - return password; - } + public String getPassword() + { + return password; + } - public String execute(){ - System.out.println("name: " + name + " , password: " + password); - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } + public String execute() + { + System.out.println("name: " + name + " , password: " + password); + 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; - } + 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/group24/1525619747/homework_20170319/src/com/struts/Struts.java b/group24/1525619747/homework_20170319/src/com/struts/Struts.java index 1750ce2018..f0ff145909 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/Struts.java +++ b/group24/1525619747/homework_20170319/src/com/struts/Struts.java @@ -15,7 +15,8 @@ public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException, NoSuchFieldException + IllegalArgumentException, InvocationTargetException, + NoSuchFieldException { /* @@ -34,55 +35,57 @@ public static View runAction(String actionName, * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, * 放到View对象的jsp字段中。 */ - + DomXmlHelper dx = new DomXmlHelper(); String action = "login"; String className = dx.getActionClassByName(action); -// System.out.println(className); - + // System.out.println(className); + Class class1 = null; class1 = Class.forName(className); -// System.out.println("类名称 " + class1.getName()); - - if (class1 != null) { - // 调用class1的setName方法, 待参数 - //根据.class反射出来的类实例 - Object instance = class1.newInstance(); - + // System.out.println("类名称 " + class1.getName()); + + if (class1 != null) + { + // 调用class1的setName方法, 待参数 + // 根据.class反射出来的类实例 + Object instance = class1.newInstance(); + Method method = class1.getMethod("setName", String.class); - Object res1 =method.invoke(instance, "test"); - - method = class1.getMethod("setPassword", String.class); - Object res2 = method.invoke(instance, "1234"); - - // set attr - for (Map.Entry entity : parameters.entrySet()) { - String attrName = entity.getKey(); - String attrValue = entity.getValue(); - - Field idF = class1.getDeclaredField(attrName); // 获取属性 - idF.setAccessible(true); // 使用反射机制可以打破封装性,导致了java对象的属性不安全。 - idF.set(instance, attrValue); // set - } - - View view = new View(); - - method = class1.getMethod("execute"); - Object res3 = method.invoke(instance); -// System.out.println(res3); - String jsp = dx.getActionView(action, res3.toString()); - view.setJsp(jsp); - - method = class1.getMethod("getMessage"); - Object res4 = method.invoke(instance); -// System.out.println(res4); - - Map map = new HashMap(); - map.put("message", res4.toString()); - - view.setParameters(map); - - return view; + Object res1 = method.invoke(instance, "test"); + + method = class1.getMethod("setPassword", String.class); + Object res2 = method.invoke(instance, "1234"); + + // set attr + for (Map.Entry entity : parameters.entrySet()) + { + String attrName = entity.getKey(); + String attrValue = entity.getValue(); + + Field idF = class1.getDeclaredField(attrName); // 获取属性 + idF.setAccessible(true); // 使用反射机制可以打破封装性,导致了java对象的属性不安全。 + idF.set(instance, attrValue); // set + } + + View view = new View(); + + method = class1.getMethod("execute"); + Object res3 = method.invoke(instance); + // System.out.println(res3); + String jsp = dx.getActionView(action, res3.toString()); + view.setJsp(jsp); + + method = class1.getMethod("getMessage"); + Object res4 = method.invoke(instance); + // System.out.println(res4); + + Map map = new HashMap(); + map.put("message", res4.toString()); + + view.setParameters(map); + + return view; } return null; diff --git a/group24/1525619747/homework_20170319/src/com/struts/View.java b/group24/1525619747/homework_20170319/src/com/struts/View.java index 5d6a6d9d1f..b5b9e2802b 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/View.java +++ b/group24/1525619747/homework_20170319/src/com/struts/View.java @@ -2,21 +2,29 @@ import java.util.Map; -public class View { +public class View +{ private String jsp; private Map parameters; - - public String getJsp() { + + public String getJsp() + { return jsp; } - public View setJsp(String jsp) { + + 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; } diff --git a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java index 8fd79a3e4e..61ef07748f 100644 --- a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java +++ b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java @@ -1,57 +1,55 @@ package testcase; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; - - - - - import org.dom4j.DocumentException; import org.junit.Assert; import org.junit.Test; -import com.struts.DomXmlHelper; import com.struts.Struts; import com.struts.View; - - -public class StrutsTest { +public class StrutsTest +{ @Test - public void testLoginActionSuccess() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { - + public void testLoginActionSuccess() throws ClassNotFoundException, + NoSuchMethodException, SecurityException, InstantiationException, + IllegalAccessException, IllegalArgumentException, + InvocationTargetException, DocumentException, NoSuchFieldException + { + 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")); + + 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() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { + public void testLoginActionFailed() throws ClassNotFoundException, + NoSuchMethodException, SecurityException, InstantiationException, + IllegalAccessException, IllegalArgumentException, + InvocationTargetException, DocumentException, NoSuchFieldException + { 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")); + 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/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java index 6579308478..fffa56eac1 100644 --- a/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java +++ b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java @@ -69,78 +69,83 @@ public void testMerge() int[] a1 = { 3, 5, 7, 8 }; int[] a2 = { 4, 5, 6, 7 }; - + int[] newArr = arrayUtil.merge(a1, a2); -// print_r(newArr); + // print_r(newArr); assertTrue(newArr[0] == 3); assertTrue(newArr[2] == 5); assertTrue(newArr[3] == 6); assertTrue(newArr[5] == 8); } - + @Test - public void testGrow() { + public void testGrow() + { ArrayUtil arrayUtil = new ArrayUtil(); int[] a1 = { 3, 5, 7, 8 }; a1 = arrayUtil.grow(a1, 3); -// print_r(a1); + // print_r(a1); assertTrue(a1[0] == 3); assertTrue(a1[2] == 7); assertTrue(a1[3] == 8); assertTrue(a1[4] == 0); assertTrue(a1[5] == 0); assertTrue(a1[6] == 0); - + } - + @Test - public void testFibonacci() { + public void testFibonacci() + { ArrayUtil arrayUtil = new ArrayUtil(); int max = 100; int[] arr = arrayUtil.fibonacci(max); -// print_r(arr); - + // print_r(arr); + assertNotNull(arr); int index = (int) (Math.random() * arr.length); assertTrue(arr[index] < max); - + arr = arrayUtil.fibonacci(1); assertNull(arr); } - + @Test - public void testGetPrimes() { + public void testGetPrimes() + { ArrayUtil arrayUtil = new ArrayUtil(); int max = 23; int[] arr = arrayUtil.getPrimes(max); -// print_r(arr); - + // print_r(arr); + int index = (int) (Math.random() * arr.length); assertTrue(arr[index] < max); assertTrue(arrayUtil.isPrime(arr[index])); - + } - + @Test - public void testGetPerfectNumbers() { + public void testGetPerfectNumbers() + { ArrayUtil arrayUtil = new ArrayUtil(); int max = 300; int[] arr = arrayUtil.getPerfectNumbers(max); -// print_r(arr); - + // print_r(arr); + int index = (int) (Math.random() * arr.length); assertTrue(arr[index] < max); assertTrue(arrayUtil.isPerfectNumber(arr[index])); - + } - + @Test - public void testJoin() { + public void testJoin() + { ArrayUtil arrayUtil = new ArrayUtil(); - int[] a = {3,8,9}; + int[] a = { 3, 8, 9 }; String str = arrayUtil.join(a, "-"); -// System.out.println(str); + // System.out.println(str); assertTrue(str.equals("3-8-9")); } From bc25d5ef3383d1077502de7b6019394d91abce1b Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 21:00:45 +0800 Subject: [PATCH 5/6] add BinaryTreeNode and testcase --- .../src/com/basic/BinaryTreeNode.java | 45 ++++++++++++++++++- .../src/testcase/TestBinaryTreeNode.java | 24 ++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java index 9cf6755972..71c313b68a 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -6,6 +6,18 @@ public class BinaryTreeNode private Object data; private BinaryTreeNode left; private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } public Object getData() { @@ -37,9 +49,40 @@ public void setRight(BinaryTreeNode right) this.right = right; } + /* + * 排序二叉树的插入 + * */ public BinaryTreeNode insert(Object o) { - return null; + if ( ((Integer) data) > ((Integer) o) ) { + if (left == null) { + setLeft(new BinaryTreeNode(o)); + } else { + left.insert(o); + } + } else { + if (right == null) { + setRight(new BinaryTreeNode(o)); + } else { + right.insert(o); + } + } + return this; + } + + /* + * 前序遍历 + * */ + public void preOrderInterator() { + if (left != null) { + left.preOrderInterator(); + } + + System.out.print(data.toString() + " "); + + if (right != null) { + right.preOrderInterator(); + } } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java new file mode 100644 index 0000000000..6434d6880f --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java @@ -0,0 +1,24 @@ +package testcase; + +import org.junit.Test; + +import com.basic.BinaryTreeNode; + +public class TestBinaryTreeNode +{ + @Test + public void testBinaryTree() { + BinaryTreeNode binNode = new BinaryTreeNode(5); + binNode.insert(1); + binNode.insert(10); + binNode.insert(4); + binNode.insert(6); + binNode.insert(2); + binNode.insert(15); + binNode.insert(8); + + //1 2 4 5 6 8 10 15 + binNode.preOrderInterator(); + } + +} From bec6ad33f840836be1b352edb542477c2c4ada1b Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 21:01:31 +0800 Subject: [PATCH 6/6] format codes --- .../src/com/basic/ArrayList.java | 2 +- .../src/com/basic/BinaryTreeNode.java | 57 ++-- .../src/com/basic/LinkedList.java | 243 +++++++++----- .../src/testcase/TestArrayList.java | 28 +- .../src/testcase/TestBinaryTreeNode.java | 9 +- .../src/testcase/TestLinkedList.java | 310 ++++++++++-------- .../src/testcase/TestQueue.java | 14 +- .../src/testcase/TestStack.java | 16 +- 8 files changed, 398 insertions(+), 281 deletions(-) diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java index bb540052cd..008b390255 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -54,7 +54,7 @@ public Object remove(int index) + " is invalid, current range: 0 - " + (size - 1); throw new ArrayIndexOutOfBoundsException(errorInfo); } - + Object o = elementData[index]; for (int i = index; i < size - 1; i++) { diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java index 71c313b68a..63899d38ce 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -6,14 +6,17 @@ public class BinaryTreeNode private Object data; private BinaryTreeNode left; private BinaryTreeNode right; - - public BinaryTreeNode(Object data) { + + public BinaryTreeNode (Object data) + { this.data = data; left = null; right = null; } - - public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + + public BinaryTreeNode (Object data, BinaryTreeNode left, + BinaryTreeNode right) + { this.data = data; this.left = left; this.right = right; @@ -51,38 +54,50 @@ public void setRight(BinaryTreeNode right) /* * 排序二叉树的插入 - * */ + */ public BinaryTreeNode insert(Object o) { - if ( ((Integer) data) > ((Integer) o) ) { - if (left == null) { + if (((Integer) data) > ((Integer) o)) + { + if (left == null) + { setLeft(new BinaryTreeNode(o)); - } else { + } + else + { left.insert(o); - } - } else { - if (right == null) { + } + } + else + { + if (right == null) + { setRight(new BinaryTreeNode(o)); - } else { + } + else + { right.insert(o); - } + } } return this; } - + /* * 前序遍历 - * */ - public void preOrderInterator() { - if (left != null) { + */ + public void preOrderInterator() + { + if (left != null) + { left.preOrderInterator(); } - + System.out.print(data.toString() + " "); - - if (right != null) { + + if (right != null) + { right.preOrderInterator(); - } + } } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java index 2d9943e89b..e741421aad 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -7,11 +7,15 @@ public class LinkedList implements List public void add(Object o) { - if (head == null) { + if (head == null) + { head = new Node(o, null); - } else { + } + else + { Node nodePointer = head; - while (nodePointer.next != null) { + while (nodePointer.next != null) + { nodePointer = nodePointer.next; } nodePointer.next = new Node(o, null); @@ -22,13 +26,16 @@ public void add(Object o) public void add(int index, Object o) { int size = size(); - if (index < 0 || index > size) { - String errorInfo = "Invalid index to add:" + index+ " out of range: [0," + size + "]"; + if (index < 0 || index > size) + { + String errorInfo = "Invalid index to add:" + index + + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } int step = 0; Node nodePointer = head; - while (step < index) { + while (step < index) + { nodePointer = nodePointer.next; ++step; } @@ -38,62 +45,73 @@ public void add(int index, Object o) public Object get(int index) { int size = size(); - if (index < 0 || index > size) { - String errorInfo = "Invalid index to get:" + index+ " out of range: [0," + size + "]"; + if (index < 0 || index > size) + { + String errorInfo = "Invalid index to get:" + index + + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } - + int step = 0; Node nodePointer = head; - while (step < index) { + while (step < index) + { nodePointer = nodePointer.next; ++step; } - + return nodePointer.data; } public Object remove(int index) { int size = size(); - if (index < 0 || index > size) { - String errorInfo = "Invalid index to remove:" + index+ " out of range: [0," + size + "]"; + if (index < 0 || index > size) + { + String errorInfo = "Invalid index to remove:" + index + + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } - + int step = 0; Node nodePointer = head; Node lastPointer = head; - - while (step < index) { + + while (step < index) + { lastPointer = nodePointer; nodePointer = nodePointer.next; ++step; } - + Object o = null; - - if (lastPointer == nodePointer) { + + if (lastPointer == nodePointer) + { Node toDelete = head; o = toDelete.data; head = head.next; toDelete = null; - } else { + } + else + { o = nodePointer.data; lastPointer.next = nodePointer.next; nodePointer = null; - } - + } + return o; } public int size() { int size = 0; - if (head != null) { + if (head != null) + { ++size; Node nodePointer = head; - while (nodePointer.next != null) { + while (nodePointer.next != null) + { ++size; nodePointer = nodePointer.next; } @@ -103,7 +121,8 @@ public int size() public void addFirst(Object o) { - if (head == null) { + if (head == null) + { head = new Node(o, null); return; } @@ -112,13 +131,15 @@ public void addFirst(Object o) public void addLast(Object o) { - if (head == null) { + if (head == null) + { head = new Node(o, null); return; } - + Node nodePointer = head; - while (nodePointer.next != null) { + while (nodePointer.next != null) + { nodePointer = nodePointer.next; } nodePointer.next = new Node(o, null); @@ -127,44 +148,48 @@ public void addLast(Object o) @SuppressWarnings ("unused") public Object removeFirst() { - if (head == null) { + if (head == null) + { return null; } - + Node toDelete = head; Object o = head.data; head = head.next; toDelete = null; - + return o; } public Object removeLast() { - if (head == null) { + if (head == null) + { return null; } Node nodePointer = head; Node lastPointer = head; - - while (nodePointer.next != null) { + + while (nodePointer.next != null) + { lastPointer = nodePointer; nodePointer = nodePointer.next; } lastPointer.next = null; Object o = nodePointer.data; nodePointer = null; - + return o; } public Iterator iterator() { - return new Iterator() { - + return new Iterator() + { + private Node nodePointer = head; - + public boolean hasNext() { // TODO Auto-generated method stub @@ -174,13 +199,14 @@ public boolean hasNext() public Object next() { // TODO Auto-generated method stub - if (hasNext()) { + if (hasNext()) + { Object o = nodePointer.data; nodePointer = nodePointer.next; return o; } return null; - } + } }; } @@ -188,7 +214,9 @@ private static class Node { Object data; Node next; - public Node(Object o, Node n) { + + public Node (Object o, Node n) + { this.data = o; this.next = n; } @@ -199,19 +227,20 @@ public Node(Object o, Node n) { */ public void reverse() { - if (head == null) { + if (head == null) + { return; } - + Node reverse = null; - while (size() > 0) { + while (size() > 0) + { reverse = new Node(removeFirst(), reverse); } - + head = reverse; } - /** * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 * ,删除以后的值为7,8,10 @@ -222,7 +251,8 @@ public void removeFirstHalf() int removeLength = size() / 2; int step = 0; Node toDelete = null; - while (step < removeLength) { + while (step < removeLength) + { toDelete = head; head = head.next; toDelete = null; @@ -239,29 +269,36 @@ public void removeFirstHalf() public void remove(int i, int length) { int size = size(); - if (i >= size) { + if (i >= size) + { return; } Node nodePointer = head; Node lastPointer = head; int step = 0; - while (step < i) { + while (step < i) + { lastPointer = nodePointer; nodePointer = nodePointer.next; ++step; } - + step = 0; Node toDelete = null; - if (lastPointer == head) { - while (step < length) { + if (lastPointer == head) + { + while (step < length) + { toDelete = head; head = head.next; toDelete = null; ++step; } - } else { - while (step < length) { + } + else + { + while (step < length) + { toDelete = nodePointer; nodePointer = nodePointer.next; toDelete = null; @@ -269,7 +306,7 @@ public void remove(int i, int length) } lastPointer.next = nodePointer; } - + } /** @@ -284,7 +321,8 @@ public int[] getElements(LinkedList list) int[] elements = new int[list.size()]; Iterator it = list.iterator(); int index = 0; - for ( ; it.hasNext(); ) { + for (; it.hasNext();) + { elements[index++] = (Integer) get((Integer) (it.next())); } return elements; @@ -298,36 +336,45 @@ public int[] getElements(LinkedList list) public void subtract(LinkedList list) { - if (head == null) { + if (head == null) + { return; } Node nodePointer = head; Node lastPointer = head; Node toDelte = null; - while (nodePointer != null) { - if (list.contain(nodePointer.data)) { - if (nodePointer == head) { + while (nodePointer != null) + { + if (list.contain(nodePointer.data)) + { + if (nodePointer == head) + { toDelte = head; head = head.next; toDelte = null; nodePointer = head; lastPointer = head; - } else { + } + else + { toDelte = nodePointer; lastPointer.next = nodePointer.next; toDelte = null; } } lastPointer = nodePointer; - nodePointer = nodePointer.next; + nodePointer = nodePointer.next; } - + } - - private boolean contain(Object o) { + + private boolean contain(Object o) + { Node nodePointer = head; - while (nodePointer != null) { - if (nodePointer.data.equals(o)) { + while (nodePointer != null) + { + if (nodePointer.data.equals(o)) + { return true; } nodePointer = nodePointer.next; @@ -341,13 +388,17 @@ private boolean contain(Object o) { public void removeDuplicateValues() { Node nodePointer = head; - if (nodePointer.next == null) { + if (nodePointer.next == null) + { return; } - + Node toDelete = null; - while (nodePointer.next != null) { - while (nodePointer.next != null && nodePointer.data.equals(nodePointer.next.data)) { // delete nodePointer.next + while (nodePointer.next != null) + { + while (nodePointer.next != null + && nodePointer.data.equals(nodePointer.next.data)) + { // delete nodePointer.next toDelete = nodePointer.next; nodePointer.next = nodePointer.next.next; toDelete = null; @@ -366,22 +417,30 @@ public void removeRange(int min, int max) { Node nodePointer = head; Node lastPointer = head; - if (nodePointer == null) { + if (nodePointer == null) + { return; } - while (nodePointer != null && ((Integer) nodePointer.data) <= (new Integer(min))) { + while (nodePointer != null + && ((Integer) nodePointer.data) <= (new Integer(min))) + { lastPointer = nodePointer; nodePointer = nodePointer.next; } Node toDelete = null; - while (nodePointer != null && ((Integer) nodePointer.data) < (new Integer(max))) { - if (nodePointer == head) { + while (nodePointer != null + && ((Integer) nodePointer.data) < (new Integer(max))) + { + if (nodePointer == head) + { toDelete = head; head = head.next; toDelete = null; nodePointer = head; lastPointer = head; - } else { + } + else + { toDelete = nodePointer; lastPointer.next = nodePointer.next; nodePointer = nodePointer.next; @@ -403,28 +462,36 @@ public LinkedList intersection(LinkedList list) Iterator it2 = list.iterator(); Object o1 = null; Object o2 = null; - - if (size() == 0 || list.size() == 0) { + + if (size() == 0 || list.size() == 0) + { return null; } - + o1 = it1.next(); o2 = it2.next(); - - while (o1 != null && o2 != null) { -// System.out.println(o1 + " " + o2); - if (((Integer) o1) == ((Integer) o2)) { + + while (o1 != null && o2 != null) + { + // System.out.println(o1 + " " + o2); + if (((Integer) o1) == ((Integer) o2)) + { linkedList.add(o1); o1 = it1.next(); o2 = it2.next(); - } else { - if (((Integer) o1) > ((Integer) o2)) { + } + else + { + if (((Integer) o1) > ((Integer) o2)) + { o2 = it2.next(); - } else { + } + else + { o1 = it1.next(); } - } - } + } + } return linkedList; } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java index 4cf2bf1a62..134a541265 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -33,7 +33,7 @@ public void testAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } @@ -54,19 +54,19 @@ public void testIndexAdd() } list.add(3, 20); list.add(0, 30); - + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } - -// Iterator it = list.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); - + + // Iterator it = list.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + assertTrue(list.get(0).equals(30)); assertTrue(list.get(4).equals(20)); assertTrue(list.get(5).equals(3)); @@ -81,7 +81,7 @@ public void testIndexAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } } @@ -120,8 +120,8 @@ public void testGet() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); -// System.out.println(list.size()); + // System.out.println(e.getMessage()); + // System.out.println(list.size()); String errorInfo = "Index to get: 10 is invalid, current range: 0 - " + (list.size() - 1); assertEquals(errorInfo, e.getMessage()); @@ -159,7 +159,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); } try @@ -169,7 +169,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java index 6434d6880f..472b9a7fb0 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java @@ -7,7 +7,8 @@ public class TestBinaryTreeNode { @Test - public void testBinaryTree() { + public void testBinaryTree() + { BinaryTreeNode binNode = new BinaryTreeNode(5); binNode.insert(1); binNode.insert(10); @@ -16,9 +17,9 @@ public void testBinaryTree() { binNode.insert(2); binNode.insert(15); binNode.insert(8); - - //1 2 4 5 6 8 10 15 + + // 1 2 4 5 6 8 10 15 binNode.preOrderInterator(); } - + } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java index 54cf229718..05ac118a90 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -12,126 +12,149 @@ public class TestLinkedList /* * test add() and size() - * */ - @Test - public void testAdd() { + */ + @Test + public void testAdd() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); assertTrue(linkedList.size() == 2); } - @Test - public void testGet() { + public void testGet() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); assertTrue(linkedList.get(0).equals(3)); assertFalse(linkedList.get(0).equals("hello world")); assertTrue(linkedList.get(1).equals("hello world")); - - try { + + try + { linkedList.get(-1); - } catch (ArrayIndexOutOfBoundsException e) { + } + catch (ArrayIndexOutOfBoundsException e) + { assertNotNull(e); - String errorInfo = "Invalid index to get:" + -1 + " out of range: [0," + linkedList.size() + "]"; + String errorInfo = "Invalid index to get:" + -1 + + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); } } - @Test - public void testRemove() { + public void testRemove() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); linkedList.add(4); linkedList.add("Leon Deng"); - try { + try + { linkedList.remove(-1); - } catch (ArrayIndexOutOfBoundsException e) { + } + catch (ArrayIndexOutOfBoundsException e) + { assertNotNull(e); - String errorInfo = "Invalid index to remove:" + -1 + " out of range: [0," + linkedList.size() + "]"; + String errorInfo = "Invalid index to remove:" + -1 + + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); } - - try { + + try + { linkedList.remove(10); - } catch (ArrayIndexOutOfBoundsException e) { + } + catch (ArrayIndexOutOfBoundsException e) + { assertNotNull(e); - String errorInfo = "Invalid index to remove:" + 10 + " out of range: [0," + linkedList.size() + "]"; + String errorInfo = "Invalid index to remove:" + 10 + + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); } - - Object o = null; - try { + + Object o = null; + try + { o = linkedList.remove(0); - } catch (ArrayIndexOutOfBoundsException e) { - assertNull(e); - } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } assertTrue(o.equals(3)); - - try { + + try + { o = linkedList.remove(2); - } catch (ArrayIndexOutOfBoundsException e) { - assertNull(e); - } -// System.out.println(o); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + // System.out.println(o); assertTrue(o.equals("Leon Deng")); } - @Test - public void testAddFirst() { + public void testAddFirst() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); - + linkedList.addFirst("Leon Deng"); assertTrue(linkedList.get(0).equals("Leon Deng")); assertTrue(linkedList.get(1).equals(3)); } @Test - public void testAddLast() { + public void testAddLast() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); - + linkedList.addLast("Leon Deng"); assertTrue(linkedList.get(linkedList.size() - 1).equals("Leon Deng")); } - + @Test - public void testRemoveFirst() { + public void testRemoveFirst() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); - + Object o = linkedList.removeFirst(); assertTrue(o.equals(3)); o = linkedList.removeFirst(); assertTrue(o.equals("hello world")); } - + @Test - public void testRemoveLast() { + public void testRemoveLast() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); linkedList.add("Leon Deng"); - + Object o = linkedList.removeLast(); assertTrue(o.equals("Leon Deng")); o = linkedList.removeLast(); assertTrue(o.equals("hello world")); } - + @Test - public void testInterator() { + public void testInterator() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("Leon Deng"); @@ -143,78 +166,81 @@ public void testInterator() { assertTrue(it.next().equals("Leon Deng")); assertFalse(it.hasNext()); } - + @Test - public void testReverse() { + public void testReverse() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add(4); linkedList.add(5); linkedList.add(6); -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); -// -// linkedList.reverse(); -// -// it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); - + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + // + // linkedList.reverse(); + // + // it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + Object o1 = linkedList.get(0); Object o2 = linkedList.get(linkedList.size() - 1); - + linkedList.reverse(); - + Object o3 = linkedList.get(0); Object o4 = linkedList.get(linkedList.size() - 1); assertEquals(o1, o4); assertEquals(o2, o3); - + linkedList.reverse(); Object o5 = linkedList.get(0); Object o6 = linkedList.get(linkedList.size() - 1); - + assertEquals(o1, o5); assertEquals(o2, o6); } - + @Test - public void testRemoveFirstHalf() { + public void testRemoveFirstHalf() + { LinkedList linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); linkedList.add(7); linkedList.add(8); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); - + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + linkedList.removeFirstHalf(); assertTrue(linkedList.get(0).equals(7)); - + linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); linkedList.add(7); linkedList.add(8); linkedList.add(10); - + linkedList.removeFirstHalf(); assertTrue(linkedList.get(2).equals(10)); } - + @Test - public void testRemoveIndexLength() { + public void testRemoveIndexLength() + { LinkedList linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); @@ -222,15 +248,15 @@ public void testRemoveIndexLength() { linkedList.add(8); linkedList.add(10); - linkedList.remove(0,2); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + linkedList.remove(0, 2); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(linkedList.get(0).equals(7)); - + linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); @@ -238,15 +264,16 @@ public void testRemoveIndexLength() { linkedList.add(8); linkedList.add(10); - linkedList.remove(2,2); + linkedList.remove(2, 2); assertTrue(linkedList.get(0).equals(2)); assertTrue(linkedList.get(2).equals(10)); - + } - + @Test - public void testGetElements() { -// 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + public void testGetElements() + { + // 11->101->201->301->401->501->601->701 listB = 1->3->4->6 LinkedList linkedList = new LinkedList(); linkedList.add(11); linkedList.add(101); @@ -256,29 +283,29 @@ public void testGetElements() { linkedList.add(501); linkedList.add(601); linkedList.add(701); - + LinkedList indexLinkedList = new LinkedList(); indexLinkedList.add(1); indexLinkedList.add(3); indexLinkedList.add(4); indexLinkedList.add(6); - + int[] elements = linkedList.getElements(indexLinkedList); - -// for (int i = 0; i < elements.length; i++) { -// System.out.print(elements[i] + " "); -// } -// System.out.println(); + + // for (int i = 0; i < elements.length; i++) { + // System.out.print(elements[i] + " "); + // } + // System.out.println(); assertEquals(elements[0], linkedList.get(1)); assertEquals(elements[1], linkedList.get(3)); assertEquals(elements[2], linkedList.get(4)); - assertEquals(elements[3], linkedList.get(6)); + assertEquals(elements[3], linkedList.get(6)); } - - + @Test - public void testSubstract() { + public void testSubstract() + { LinkedList linkedList = new LinkedList(); linkedList.add(11); linkedList.add(101); @@ -288,7 +315,6 @@ public void testSubstract() { linkedList.add(501); linkedList.add(601); linkedList.add(701); - LinkedList substractList = new LinkedList(); substractList.add(11); @@ -296,21 +322,22 @@ public void testSubstract() { substractList.add(404); substractList.add(501); substractList.add(701); - + linkedList.subtract(substractList); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertFalse(linkedList.get(0).equals(11)); assertTrue(linkedList.get(0).equals(101)); } - + @Test - public void testRemoveDuplicateValues() { + public void testRemoveDuplicateValues() + { LinkedList linkedList = new LinkedList(); linkedList.add(1); linkedList.add(1); @@ -320,41 +347,44 @@ public void testRemoveDuplicateValues() { linkedList.add(4); linkedList.add(4); linkedList.add(5); - + linkedList.removeDuplicateValues(); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(linkedList.get(0).equals(1)); assertTrue(linkedList.get(1).equals(2)); assertTrue(linkedList.get(4).equals(5)); - + } - + @Test - public void testRemoveRange() { + public void testRemoveRange() + { LinkedList linkedList = new LinkedList(); - for (int i = 0; i < 10; i++) { - linkedList.add(i); + for (int i = 0; i < 10; i++) + { + linkedList.add(i); } linkedList.removeRange(3, 7); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(linkedList.get(3).equals(3)); - assertTrue(linkedList.get(4).equals(7)); + assertTrue(linkedList.get(4).equals(7)); } - + @Test - public void testIntersection() { + public void testIntersection() + { LinkedList linkedList = new LinkedList(); linkedList.add(1); linkedList.add(2); @@ -362,23 +392,23 @@ public void testIntersection() { linkedList.add(4); linkedList.add(5); linkedList.add(6); - + LinkedList secondList = new LinkedList(); secondList.add(1); secondList.add(3); secondList.add(5); secondList.add(6); - + LinkedList intersection = linkedList.intersection(secondList); -// Iterator it = intersection.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + // Iterator it = intersection.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(intersection.get(0).equals(1)); assertTrue(intersection.get(1).equals(3)); assertTrue(intersection.get(2).equals(5)); assertTrue(intersection.get(3).equals(6)); } - + } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java index a08ee80951..390238efc9 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java @@ -10,17 +10,19 @@ public class TestQueue { /* * test enQueue() isEmpty() and size() deQueue - * */ + */ @Test - public void testQueue() { + public void testQueue() + { Queue queue = new Queue(); - + assertTrue(queue.isEmpty()); - - for (int i = 10; i < 20; i++) { + + for (int i = 10; i < 20; i++) + { queue.enQueue(i); } - + assertFalse(queue.isEmpty()); assertTrue(queue.size() == 10); diff --git a/group24/1525619747/homework_20170312/src/testcase/TestStack.java b/group24/1525619747/homework_20170312/src/testcase/TestStack.java index 58c7206fea..2091577ec1 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestStack.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestStack.java @@ -9,21 +9,23 @@ public class TestStack { @Test - public void testStack() { + public void testStack() + { Stack stack = new Stack(); - + assertTrue(stack.isEmpty()); - - for (int i = 10; i < 20; i++) { + + for (int i = 10; i < 20; i++) + { stack.push(i); } - + assertFalse(stack.isEmpty()); assertTrue(stack.size() == 10); - + assertTrue(stack.peek().equals(19)); assertFalse(stack.peek().equals(10)); - + assertTrue(stack.pop().equals(19)); assertTrue(stack.pop().equals(18)); }