forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #12 from Wilsonlhuang/master
第二次的作业
- Loading branch information
Showing
8 changed files
with
555 additions
and
0 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
group23/1246614258/第二次作业/DataStructsTest/src/com/coding/datastructs/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,141 @@ | ||
/** | ||
* | ||
*/ | ||
package com.coding.datastructs; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
|
||
public class ArrayUtil { | ||
|
||
|
||
public void reverseArray(int[] origin){ | ||
for (int i=0; i<origin.length/2; i++) { | ||
int temp = origin[i]; | ||
origin[i] = origin[origin.length-1-i]; | ||
origin[origin.length-1-i] = temp; | ||
} | ||
} | ||
|
||
|
||
|
||
public int[] removeZero(int[] oldArray){ | ||
int count=0; | ||
for(int i=0;i<oldArray.length;i++){ | ||
if(oldArray[i]!=0){ | ||
oldArray[count++]=oldArray[i]; | ||
}else{ | ||
continue; | ||
} | ||
} | ||
return Arrays.copyOfRange(oldArray, 0, count); | ||
} | ||
|
||
|
||
|
||
public int[] merge(int[] array1, int[] array2){ | ||
int length = array1.length+array2.length; | ||
int[] newArray = new int[length]; | ||
System.arraycopy(array1, 0, newArray, 0,array1.length); | ||
System.arraycopy(array2, 0, newArray, array1.length,array2.length); | ||
Set<Integer> temp = new HashSet<Integer>(); | ||
for(Integer i : newArray) | ||
temp.add(i); | ||
for (int i = 0; i < temp.size(); i++) { | ||
newArray[i]=(int)temp.toArray()[i]; | ||
} | ||
Arrays.sort(Arrays.copyOfRange(newArray, 0, temp.size())); | ||
return Arrays.copyOfRange(newArray, 0, temp.size()); | ||
} | ||
|
||
public int[] grow(int [] oldArray, int size){ | ||
int[] target = new int[oldArray.length+size]; | ||
System.arraycopy(oldArray, 0, target, 0, oldArray.length); | ||
return target; | ||
} | ||
|
||
|
||
public int[] fibonacci(int max){ | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
list.add(1); | ||
list.add(1); | ||
if(max<=1){ | ||
return new int[0]; | ||
} | ||
for (int i = 2; i < max; i++) { | ||
int a = list.get(i-1)+list.get(i-2); | ||
if(a<max){ | ||
list.add(a); | ||
}else{ | ||
break; | ||
} | ||
} | ||
int[] newArray = new int[list.size()]; | ||
for(int i=0;i<list.size();i++){ | ||
newArray[i]=(int)list.get(i); | ||
} | ||
return newArray; | ||
} | ||
|
||
|
||
public int[] getPrimes(int max){ | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
int j=1; | ||
for(int i=2;i<100;i++){ | ||
j=i; | ||
while(j>0){ | ||
if(i%j==0&&i!=j&&j!=1){ | ||
break; | ||
}else{ | ||
j--; | ||
} | ||
} | ||
if(j==0){ | ||
list.add(i); | ||
} | ||
} | ||
int[] newArray = new int[list.size()]; | ||
for(int i=0;i<list.size();i++){ | ||
newArray[i]=(int)list.get(i); | ||
} | ||
return newArray; | ||
} | ||
|
||
|
||
public int[] getPerfectNumbers(int max){ | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
for(int i = 1; i < max; i++) | ||
{ | ||
int sum = 0; | ||
for(int j = 1; j < i; j++) | ||
{ | ||
if(i % j == 0) | ||
{ | ||
sum = sum + j; | ||
} | ||
} | ||
if(sum == i) | ||
{ | ||
list.add(i); | ||
} | ||
} | ||
int[] newArray = new int[list.size()]; | ||
for(int i=0;i<list.size();i++){ | ||
newArray[i]=(int)list.get(i); | ||
} | ||
return newArray; | ||
} | ||
|
||
public String join(int[] array, String seperator){ | ||
StringBuilder str = new StringBuilder(); | ||
for(int i=0;i<array.length;i++){ | ||
str.append(array[i]).append(seperator); | ||
} | ||
return str.substring(0, str.length()-1).toString(); | ||
} | ||
|
||
|
||
} |
86 changes: 86 additions & 0 deletions
86
group23/1246614258/第二次作业/DataStructsTest/src/com/coding/test/ArrayUtilTest.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,86 @@ | ||
/** | ||
* | ||
*/ | ||
package com.coding.test; | ||
|
||
|
||
import org.junit.Test; | ||
|
||
import com.coding.datastructs.ArrayUtil; | ||
|
||
|
||
public class ArrayUtilTest { | ||
@Test | ||
public void reverseTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] origin ={7,9,30,3,5,6,2,4,78,1,4,4567,3}; | ||
array.reverseArray(origin); | ||
for(int i=0;i<origin.length;i++){ | ||
System.out.print(origin[i]+","); | ||
} | ||
} | ||
@Test | ||
public void removeZeroTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] originArray ={0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5,0}; | ||
int[] newArray=array.removeZero(originArray); | ||
for(int i=0;i<newArray.length;i++){ | ||
System.out.print(newArray[i]+","); | ||
} | ||
} | ||
@Test | ||
public void mergeTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] a1 = {3, 5, 7,8}; | ||
int[] a2 = {4, 5, 6,7}; | ||
int[] a3 = array.merge(a1, a2); | ||
for(int i=0;i<a3.length;i++){ | ||
System.out.print(a3[i]+","); | ||
} | ||
|
||
} | ||
@Test | ||
public void growTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] oldArray = {2,3,6}; | ||
oldArray = array.grow(oldArray, 3); | ||
for(int i=0;i<oldArray.length;i++){ | ||
System.out.print(oldArray[i]+","); | ||
} | ||
} | ||
@Test | ||
public void fibinaqiTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] a = array.fibonacci(56); | ||
for(int i=0;i<a.length;i++){ | ||
System.out.print(a[i]+","); | ||
} | ||
} | ||
@Test | ||
public void sushuTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] aa = array.getPrimes(100); | ||
for(int t=0;t<aa.length;t++){ | ||
System.out.print(aa[t]+","); | ||
} | ||
} | ||
@Test | ||
public void perfectTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] aaa = array.getPerfectNumbers(1000); | ||
for(int t=0;t<aaa.length;t++){ | ||
System.out.print(aaa[t]+","); | ||
} | ||
} | ||
@Test | ||
public void seperatorTest(){ | ||
ArrayUtil array = new ArrayUtil(); | ||
int[] arr = array.getPrimes(100); | ||
String str = array.join(arr, "-"); | ||
System.out.println(str); | ||
} | ||
|
||
// | ||
|
||
|
||
} |
11 changes: 11 additions & 0 deletions
11
group23/1246614258/第二次作业/StrutsDemo/WebContent/WEB-INF/etc/struts.xml
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<struts> | ||
<action name="login" class="com.coderising.litestruts.LoginAction"> | ||
<result name="success">/jsp/homepage.jsp</result> | ||
<result name="fail">/jsp/showLogin.jsp</result> | ||
</action> | ||
<action name="logout" class="com.coderising.litestruts.LogoutAction"> | ||
<result name="success">/jsp/welcome.jsp</result> | ||
<result name="error">/jsp/error.jsp</result> | ||
</action> | ||
</struts> |
35 changes: 35 additions & 0 deletions
35
group23/1246614258/第二次作业/StrutsDemo/src/com/coderising/litestruts/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,35 @@ | ||
package com.coderising.litestruts; | ||
|
||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.