Skip to content

Commit

Permalink
Merge pull request #14 from KiLien/master
Browse files Browse the repository at this point in the history
week02
  • Loading branch information
dracome authored Mar 6, 2017
2 parents 7f2738a + 1645e58 commit e8317a1
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 0 deletions.
263 changes: 263 additions & 0 deletions group09/790466157/src/com/coderising/array/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
package com.coderising.array;
import java.util.*;

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[] origin1 = {7,9,30,3};
//交换数组元素
for (int i = 0; i < origin1.length/2; i++){
int tmp = origin1[i];
origin1[i] = origin1[origin1.length-i-1];
origin1[origin1.length-i-1] = tmp;
}
System.out.println(Arrays.toString(origin1));
}

/**
* 现在有如下的一个数组: 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[] oldArray1 = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int newArrayLength = getLength(oldArray1);
int[] newArray = getNewArray(oldArray1, newArrayLength);
print(oldArray1);
print(newArray);
return newArray;
}

public static int getLength(int[] array){
int num = 0;
for(int i = 0 ; i < array.length;i++){
if(array[i] != 0){
num++;
}
}
return num;
}

public static int[] getNewArray(int[] array,int num){
int[] newArray = new int[num];
int index = 0;
for(int i = 0; i < array.length; i ++){
if(array[i]!=0){
newArray[index] = array[i];
index++;
}
}
return newArray;
}
public static void print(int [] array){
for(int i : array){
System.out.print(i+" ");
}
System.out.println();
}


/**
* 给定两个已经排序好的整形数组, 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[] a={10,20,30,40,50};
int[] b={10,20,60};
int[] c = new int[a.length+b.length-cf(a,b)*2];
int index = 0;
for (int i=0;i<a.length;i++){
if (!isExist(b,a[i])){
c[index++] = a[i];
}
else{

}
}
for (int i=0;i<b.length;i++){
if (!isExist(a,b[i])){
c[index++] = b[i];
}
}
return c;
}

public static int cf(int[] a,int [] b){
int num = 0;
for (int i=0;i<a.length;i++){
if (isExist(b,a[i])){
num++;
}
}
return num;
}

public static boolean isExist(int[] a,int s){
boolean d = false;
for (int i=0;i<a.length;i++){
if (s==a[i]){
d = true;
}
}
return d;
}


/**
* 把一个已经存满数据的数组 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[] oldArray1 = {2,3,6};
int size1 = 3;
int aL = oldArray1.length;
oldArray1 = Arrays.copyOf(oldArray1,aL + size1);
System.arraycopy(oldArray1,0,oldArray1,aL + size1,aL);
//System.out.println(Arrays.toString(oldArray1));
return oldArray1;
}

/**
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
* max = 1, 则返回空数组 []
* @param max
* @return
*/
public static int[] fibonacci(int max){
int[] f = new int[max];
f[0] = 1;
f[1] = 1;
for(int i =2; i < max; i++){
f[i] = f[i-1] + f[i-2];
}

return f;
}

/**
* 返回小于给定最大值max的所有素数数组
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
* @param max
* @return
*/
public class primes {
public void showPrimeNumber(int range){
boolean[] primes = this.sieve(range);
int number = 0 ;
if(primes != null){
int size = primes.length;
System.out.println("范围在"+range+"内的质数个数有:");
for(int i = 1 ; i< size ; i++){
if(primes[i]){
System.out.print(i + " ");

if(++number %10 ==0){
System.out.println();
}
}
}
System.out.println();
}
}

private boolean[] sieve(int range){
if(range <= 0 ){
System.out.println("求质数的范围range必须大于0!");
return null;
}
boolean[] isPrime = new boolean[range + 1];
isPrime[1] = false ;
Arrays.fill(isPrime, 2,range+1,true);
int n = (int)Math.sqrt(range);
for(int i = 1 ; i <= n ; i++){
if(isPrime[i]){
for(int j = 2 * i ;j <= range ; j+=i){
isPrime[j] = false;

}
}
}
return isPrime;
}
}




/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
* @param max
* @return
*/

public static int[] PerfectNumbers(int max){
int[] f = new int[max];
int i,j,sum; //sum用来存放因子之和
for(i=0;i<max;i++) //对1到numb以内的数依次尝试
{
sum = 0; //给sum赋值,同时也是对上一次的值清空
for(j=1;j<=i/2;j++) //查找因子
{
if(i%j==0) // 如果是因子
{
sum+=j; //把当前的因子累加到sum中
}
}
if(sum==i) //判断是不是完数,即因子之和等于自身
{
f[i]=i; //是完数,输出
}
}
return f;
}

/**
* 用seperator 把数组 array给连接起来
* 例如array= [3,8,9], seperator = "-"
* 则返回值为"3-8-9"
* @param array
* @param s
* @return
*/
public static String join(Object[] array, char separator) {
if (array == null) {
return null;
}
int arraySize = array.length;
int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize);
StringBuffer buf = new StringBuffer(bufSize);

for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}


}
39 changes: 39 additions & 0 deletions group09/790466157/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;
}
}
30 changes: 30 additions & 0 deletions group09/790466157/src/com/coderising/litestruts/SAX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.coderising.litestruts;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
public class SAX extends DefaultHandler {
//文档开始事件处理方法
public void startDocument() {
System.out.println("文档开始 ");
}
//文档结束事件处理方法
public void endDocument() {
System.out.println("文档结束");
}
//元素开始事件处理方法
public void startElement(String uri, String localName, String qname, Attributes attr)
{ System.out.println("元素开始: 本地名: " + localName + " 限定名: " + qname + " 命名空间URI: "+uri);
int attrCount = attr.getLength();
if(attrCount>0) {
System.out.println("属性:");
for(int i = 0 ; i<attrCount ; i++) {
System.out.println(" 属性名 : " + attr.getQName(i));
System.out.println(" 属性类型 : " + attr.getType(i));
System.out.println(" 属性值: " + attr.getValue(i)); } } }
//元素结束事件处理方法
public void endElement(String uri, String localName, String qname) {
System.out.println("元素结束: 本地名: " + localName + " 限定名: " + qname + " 命名空间URI: "+uri); }
//文本事件处理方法
public void characters(char[] ch, int start, int length) {
System.out.println("文本字符: " + new String(ch, start, length)); }
//空白字符事件处理方法
public void ignorableWhitespace(char[] ch, int start, int length) { System.out.println("忽略的空白: " + length + " Characters."); } }
21 changes: 21 additions & 0 deletions group09/790466157/src/com/coderising/litestruts/SAXmain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.coderising.litestruts;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
public class SAXmain {
public static void main(String args[]) {
File xmlFile = new File("struts.xml");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = null;
spf.setNamespaceAware(true);
spf.setValidating(true);
try {
parser = spf.newSAXParser();
SAXmain handler = new SAXmain();
parser.parse(xmlFile, handler);
}
catch(Exception e) {
e.printStackTrace(System.err); } }
}
35 changes: 35 additions & 0 deletions group09/790466157/src/com/coderising/litestruts/Struts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.coderising.litestruts;

import java.util.Map;



public class Struts {

public static View runAction(String actionName, Map<String,String> 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中的 <result> 配置,以及execute的返回值, 确定哪一个jsp,
放到View对象的jsp字段中。
*/


return null;
}

}
Loading

0 comments on commit e8317a1

Please sign in to comment.