forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from MrGPanPan/master
第二周同步分支
- Loading branch information
Showing
34 changed files
with
1,331 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Array</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
158 changes: 158 additions & 0 deletions
158
group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
||
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 < origin.length/2; i++) {//前后交换元素 | ||
int temp = origin[i]; | ||
origin[i] = origin[origin.length-i-1]; | ||
origin[origin.length-i-1]= temp; | ||
} | ||
System.out.println(Arrays.toString(origin));//输出数组 | ||
|
||
} | ||
|
||
/** | ||
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} | ||
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: | ||
* {1,3,4,5,6,6,5,4,7,6,7,5} | ||
* @param oldArray | ||
* @return | ||
*/ | ||
|
||
public int[] removeZero(int[] oldArray){ | ||
int[] arr = new int[oldArray.length]; | ||
int count = 0; | ||
for(int i=0;i<oldArray.length;i++){ | ||
if (oldArray[i]!=0) { | ||
arr[count] = oldArray[i]; | ||
count++; | ||
} | ||
} | ||
|
||
int[] newArr = new int[count]; | ||
System.arraycopy(arr, 0, newArr, 0, count); | ||
|
||
return newArr; | ||
} | ||
|
||
/** | ||
* 给定两个已经排序好的整形数组, 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){ | ||
HashSet set1 = new HashSet(Arrays.asList(array1)); | ||
HashSet set2 = new HashSet(Arrays.asList(array2)); | ||
set1.addAll(set2); | ||
/* | ||
HashSet<Integer> set1 = new HashSet(Arrays.asList(array1));//以array1建立集合 | ||
HashSet<Integer> set2 = new HashSet(Arrays.asList(array2)); | ||
set1.addAll(set2);//求并集 | ||
Integer[] arr = set1.toArray(new Integer[set1.size()]);//获取并集后的数组 | ||
Arrays.sort(arr);//数组排序 | ||
for(int i=0;i<arr.length;i++){ | ||
System.out.print(arr[i]+" "); | ||
} | ||
*/ | ||
|
||
|
||
|
||
return null; | ||
} | ||
/** | ||
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size | ||
* 注意,老数组的元素在新数组中需要保持 | ||
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 | ||
* [2,3,6,0,0,0] | ||
* @param oldArray | ||
* @param size | ||
* @return | ||
*/ | ||
public int[] grow(int [] oldArray, int size){ | ||
int[] newArr = new int[oldArray.length+size]; | ||
System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); | ||
|
||
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){ | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* 返回小于给定最大值max的所有素数数组 | ||
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] | ||
* @param max | ||
* @return | ||
*/ | ||
public int[] getPrimes(int max){ | ||
return null; | ||
} | ||
|
||
/** | ||
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 | ||
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 | ||
* @param max | ||
* @return | ||
*/ | ||
public int[] getPerfectNumbers(int max){ | ||
return null; | ||
} | ||
|
||
/** | ||
* 用seperator 把数组 array给连接起来 | ||
* 例如array= [3,8,9], seperator = "-" | ||
* 则返回值为"3-8-9" | ||
* @param array | ||
* @param s | ||
* @return | ||
*/ | ||
public String join(int[] array, String seperator){ | ||
String aString = ""; | ||
String bString = ""; | ||
for(int a:array){ | ||
aString = a+seperator; | ||
bString = bString+aString; | ||
} | ||
return bString; | ||
} | ||
|
||
public static void main(String [] args){ | ||
ArrayUtil arrayUtil = new ArrayUtil(); | ||
int[] a = {7,9,30,3,0,6,0}; | ||
arrayUtil.reverseArray(a); | ||
|
||
System.out.println(arrayUtil.join(a, "-")); | ||
|
||
int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; | ||
System.out.println(Arrays.toString(arrayUtil.removeZero(oldArr))); | ||
|
||
System.out.println(Arrays.toString(arrayUtil.grow(a, 4))); | ||
|
||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="C:/Users/admin/Downloads/dom4j_完整版/dom4j-1.6.1/dom4j-1.6.1.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>struts</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
39 changes: 39 additions & 0 deletions
39
group11/1059156023/struts/src/com/coding/basic/LoginAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 | ||
* @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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
group11/1059156023/struts/src/com/coding/basic/Struts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.coding.basic; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.dom4j.Document; | ||
import org.dom4j.DocumentException; | ||
import org.dom4j.Element; | ||
import org.dom4j.io.SAXReader; | ||
import org.junit.experimental.theories.Theories; | ||
|
||
import com.sun.corba.se.impl.orbutil.graph.Node; | ||
import com.sun.org.apache.bcel.internal.classfile.Attribute; | ||
|
||
|
||
|
||
public class Struts { | ||
|
||
public static View runAction(String actionName, Map<String,String> parameters) throws DocumentException { | ||
//创建SAXReader对象 | ||
SAXReader reader = new SAXReader(); | ||
//读取文件 转换成Document | ||
Document document = reader.read(new File("src/com/coding/basic/struts.xml")); | ||
//获取根节点元素对象 | ||
Element root = document.getRootElement(); | ||
//遍历根节点 | ||
listNodes(root); | ||
|
||
/* | ||
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中的 <result> 配置,以及execute的返回值, 确定哪一个jsp, | ||
放到View对象的jsp字段中。 | ||
*/ | ||
|
||
return null; | ||
} | ||
|
||
public static void listNodes(Element node) { | ||
String name; | ||
List<Attribute> list = node.attributes(); | ||
|
||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
group11/1059156023/struts/src/com/coding/basic/StrutsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.dom4j.DocumentException; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class StrutsTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testLoginActionSuccess() throws DocumentException { | ||
|
||
String actionName = "login"; | ||
|
||
Map<String,String> params = new HashMap<String,String>(); | ||
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 DocumentException { | ||
String actionName = "login"; | ||
Map<String,String> params = new HashMap<String,String>(); | ||
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")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.coding.basic; | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.