Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions group22/1258890344/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/bin/
# Class files
*.class

# Package Files
*.jar
*.war
*.ear

# Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Ignore web-site project
*web-site/

# Temporary files
.DS_STORE
*.log

# Maven related
/*/target/
target

# Netbeans related
nb-configuration.xml
nbactions.xml
nbproject

# Eclipse related

.settings
*.classpath
*.project

# IntelliJ related
.idea
*.iml
*.ipr
*.iws

# Jrebel related
rebel.xml
rebel-remote.xml

# design model
*.eab

.idea/workspace.xml
209 changes: 209 additions & 0 deletions group22/1258890344/src/com/coderising/array/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.coderising.array;

import java.util.Arrays;
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 value=0;
for(int i=0;i<(length/2);i++){
value=origin[i];
origin[i]=origin[length-1-i];
origin[length-1-i]=value;

}
}

/**
* 现在有如下的一个数组: 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 j=0;
for(int i=0;i<length;i++){
if(oldArray[i]==0){
continue;
}
newArray[j]=oldArray[i];
j++;
}
return newArray;
}

/**
* 给定两个已经排序好的整形数组, 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=Arrays.copyOf(array1,length1+length2 );
int j=0;
for(int i=0;i<(length1+length2);i++){
if(array3[i]>array2[j]){
for(int k=length1;k>i;k--){
array3[k]=array3[k-1];
}
array3[i]=array2[j];
j++;
if(j<length2){
continue;
}
return array3;
}else if(array3[i]<array2[j]){
if(array3[i]==0){
array3[i]=array2[j];
j++;
}
if(j<length2){
continue;
}
return array3;
}else{
j++;
if(j<length2){
continue;
}
return array3;
}
}
return array3;
}
/**
* 把一个已经存满数据的数组 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 [] newArray=Arrays.copyOf(oldArray, length+size);
return newArray;
}

/**
* 斐波那契数列为: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){
int[] array=new int[max];
if(max==1){
return array;
}
if(max>1){
array[0]=1;
array[1]=1;
for(int i=2;i>1;i++){
array[i]=array[i-1]+array[i-2];
if(array[i]>=max){
array[i]=0;
return array;
}
}
}
return array;


}

/**
* 返回小于给定最大值max的所有素数数组
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
* @param max
* @return
*/
public int[] getPrimes(int max){

int[] array=new int[max];
int k=0;
if(max==2){
array[0]=2;
}
for(int i=3;i<max;i++){
Boolean flag=true;
for(int j=2;j<i;j++){
if(i%j==0){
flag=false;
break;
}
}
if(flag){
array[k]=i;
k++;
}
}
return array;
}

/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
* @param max
* @return
*/
public int[] getPerfectNumbers(int max){
int[] array=new int[max/2];
int k=0;
for(int i=1;i<max;i++){
int sum=0;
for(int j=1;j<i;j++){
if(i%j==0){
sum+=j;
continue;
}
}
if(sum==i){
array[k]=sum;
k++;
continue;
}
}
return array;
}

/**
* 用seperator 把数组 array给连接起来
* 例如array= [3,8,9], seperator = "-"
* 则返回值为"3-8-9"
* @param array
* @param s
* @return
*/
public String join(int[] array, String seperator){
int length=array.length;
String string="";
for(int i=0;i<length;i++){
string+=array[i]+seperator;
if(i==length-1){
string+=array[i];
}

}
return string;
}


}
88 changes: 88 additions & 0 deletions group22/1258890344/src/com/coderising/array/ArrayUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.coderising.array;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ArrayUtilTest {
private static ArrayUtil a=new ArrayUtil();

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testReverseArray() {
int[] array={3,5,7,9};
a.reverseArray(array);
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}

@Test
public void testRemoveZero() {
int oldArr[]={1,0,6,0,5,7,0};
int newArr[]=a.removeZero(oldArr);
for(int i=0;i<newArr.length;i++){
System.out.println(newArr[i]);
}
}

@Test
public void testMerge() {
int[] a1 = {3, 5, 7,8};
int[] a2 = {4, 8, 9,11,19,20};
int[] a3=a.merge(a1, a2);
for(int i=0;i<a3.length;i++){
System.out.println(a3[i]);
}
}

@Test
public void testGrow() {
int oldArr[]={1,0,6,0,5,7,0};
int newArr[]=a.grow(oldArr, 5);
System.out.println(newArr.length);
}

@Test
public void testFibonacci() {
int newArr[]=a.fibonacci(1);
for(int i=0;i<newArr.length;i++){
System.out.println(newArr[i]);
}
}

@Test
public void testGetPrimes() {
int newArr[]=a.getPrimes(2);
for(int i=0;i<newArr.length;i++){
System.out.println(newArr[i]);
}
}

@Test
public void testGetPerfectNumbers() {
int newArr[]=a.getPerfectNumbers(100);
for(int i=0;i<newArr.length;i++){
System.out.println(newArr[i]);
}
}

@Test
public void testJoin() {
int oldArr[]={1,0,6,0,5,7,0};
String result=a.join(oldArr, "-");
System.out.println(result);


}

}
39 changes: 39 additions & 0 deletions group22/1258890344/src/com/coderising/litestruts/LoginAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.coderising.litestruts;

/**
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
* @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;
}
}
Loading