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_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java new file mode 100644 index 0000000000..008b390255 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -0,0 +1,95 @@ +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 && size > 0) + { + String errorInfo = "Index to add: " + index + + " is out of current size: " + size; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + for (int i = size; i > index; 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); + } + + Object o = elementData[index]; + for (int i = index; i < size - 1; i++) + { + elementData[i] = elementData[i + 1]; + } + 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..63899d38ce --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -0,0 +1,103 @@ +package com.basic; + +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() + { + 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) + { + 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/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..e741421aad --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -0,0 +1,498 @@ +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); + } + + @SuppressWarnings ("unused") + 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 + */ + @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; + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + 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; + } + + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) + { + 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; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param 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; + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + 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; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + 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; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) + { + 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/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..b040c85722 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Queue.java @@ -0,0 +1,26 @@ +package com.basic; + +public class Queue +{ + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) + { + list.addLast(o); + } + + public Object deQueue() + { + return list.removeFirst(); + } + + public boolean isEmpty() + { + return (list.size() == 0); + } + + public int size() + { + 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 new file mode 100644 index 0000000000..db7ada4c53 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Stack.java @@ -0,0 +1,31 @@ +package com.basic; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + elementData.add(0, o); + } + + public Object pop() + { + return elementData.remove(0); + } + + public Object peek() + { + return elementData.get(0); + } + + public boolean isEmpty() + { + return (elementData.size() == 0); + } + + public int size() + { + return elementData.size(); + } +} 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..134a541265 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -0,0 +1,199 @@ +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); + } + assertTrue(list.get(0).equals(3)); + 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); + list.add(0, 30); + + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + + // 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 + { + 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/TestBinaryTreeNode.java b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java new file mode 100644 index 0000000000..472b9a7fb0 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java @@ -0,0 +1,25 @@ +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(); + } + +} 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..05ac118a90 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -0,0 +1,414 @@ +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); + } + + @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..390238efc9 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java @@ -0,0 +1,33 @@ +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..2091577ec1 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestStack.java @@ -0,0 +1,33 @@ +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)); + } + +} 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..9b4b81c12d --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java @@ -0,0 +1,312 @@ +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..31d27b592c --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java @@ -0,0 +1,121 @@ +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")); + // } + +} 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..6d1d5a2ca6 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java @@ -0,0 +1,51 @@ +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..f0ff145909 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/Struts.java @@ -0,0 +1,94 @@ +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..b5b9e2802b --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/View.java @@ -0,0 +1,31 @@ +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..61ef07748f --- /dev/null +++ b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java @@ -0,0 +1,55 @@ +package testcase; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +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..fffa56eac1 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java @@ -0,0 +1,152 @@ +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")); + } + +}