From 36e35f91fba997cc2291f480be989acc3f228db0 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:11:15 +0800 Subject: [PATCH 01/19] Create week1 --- group26/1515345281/src/week1 | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week1 diff --git a/group26/1515345281/src/week1 b/group26/1515345281/src/week1 new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week1 @@ -0,0 +1 @@ + From 597e0e024fa5aaa671b1bc4e487eaec688e1d8a8 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:11:40 +0800 Subject: [PATCH 02/19] Delete week1 --- group26/1515345281/src/week1 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group26/1515345281/src/week1 diff --git a/group26/1515345281/src/week1 b/group26/1515345281/src/week1 deleted file mode 100644 index 8b13789179..0000000000 --- a/group26/1515345281/src/week1 +++ /dev/null @@ -1 +0,0 @@ - From 9555d309af905888d23cf28e264b30a1eea5427e Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:14:14 +0800 Subject: [PATCH 03/19] Create ww --- group26/1515345281/src/week1/ww | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week1/ww diff --git a/group26/1515345281/src/week1/ww b/group26/1515345281/src/week1/ww new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week1/ww @@ -0,0 +1 @@ + From f353cfa1372e58da5b1245ef14d4aa233c8522bb Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:14:32 +0800 Subject: [PATCH 04/19] Create week2 --- group26/1515345281/src/week2 | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week2 diff --git a/group26/1515345281/src/week2 b/group26/1515345281/src/week2 new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week2 @@ -0,0 +1 @@ + From 4b40386be291b13473d13cdd8be31b9e52b093a1 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:14:49 +0800 Subject: [PATCH 05/19] Delete week2 --- group26/1515345281/src/week2 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group26/1515345281/src/week2 diff --git a/group26/1515345281/src/week2 b/group26/1515345281/src/week2 deleted file mode 100644 index 8b13789179..0000000000 --- a/group26/1515345281/src/week2 +++ /dev/null @@ -1 +0,0 @@ - From bd8f374ab06c401a7f7b1cffbd77f265b7087ca0 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:15:06 +0800 Subject: [PATCH 06/19] Create 11 --- group26/1515345281/src/week2/11 | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week2/11 diff --git a/group26/1515345281/src/week2/11 b/group26/1515345281/src/week2/11 new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week2/11 @@ -0,0 +1 @@ + From fcba1ed913a27991fe8a69592908b9d10a03c602 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:15:16 +0800 Subject: [PATCH 07/19] Delete 11 --- group26/1515345281/src/week2/11 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group26/1515345281/src/week2/11 diff --git a/group26/1515345281/src/week2/11 b/group26/1515345281/src/week2/11 deleted file mode 100644 index 8b13789179..0000000000 --- a/group26/1515345281/src/week2/11 +++ /dev/null @@ -1 +0,0 @@ - From b195d8581045e43b0a8ec2b015ba2e726d81b909 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:15:37 +0800 Subject: [PATCH 08/19] Create qqq --- group26/1515345281/src/week2/qqq | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week2/qqq diff --git a/group26/1515345281/src/week2/qqq b/group26/1515345281/src/week2/qqq new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week2/qqq @@ -0,0 +1 @@ + From e2194be906b69ad8e7a5052b24f5a3ede2bbe88b Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:17:08 +0800 Subject: [PATCH 09/19] Create a --- group26/1515345281/src/week1/collection/test/a | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week1/collection/test/a diff --git a/group26/1515345281/src/week1/collection/test/a b/group26/1515345281/src/week1/collection/test/a new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week1/collection/test/a @@ -0,0 +1 @@ + From 3c26d9c2caf738a800ce862338b6c1db699bf6e1 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:17:29 +0800 Subject: [PATCH 10/19] Delete ww --- group26/1515345281/src/week1/ww | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group26/1515345281/src/week1/ww diff --git a/group26/1515345281/src/week1/ww b/group26/1515345281/src/week1/ww deleted file mode 100644 index 8b13789179..0000000000 --- a/group26/1515345281/src/week1/ww +++ /dev/null @@ -1 +0,0 @@ - From 843a85107e5752762588da832b212cf73b892f72 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:18:40 +0800 Subject: [PATCH 11/19] Add files via upload --- .../week1/collection/test/ArrayListTest.java | 69 ++++++++++++++ .../collection/test/BinarySearchTreeTest.java | 23 +++++ .../week1/collection/test/LinkedListTest.java | 90 +++++++++++++++++++ .../src/week1/collection/test/QueueTest.java | 27 ++++++ .../src/week1/collection/test/StackTest.java | 56 ++++++++++++ 5 files changed, 265 insertions(+) create mode 100644 group26/1515345281/src/week1/collection/test/ArrayListTest.java create mode 100644 group26/1515345281/src/week1/collection/test/BinarySearchTreeTest.java create mode 100644 group26/1515345281/src/week1/collection/test/LinkedListTest.java create mode 100644 group26/1515345281/src/week1/collection/test/QueueTest.java create mode 100644 group26/1515345281/src/week1/collection/test/StackTest.java diff --git a/group26/1515345281/src/week1/collection/test/ArrayListTest.java b/group26/1515345281/src/week1/collection/test/ArrayListTest.java new file mode 100644 index 0000000000..0fa7701041 --- /dev/null +++ b/group26/1515345281/src/week1/collection/test/ArrayListTest.java @@ -0,0 +1,69 @@ +package week1.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week1.collection.ArrayList; +import week1.collection.Iterator; + +public class ArrayListTest { + + private ArrayList list=new ArrayList(); + + @Test + public void testAddObject(){ + list.add(1); + assertEquals(1 , list.get(0)); + } + + @Test + public void testAddIndexObject(){ + list.add("aa"); + list.add("bb"); + list.add(0,"cc"); + assertEquals("cc",list.get(0)); + try{ + list.add(-1,"pp"); + fail("- can't be index"); + + list.add(list.size()+100,"bb"); + fail("index should <= size"); + + }catch(Exception ex){ + + } + } + + @Test + public void testGetObject(){ + list.add(1); + assertEquals(1,list.get(0)); + } + + @Test + public void testRemoveObject(){ + list.add(1); + list.add(2); + list.add(3); + list.remove(0); + list.remove(2); + assertEquals(2,list.get(0)); + } + + @Test + public void testSize(){ + assertEquals(0,list.size()); + } + + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + Iterator it=list.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + } +} diff --git a/group26/1515345281/src/week1/collection/test/BinarySearchTreeTest.java b/group26/1515345281/src/week1/collection/test/BinarySearchTreeTest.java new file mode 100644 index 0000000000..a6ea1cac3f --- /dev/null +++ b/group26/1515345281/src/week1/collection/test/BinarySearchTreeTest.java @@ -0,0 +1,23 @@ +package week1.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week1.collection.BinaryTreeNode; + +public class BinarySearchTreeTest { + + private BinaryTreeNode root=new BinaryTreeNode(5); + + @Test + public void testInsert(){ + root.insert(2); + root.insert(2); + root.insert(7); + root.insert(1); + root.insert(4); + root.insert(3); + assertEquals(3,root.getLeft().getRight().getLeft().getData()); + } +} diff --git a/group26/1515345281/src/week1/collection/test/LinkedListTest.java b/group26/1515345281/src/week1/collection/test/LinkedListTest.java new file mode 100644 index 0000000000..cba45c719d --- /dev/null +++ b/group26/1515345281/src/week1/collection/test/LinkedListTest.java @@ -0,0 +1,90 @@ +package week1.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week1.collection.Iterator; +import week1.collection.LinkedList; + +public class LinkedListTest { + + private LinkedList list=new LinkedList(); + + @Test + public void testAdd(){ + list.add("1"); + list.add("2"); + list.add("3"); + assertEquals("1",list.get(0)); + assertEquals("2",list.get(1)); + assertEquals(3,list.size()); + } + + @Test + public void testAddByIndex(){ + list.add(2); + list.add(4); + list.add(6); + list.add(0,0); + list.add(3,3); + list.add(5,7); + assertEquals(0, list.get(0)); + assertEquals(3, list.get(3)); + assertEquals(7, list.get(5)); + try{ + list.add(-1,0); + fail("-1 not a correctly index"); + }catch(Exception ex){ + + } + } + + @Test + public void testGet(){ + list.add(0); + list.add(1); + list.add(2); + assertEquals(0,list.get(0)); + } + + @Test + public void testRemove(){ + list.add(0); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + assertEquals(0,list.remove(0)); + assertEquals(4,list.remove(3)); + assertEquals(2,list.remove(1)); + } + + @Test + public void testSize(){ + list.add(0); + list.addLast(0); + list.addFirst(0); + list.remove(0); + list.removeLast(); + list.removeFirst(); + assertEquals(0,list.size()); + } + + @Test + public void testOther(){ + list.add(1); + list.add(1); + list.add(1); + list.add(1); + list.addFirst(0); + list.addLast(2); + list.removeFirst(); + list.removeLast(); + Iterator it=list.iterator(); + while(it.hasNext()){ + System.out.print(it.next()+" "); + } + } + +} diff --git a/group26/1515345281/src/week1/collection/test/QueueTest.java b/group26/1515345281/src/week1/collection/test/QueueTest.java new file mode 100644 index 0000000000..81979682a6 --- /dev/null +++ b/group26/1515345281/src/week1/collection/test/QueueTest.java @@ -0,0 +1,27 @@ +package week1.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week1.collection.Queue; + +public class QueueTest { + + private Queue queue=new Queue(); + + @Test + public void testEnQueue(){ + queue.enQueue("123"); + queue.enQueue("456"); + assertEquals("123",queue.deQueue()); + } + + @Test + public void testDeQueue(){ + queue.enQueue("123"); + queue.enQueue("456"); + queue.deQueue(); + assertEquals("456",queue.deQueue()); + } +} diff --git a/group26/1515345281/src/week1/collection/test/StackTest.java b/group26/1515345281/src/week1/collection/test/StackTest.java new file mode 100644 index 0000000000..717bf9b6a8 --- /dev/null +++ b/group26/1515345281/src/week1/collection/test/StackTest.java @@ -0,0 +1,56 @@ +package week1.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week1.collection.Stack; + +public class StackTest { + + private Stack stack=new Stack(); + + @Test + public void testPush(){ + stack.push("hello"); + stack.push("world"); + assertEquals("world",stack.peek()); + } + + @Test + public void testPop(){ + stack.push("hello"); + stack.push("world"); + assertEquals("world",stack.pop()); + assertEquals(1,stack.size()); + } + + @Test + public void testPeek(){ + stack.push("world"); + assertEquals("world",stack.peek()); + stack.pop(); + try{ + stack.peek(); + fail("stack is empty,can't do peek"); + + }catch(Exception ex){ + + } + } + + @Test + public void testEmpty(){ + assertEquals(true,stack.isEmpty()); + stack.push("hello"); + stack.push("world"); + assertEquals(false,stack.isEmpty()); + } + + @Test + public void testSize(){ + stack.push("hello"); + stack.pop(); + assertEquals(0,stack.size()); + } +} From 338118ec396146c9f2e262328158e83a68d3cb77 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:19:14 +0800 Subject: [PATCH 12/19] Delete a --- group26/1515345281/src/week1/collection/test/a | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group26/1515345281/src/week1/collection/test/a diff --git a/group26/1515345281/src/week1/collection/test/a b/group26/1515345281/src/week1/collection/test/a deleted file mode 100644 index 8b13789179..0000000000 --- a/group26/1515345281/src/week1/collection/test/a +++ /dev/null @@ -1 +0,0 @@ - From d4d8debdbbcc48dba2e8e6aee44f26f038c9bac5 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:20:15 +0800 Subject: [PATCH 13/19] Add files via upload --- .../src/week1/collection/ArrayList.java | 79 +++++++ .../src/week1/collection/BinaryTreeNode.java | 94 ++++++++ .../src/week1/collection/Iterator.java | 5 + .../src/week1/collection/LinkedList.java | 214 ++++++++++++++++++ .../1515345281/src/week1/collection/List.java | 9 + .../src/week1/collection/ListUtils.java | 9 + .../src/week1/collection/Queue.java | 24 ++ .../src/week1/collection/Stack.java | 37 +++ 8 files changed, 471 insertions(+) create mode 100644 group26/1515345281/src/week1/collection/ArrayList.java create mode 100644 group26/1515345281/src/week1/collection/BinaryTreeNode.java create mode 100644 group26/1515345281/src/week1/collection/Iterator.java create mode 100644 group26/1515345281/src/week1/collection/LinkedList.java create mode 100644 group26/1515345281/src/week1/collection/List.java create mode 100644 group26/1515345281/src/week1/collection/ListUtils.java create mode 100644 group26/1515345281/src/week1/collection/Queue.java create mode 100644 group26/1515345281/src/week1/collection/Stack.java diff --git a/group26/1515345281/src/week1/collection/ArrayList.java b/group26/1515345281/src/week1/collection/ArrayList.java new file mode 100644 index 0000000000..2ba58db6f0 --- /dev/null +++ b/group26/1515345281/src/week1/collection/ArrayList.java @@ -0,0 +1,79 @@ +package week1.collection; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size=0; + + private Object[] elementData=new Object[100]; + + public void add(Object o){ + + add(size,o); + + } + + public void add(int index, Object o){ + + ListUtils.checkIndexRange(index,size); + + if(size == elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length+50); + } + + if(index < size){ + for(int i=size-1;i>=index;i--){ + elementData[i+1]=elementData[i]; + } + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + + ListUtils.checkIndexRange(index+1,size); + + return elementData[index]; + } + + public Object remove(int index){ + + ListUtils.checkIndexRange(index+1,size); + + Object object=elementData[index]; + for(int i=index;i 0){ + if(null == currentNode.getLeft()){ + BinaryTreeNode insertNode=new BinaryTreeNode(o); + currentNode.setLeft(insertNode); + return insertNode; + } + currentNode=currentNode.left; + }else{ + if(null ==currentNode.right){ + BinaryTreeNode insertNode=new BinaryTreeNode(o); + currentNode.setRight(insertNode); + return insertNode; + } + currentNode=currentNode.right; + } + } + return new BinaryTreeNode(o); + }*/ + + /* + * 递归实现 + */ + public BinaryTreeNode insert(Comparable o){ + + Comparable data=(Comparable) this.getData(); + int result=data.compareTo(o); + + if(result == 0){ + return this; + }else if(result > 0){ + if(this.getLeft()==null){ + BinaryTreeNode node=new BinaryTreeNode(o); + this.setLeft(node); + return node; + } + return this.getLeft().insert(o); + }else{ + if(this.getRight()==null){ + BinaryTreeNode node=new BinaryTreeNode(o); + this.setRight(node); + return node; + } + return this.getRight().insert(o); + } + } + + public Object getData() { + return data; + } + + public void setData(Comparable 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; + } + +} diff --git a/group26/1515345281/src/week1/collection/Iterator.java b/group26/1515345281/src/week1/collection/Iterator.java new file mode 100644 index 0000000000..03e50730f7 --- /dev/null +++ b/group26/1515345281/src/week1/collection/Iterator.java @@ -0,0 +1,5 @@ +package week1.collection; +public interface Iterator{ + public boolean hasNext(); + public Object next(); +} \ No newline at end of file diff --git a/group26/1515345281/src/week1/collection/LinkedList.java b/group26/1515345281/src/week1/collection/LinkedList.java new file mode 100644 index 0000000000..fd0b9c76dd --- /dev/null +++ b/group26/1515345281/src/week1/collection/LinkedList.java @@ -0,0 +1,214 @@ +package week1.collection; + +/* + * 要求:单向链表实现 + */ +public class LinkedList implements List { + + private int size=0;//表示该链表的长度 + private Node head;//链表的头元素 + + public void add(Object o){ + if(null == head){ + head = new Node(o); + size++; + return ; + } + + Node node=head; + while(null != node.next){ + node=node.next; + } + Node addNode=new Node(o); + node.next=addNode; + size++; + } + + public void add(int index , Object o){ + if(size == 0 || index ==size){ + add(o); + return ; + } + + ListUtils.checkIndexRange(index, size); + + if(index==0){ + Node node=new Node(head.next.data); + node.next=head.next; + head.next=node; + head.data=o; + size++; + return ; + } + + Node node=head; + for(int i=0;i7->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){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @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; + } +} \ No newline at end of file diff --git a/group26/1515345281/src/week1/collection/List.java b/group26/1515345281/src/week1/collection/List.java new file mode 100644 index 0000000000..013956e25b --- /dev/null +++ b/group26/1515345281/src/week1/collection/List.java @@ -0,0 +1,9 @@ +package week1.collection; + +public interface List{ + public void add(Object o); + public void add(int index,Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group26/1515345281/src/week1/collection/ListUtils.java b/group26/1515345281/src/week1/collection/ListUtils.java new file mode 100644 index 0000000000..3ebb559aea --- /dev/null +++ b/group26/1515345281/src/week1/collection/ListUtils.java @@ -0,0 +1,9 @@ +package week1.collection; + +public class ListUtils { + + public static void checkIndexRange(int index, int size) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException(); + } +} diff --git a/group26/1515345281/src/week1/collection/Queue.java b/group26/1515345281/src/week1/collection/Queue.java new file mode 100644 index 0000000000..06f5ea52c8 --- /dev/null +++ b/group26/1515345281/src/week1/collection/Queue.java @@ -0,0 +1,24 @@ +package week1.collection; + +public class Queue { + + private LinkedList list=new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + + return list.removeFirst(); + } + + public boolean isEmpty(){ + + return list.size()==0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group26/1515345281/src/week1/collection/Stack.java b/group26/1515345281/src/week1/collection/Stack.java new file mode 100644 index 0000000000..63b226dca8 --- /dev/null +++ b/group26/1515345281/src/week1/collection/Stack.java @@ -0,0 +1,37 @@ +package week1.collection; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + + Object data=elementData.get(size()-1); + elementData.remove(size()-1); + return data; + } + + public Object peek(){ + return (Object)elementData.get(size()-1); + } + + public boolean isEmpty(){ + if(elementData.size()==0) + return true; + else + return false; + } + + public int size(){ + return elementData.size(); + } +} From 1939a6347f8911f56ead6b2b50119699634f99b6 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:22:36 +0800 Subject: [PATCH 14/19] Create ArrayUtil.java --- .../src/week2/arrayutil/ArrayUtil.java | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 group26/1515345281/src/week2/arrayutil/ArrayUtil.java diff --git a/group26/1515345281/src/week2/arrayutil/ArrayUtil.java b/group26/1515345281/src/week2/arrayutil/ArrayUtil.java new file mode 100644 index 0000000000..dcc530949d --- /dev/null +++ b/group26/1515345281/src/week2/arrayutil/ArrayUtil.java @@ -0,0 +1,225 @@ +package week2.arrayutil; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for(int i=0;i list=new ArrayList(); + int i=0; + int j=0; + while(i array2[j]){ + list.add(array2[j]); + j++; + }else{ + list.add(array1[i]); + i++; + j++; + } + } + while(i=result.length ){ + result=this.grow(result, 5); + } + result[cursor]=result[cursor-2]+result[cursor-1]; + cursor++; + } + return result; + } + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max<=1){ + return new int[0]; + } + if(max == 2){ + return new int[]{2}; + } + + int[] temp=new int[max]; + temp[2]=2; + int primeNum=0; + for(int i=3;i list=new ArrayList(); + for(int i=6;i Date: Sun, 19 Mar 2017 23:22:58 +0800 Subject: [PATCH 15/19] Delete qqq --- group26/1515345281/src/week2/qqq | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group26/1515345281/src/week2/qqq diff --git a/group26/1515345281/src/week2/qqq b/group26/1515345281/src/week2/qqq deleted file mode 100644 index 8b13789179..0000000000 --- a/group26/1515345281/src/week2/qqq +++ /dev/null @@ -1 +0,0 @@ - From 43416367d6769b7486e15ee50ba113642214f93e Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:23:44 +0800 Subject: [PATCH 16/19] Create View.java --- .../1515345281/src/week2/struts2/View.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 group26/1515345281/src/week2/struts2/View.java diff --git a/group26/1515345281/src/week2/struts2/View.java b/group26/1515345281/src/week2/struts2/View.java new file mode 100644 index 0000000000..5a907a455f --- /dev/null +++ b/group26/1515345281/src/week2/struts2/View.java @@ -0,0 +1,23 @@ +package week2.struts2; + +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; + } +} From b0fe53ffc6878af624b023cc320dd51e4dd833ce Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:24:33 +0800 Subject: [PATCH 17/19] Add files via upload --- .../src/week2/struts2/LoginAction.java | 39 ++++++++++++++++++ .../src/week2/struts2/LoginOutAction.java | 5 +++ .../1515345281/src/week2/struts2/Struts.java | 34 ++++++++++++++++ .../src/week2/struts2/StrutsTest.java | 40 +++++++++++++++++++ .../1515345281/src/week2/struts2/struts.xml | 12 ++++++ 5 files changed, 130 insertions(+) create mode 100644 group26/1515345281/src/week2/struts2/LoginAction.java create mode 100644 group26/1515345281/src/week2/struts2/LoginOutAction.java create mode 100644 group26/1515345281/src/week2/struts2/Struts.java create mode 100644 group26/1515345281/src/week2/struts2/StrutsTest.java create mode 100644 group26/1515345281/src/week2/struts2/struts.xml diff --git a/group26/1515345281/src/week2/struts2/LoginAction.java b/group26/1515345281/src/week2/struts2/LoginAction.java new file mode 100644 index 0000000000..0a34e92f18 --- /dev/null +++ b/group26/1515345281/src/week2/struts2/LoginAction.java @@ -0,0 +1,39 @@ +package week2.struts2; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group26/1515345281/src/week2/struts2/LoginOutAction.java b/group26/1515345281/src/week2/struts2/LoginOutAction.java new file mode 100644 index 0000000000..54bfb503fd --- /dev/null +++ b/group26/1515345281/src/week2/struts2/LoginOutAction.java @@ -0,0 +1,5 @@ +package week2.struts2; + +public class LoginOutAction { + +} diff --git a/group26/1515345281/src/week2/struts2/Struts.java b/group26/1515345281/src/week2/struts2/Struts.java new file mode 100644 index 0000000000..06edfe7e20 --- /dev/null +++ b/group26/1515345281/src/week2/struts2/Struts.java @@ -0,0 +1,34 @@ +package week2.struts2; + +import java.util.Map; + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + - + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + return null; + } + +} diff --git a/group26/1515345281/src/week2/struts2/StrutsTest.java b/group26/1515345281/src/week2/struts2/StrutsTest.java new file mode 100644 index 0000000000..68ec3ee4b4 --- /dev/null +++ b/group26/1515345281/src/week2/struts2/StrutsTest.java @@ -0,0 +1,40 @@ +package week2.struts2; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group26/1515345281/src/week2/struts2/struts.xml b/group26/1515345281/src/week2/struts2/struts.xml new file mode 100644 index 0000000000..9e69fa4e47 --- /dev/null +++ b/group26/1515345281/src/week2/struts2/struts.xml @@ -0,0 +1,12 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From 96e57174278bccd9a9ef56077f6c05fe5349969d Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:26:01 +0800 Subject: [PATCH 18/19] Create ArrayUtilTest.java --- group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java | 1 + 1 file changed, 1 insertion(+) create mode 100644 group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java diff --git a/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java b/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java @@ -0,0 +1 @@ + From acd6537cdf67c7abbc3a61106e6d40ea35578532 Mon Sep 17 00:00:00 2001 From: SJsunshine Date: Sun, 19 Mar 2017 23:26:27 +0800 Subject: [PATCH 19/19] Update ArrayUtilTest.java --- .../src/week2/arrayutil/ArrayUtilTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java b/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java index 8b13789179..7b0d3c94e0 100644 --- a/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java +++ b/group26/1515345281/src/week2/arrayutil/ArrayUtilTest.java @@ -1 +1,97 @@ +package week2.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import week2.arrayutil.ArrayUtil; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil=new ArrayUtil(); + + @Test + public void testReverseArray(){ + + int[] origin={1,2,3,4}; + arrayUtil.reverseArray(origin); + assertArrayEquals(new int[]{4,3,2,1}, origin); + + int[] origin1={5,6,7}; + arrayUtil.reverseArray(origin1); + assertArrayEquals(new int[]{7,6,5},origin1); + } + + @Test + public void testRemoveArray(){ + + int[] oldArray={0,0,1,2,0,0,3,4,0,0}; + int[] result=arrayUtil.removeZero(oldArray); + assertArrayEquals(new int[]{1,2,3,4},result); + } + + @Test + public void testMerge(){ + int[] array1={3,5,7,8}; + int[] array2={4,5,6,7}; + int[] array=arrayUtil.merge(array1, array2); + assertArrayEquals(new int[]{3,4,5,6,7,8}, array); + } + + @Test + public void testGrow(){ + int[] oldArray={2,3,6}; + int size=3; + int[] newArray=new int[oldArray.length+size]; + newArray=arrayUtil.grow(oldArray, size); + assertArrayEquals(new int[]{2,3,6,0,0,0},newArray); + } + + @Test + public void testFibonacci(){ + int max=1; + int[] result=arrayUtil.fibonacci(max); + assertArrayEquals(new int[0],result); + + max=2; + int[] result1=arrayUtil.fibonacci(max); + assertEquals(1,result1[0]); + + max=15; + int[] result2=arrayUtil.fibonacci(max); + assertArrayEquals(new int[]{1,1,2,3,5,8,13},result2); + } + + @Test + public void testGetPrime(){ + int max=-1; + int[] result=arrayUtil.getPrimes(max); + assertEquals(new int[0],result); + + max=23; + result=arrayUtil.getPrimes(max); + assertEquals(new int[]{2,3,5,7,11,13,17,19},result); + + } + + @Test + public void testGetPerfectNumbers(){ + int max=7; + int[] result=arrayUtil.getPerfectNumbers(max); + assertArrayEquals(new int[]{6},result); + + max=100; + result=arrayUtil.getPerfectNumbers(max); + assertArrayEquals(new int[]{6,28},result); + } + + @Test + public void testJoin(){ + String seperator="-"; + int[] array={3,5,8}; + String result=arrayUtil.join(array, seperator); + assertEquals("3-5-8",result); + + } +}