Java第一阶段基础练习

package org.anbo.msbexam;
/**
 * 排序。。。
**算法:控制语句+数组:
        (1):10以类奇数JC和...
        (2):费波那契数列(三种写法//变种)...
        (3):递归(内存执行图)
        (4):水仙花数
        (5):100-200质数和
        (6):二分法查某数在数组中的位置
        (7):矩阵对角线元素和(五子棋修饰边符,判断输赢)
        (8):生成一个在3-7之间的数
        (9):500个小孩数三退一,求最后一个的位置
        (10):杨辉三角
//----------------------
1:String常用方法(判断输入多少个类型的字符);
	2:File类遍历目录(递归)...
	    递归:就是允许方法内部调用方法自身的一种方法,是设计和描述算法的一种有力的工具。
	3:String temp = new String("tom");内存图

**5:正则表达式: 
  
//-------------
第六章:GUI编程(AWT,Swing)
	**图书管理系统......
	**两人五子棋......
	**记事本......
	**豆子吃妖精......
	**模拟QQ软件......
    **Java计算器……
//--------------------
**线程互斥(模似生产,消费者...)
//---------------
*读写文件(byte[]提高速度),对象Object操作……
//---------
集合:
*(1):Distinct Words Detected!(Map,输入String次数计数器);
*(2):hashSet示例(重写equals,hashCode);
* (3):  Date[]的比较大小[排序]
------------------------
JDBC;JDBC_ODBC:
----------------------------
*(1):CRUD CREATE READ UPDATE DELETE 插入数据,更新,读取,删除 
 *(2):表id 序列;  插入一条记录后,取得id值;;
 *(3):二个sql一个事务;;insert update 出错回滚
 *(4):一个循环 1000 insert . 0.5s
 *(5):存储过程...函数,,,查询表,,,取出来,
 *(6):图片存数据库;;
-------------
 * */
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyJ2seExam{
	int year = 0;
	int age = 0;
	//一千瓶子算法...
	public void sum1000(){
		int empty = 0;
		int drink = 0; 
		for(int sum = 1000; sum >= 0; --sum){
			empty ++;
			drink ++;
			if(empty == 3){
				sum = sum + 1;;
				empty = 0;
			}
		}
		System.out.println(empty + " : " + drink);
	}
	//水仙花数...
	public void  GetAaffodil(){
		for(int i = 100; i < 1000; i++){
			int j= 0 ,k = 0,l = 0;
			j = i % 10;
			k = i / 100;
			l = (i % 100)/10;
			if(j*j*j + k*k*k + l*l*l == i){
				System.out.println(i);
			}
		}
	}
	//九九乘法表...
	public void MultiplicationTable(){
		for(int i = 1; i < 10; i++){
			for(int j = 1; j < i+1; j++){
				if(i*j < 10){
					System.out.print(i*j + "   ");
				}else{
				    System.out.print(i*j  + "  ");
				}
			}
			System.out.println();
		}
	}
	//打印星阵...
	public void GetStar(int row){
		for(int i = 0; i < row; i++){
			for(int j = 0; j < row - i; j++){
			    System.out.print(" ");	
			}
			for(int k = 0; k < 2*i+1; k++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
	//一百以内的质数(素数)...
	public void GetPrimeNumber(){
		for(int i = 1; i < 100; i+=2){
			boolean isprime = true;
			for(int j = 2; j < i; j++){
				if(i%j == 0){
					isprime = false;
					break;
				}
			}
			if(!isprime){
				continue;
			}else{
				System.out.println(i);
			}
		}
	}
	//计算数字m和n的最小公倍数。(还有最大公约数)...
	public void GetNumber(int m, int n){
	    int total = m*n;
	    int r = 0; 
	    for(int i = 1; i < m; i++){
	    	for(int j = 1; j < n; j++){
	    		if(m%i == 0 && n%j == 0){
	    			r = i;
	    		}
	    	}
	    }
	    System.out.println("最大公约数: " + r + "最小公倍数: " + total/r);
	}
	//1到m的jc和
	public void Sumjc(int m){
		int sum = 0; 
		int jc = 1;
		for(int i = 1; i <= m; i++){
			jc = jc * i;
			sum += jc;
		}
		System.out.println(m+"的jc是:" + jc + ": jc和是: "+sum);
	}
	// 如果苹果 1元/个, 桔子 2 元/个, 芒果 4元/个,若是用10元去买,	有几种组合呢?
	public void GetN(){
		int sum = 10;
		for(int i = 0; i < sum/1; i++){
			for(int j = 0; j < sum/2; j++){
				for(int k = 0; k < sum/2; k++){
					if(i*1+j*2+k*4 == 10){
						System.out.println(i+": "+j+": "+k);
					}
				}
			}
		}
	}
	//费波那契数列...
	//----------------------------
	public int Fab1(int m){
		if(m == 1 || m == 2){
			return 1;
		}else{
			return Fab1(m - 1) + Fab1(m - 2);
		}
	}
	public long Fab2(int m){
		long f1 = 1L;
		long f2 = 1L;
		long f = 0;
		if(m == 1 || m == 2){
			return 1;
		}else{
			for(int i = 0; i < m - 2; i++){
				f = f1 + f2;
				f2 = f1;
				f1 = f;
			}
		}
		return f;
	}
	public int[] ShowFab(int m){
		int[] list = new int[m];
		list[0] = 1;
		list[1] = 1;
		for(int i = 2; i< list.length; i++){
			list[i] = list[i-1]+list[i-2];
		}
		return list;
	}
	/*一个农场有头母牛,现在母牛才一岁,要到3岁才能生小牛,3岁之后
	   每年生一头小牛。假设每次生的都是母牛,并且也遵守3年才生育并生母牛的原则,
	   并且无死亡,请问n年后共有多少头牛?
	 */
	 static int count = 1;
	private static void feedCow(int year,int age){
		 year++;
		 age++;
		 if(year<=10){
		     if(age>=3){
		         count++;
		         feedCow(year,0);
		   }
		  feedCow(year,age);
		 }
	}
	/*
	 * 第二种思路...
	 * Java codepublic class Cow {
         public static int count = 0;
          public Cow(int year){
            count++;
            for(int i=3+year;i<=10;i++){
               new Cow(i);
        }
    }
    public static void main(String[] args) {
        new Cow(0);
        System.out.println(count);
    }
   }
*/
    /*题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
       几个数相加有键盘控制。 程序分析:关键是计算出每一项的值。 
     * */
    public int GetSum(int m){
    	int sum = 0; 
    	int sum1 = 0;
    	int sum2 = 1;
    	for(int i = 0; i <  m; i++){
    		sum1 += m * sum2;
    		sum2 *= 10;
    		sum += sum1;
    	}
    	return sum;
    }
    /*题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;
     * 再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? 
     * */
    public void GetBang(){
    	double SUMM = 100;
    	double xm = 0;
    	double sum = 0;
    	for(int i = 0; i < 10; i++){
            xm = SUMM/2;
            sum += SUMM + xm;
            SUMM = xm;
    	}
    	System.out.println(sum + " : " + SUMM);
    }
    /*题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 
       1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
     * */
    public void SetNumber(int x,int y,int z){
        if(x > y){
        	y = x;
        }else if(y > z){
        	z = y;
        }   
        System.out.println("最大数为: " + z);
    }
    //题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 
    public void NewFab(){
    	double newFab;
    	//初始化数组...
    	int[] lists = new int[25];
    	lists[0] = 1;
    	lists[1] = 1;
    	for(int i = 2; i < lists.length; i++){
    		lists[i] = lists[i-1] + lists[i-2];
    	}
    	for(int i = 2; i < 20+2; i++){
    		newFab = (double)lists[i]/lists[i-1];
    		System.out.println(lists[i] + "/" + lists[i-1]+ " : "+newFab);
    	}
    }
    //题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。 
    public void GetCount(int m){
    	//m = 87654
    	int m1 = m%10;
    	int m2 = (m/10)%10;
    	int m3 = (m/100)%10;
    	int m4 = (m/1000)%10;
    	int m5 = m/10000;
    	if(m5 != 0){
    		System.out.println("该数是五位数");
    		System.out.println(m1 * 10000 + m2*1000 + m3*100 + m4*10 + m5);
    	}else if(m4 != 0){
    		System.out.println("该数是四位数");
    		System.out.println(m1 * 1000 + m2 * 100 + m3 * 10 + m4);
    	}else if(m3 != 0){
    		System.out.println("该数是个三位数");
    	    System.out.println(m1 * 100 + m2 * 10 + m3);
    	}else if(m2 != 0){
    		System.out.println("该数是个二位数");
    		System.out.println(m1 * 10 + m2);
    	}else if(m1 != 0){
    		System.out.println("该数是一位数");
    		System.out.println(m);
    	}
    }
    /*题目:求一个3*3矩阵对角线元素之和 
      1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。 */
    public void GetSum_JZ(){
    	int sum = 0;
    	int sum1 = 0;
    	int sum2 = 0;
    	int[][] lists = new int[3][3];
    	for(int i = 0; i < lists.length; i++){
    		for(int j = 0; j < lists.length; j++){
    		    lists[i][j] = (int)(Math.random() * 100);
    		    System.out.print(lists[i][j] + "   ");
    		}
    		System.out.println();
    	}
    	for(int i = 0; i < lists.length; i++){
    		sum += lists[i][i] + lists[i][lists.length-1 - i];
    	}
    	if(lists.length%2 != 0){
    	    sum = sum - lists[lists.length/2][lists.length/2];
    	}
    	System.out.println(sum);
    }
    //杨辉三角  ?????
    private int tri[][];
    public static final int defaultLine = 10;
    //添加元素...
    public void yanghuiTri(){
   	    int i , j;
   	    for(i = 0; i < tri.length; ++i){
   	    	tri[i][0] = tri[i][i] = 1;
   	    }
   	    for(i = 2; i < tri.length; ++i){
   	    	for(j = 1;j < i;++j){
   	    		tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
   	    	}
   	    }
    }
    //初始化,程序入口
    public void defaultTri(int line){
    	tri = new int[line][];
    	for(int i = 0; i < line; i++){
    		tri[i] = new int[i+1];
    	}
    	yanghuiTri();
    	showTri();
    }
    //显示扬辉三角...
    public void showTri(){
    	for(int i = 0; i < tri.length; i++){
    		for(int j = 0; j <= i; j++){
    			System.out.print(tri[i][j] + "  ");
    		}
    		System.out.println();
    	}
    }
    //  500个小孩手拉手连成一个圈,数1,2,3数到三退一,问最后一个index
     public static void getTrue(){
    	boolean[] arr = new boolean[500];
    	for(int i = 0; i < arr.length; i++){
    		arr[i] = true;
    	}
    	int index = 0; 
    	int sum = arr.length;
    	int countSum = 0; 
    	while(sum > 1){
    		if(arr[index] == true){
    			countSum++;
    		}
    		if(countSum == 3){
    		    countSum = 0;
    		    arr[index] = false;
    		    sum--;
    		}
    		index++;
    		if(index == arr.length){
    			index = 0;
    		}
    	}
    	for(int i = 0; i < arr.length; i++){
	    	if(arr[i] == true){
	    		System.out.println(i);
	    		//这是问的是第几个人,正如他们所说,最后的结果应该+1吧;
	    	}
	    }
    }
     
    //二分查找法
     public static int binarySearch(int[] a,int i){
     	if(a.length == 0) return -1;
     	int startPos = 0;
     	int endPos = a.length -1;
     	int m = (startPos + endPos) /2;
     	while(startPos <= endPos){
     		if(i == a[m]) return m;
     		if(i > a[m]){
     			startPos = m + 1;
     		}
     		if(i < a[m]){
     			endPos = m - 1;
     		}
     		m = (startPos + endPos) / 2;
     	}
     	return -1;
     }
     //系统自带....
     public void FindIndex(int[] list){
     	Arrays.sort(list);
     	int i = Arrays.binarySearch(list, 4); //返回4在list中第一次出现的Index;
     	System.out.println(i);
     }
   
     //数组排序:
     public int[] order(int[] temp ,int type){
    	 //type == 1表示冒泡..
    	 //type == 2表示插入排序
    	 if(type == 1){
    		 long time1 = System.currentTimeMillis();
    		 for(int i = 0; i < temp.length; i++){
    	    	 for(int j = 0; j < temp.length - 1; j++){
    	    		if(temp[j] > temp[j + 1]){
    	    			int t = temp[j];
    	    			temp[j] = temp[j + 1];
    	    			temp[j + 1] = t;
    	    		} 
    	    	 }
        	 }
    		 long time2 = System.currentTimeMillis();
     		 long userTime = time2 - time1; 
     		 System.out.println("此次排序所用时间为:" + userTime);
    	 }else{
    		 for(int i = 0; i < temp.length; i++){
    			 for(int j = 0; j < temp.length - 1; j++){
    				 if(temp[i] > temp[j]){
    					 int t = temp[i];
    					 temp[i] = temp[j];
    					 temp[j] = t;
    				 }
    			 }
    		 }
    	 }
    	 return temp;
     }
     //String常用方法
     //1:(判断输入多少个类型的字符);
    public static void getNum(){
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String temp = null;
    	try {
			temp = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    String b1 = "abcdefghijklmnopqrstuvwxyz";
	    String b2=  "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	    int b1count = 0;
	    int b2count = 0;
	    
	    for(int i = 0; i < temp.length(); i++){
	    	char c = temp.charAt(i);
	    	if(b1.indexOf(c) != -1){
	    		b1count++;
	    	}
	    	if(b2.indexOf(c) != -1){
	    		b2count++;
	    	}
	    }
	    System.out.println("输入小写字母:" + b1count + "输入大写字母:" + b2count);
    }
   // 2:拆分一个String 装入double[][]中 :
    public static void getArray(){
		double[][] d;
	    String s = "1,3;5,6,8;4,9";
	    String[] ss = s.split(";");
	    d = new double[ss.length][];
	    
	    for(int i = 0; i < ss.length; i++){
	    	String[] sSecond = ss[i].split(",");
	    	 d[i] = new double[sSecond.length];
	    	for(int j = 0; j < sSecond.length; j++){
	    		d[i][j] = Double.parseDouble(sSecond[j]);
	    	}
	    }
	    for(int i = 0; i < d.length; i++){
	    	for(int j = 0; j < d[i].length; j++){
	    		System.out.print(d[i][j] + "  ");
	    	}
	    	System.out.println();
	    }
    }
   // File类遍历目录(递归)...
   // 递归:就是允许方法内部调用方法自身的一种方法,是设计和描述算法的一种有力的工具。
    public static void tree(File f,int level){
		String preStr = "  ";
		for(int i = 0; i < level; i++){
			preStr += "  ";
		}
		File[] childs = f.listFiles();
		for(int i = 0; i < childs.length; i++){
			System.out.println(level+preStr+childs[i].getName());
			if(childs[i].isDirectory()){
				tree(childs[i],level + 1);
			}
		}
	}

    //**5:正则表达式:
    public  static void getReg(){
    	//System.out.println("abc".matches("..."));
    	//匹配...三个字母
    	Pattern p = Pattern.compile("\\w+");
    	String temp = "abc,db,34bn,66c,678,gkd";
    	Matcher m = p.matcher(temp);
    	while(m.find()){
    		System.out.println(m.group());
    	}
    }
    
    //枚举类型...
	public enum MyColor{red,green,blue};
	public static void myColor(){
		MyColor m = MyColor.green;
		switch(m){
		case red :
				System.out.println(m.ordinal()); //返回序数int
			break;
		case green :
			System.out.println(m.ordinal());
			break;
		case blue : 
			System.out.println(m.ordinal());
			break;
		}
	}
	//Distinct Words Detected!(Map,输入String次数计数器);
	public void distinctMap(){
			Map m = new HashMap();
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String temp;
			try {
				temp = br.readLine();
				String[] temps = temp.split(",");
				for(int i = 0; i < temps.length; i++){
					Integer freq = (Integer)m.get(temps[i]);
					if(freq == null){
						m.put(temps[i], 1);	
					}else{
						m.put(temps[i], freq + 1);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			System.out.println(m.size() + " ");
			System.out.println(m);
		}
	
    //遍历集合...
	public void lookColl(){
		Collection coll = new HashSet();
		coll.add(new Cat(1,"tom"));
		coll.add(new Cat(2,"jarry"));
		coll.add(new Cat(3,"jack"));
		coll.add(new Cat(1,"tom"));
		
		//System.out.println(coll + "   ");  
		
		Iterator itor = coll.iterator();
		while(itor.hasNext()){
			System.out.println(itor.next());
		}
		System.out.println("----------------");
	} 
	//用Collections.sort排序....
	//类,要实现Comparable / compareTo方法!
	public void orderColl(){
		Collection coll2 = new LinkedList();
		coll2.add(new Cat(2,"tom"));
		coll2.add(new Cat(1,"jarry"));
		coll2.add(new Cat(3,"jack"));
		coll2.add(new Cat(2,"tom"));
		
		System.out.println(coll2 + "  ");
		System.out.println("----------");
		
		Collections.sort((LinkedList)coll2);
		System.out.println("排序完成结果为:");
		System.out.println(coll2 + "  ");
	}
	//--------------
	public static void main(String[] args){
      /*测试排序...
		int[] temp = {5,7,1,3,9,4};
		new MyJ2seExam().order(temp,2);
		for(int i : temp){
			System.out.println(i);
		}
		*/
		new MyJ2seExam().getReg();
	}
}
//在用Collections.sort排序的时候
//一定要实现接口Comparbale的
//集合测试类:Cat
class Cat implements Comparable{
	
	private int i;
	private String name;
	public Cat(int i,String name){
		this.i = i;
		this.name = name;
	}
	public String getName(){
		return this.name;
	}
	public String toString(){
		return this.name;
	}
	//lookColl为了不添加重复项,得重写equals,hashCode方法!
	public boolean equals(Object o){
		if(!(o instanceof Cat)) return false;
		Cat c = (Cat)o;
		if(this.i == c.i && this.name == c.name){
			return true;
		}else{
			return false;
		}
	}
	public int hashCode(){
		return i;
	}
	public int compareTo(Object o) {
	    if(!(o instanceof Cat))  return 0;
	    Cat c = (Cat)o;
	    if(this.i > c.i){
	    	return 1;
	    }else if(this.i < c.i){
	    	return - 1;
	    }else{
	    	return 0;
	    }
	}
}

/**
    //-----------i++;++i区别----------
    public void printijk(){
    	int i = 3; 
    	int j = i++;
    	//System.out.println("i");     此处值应该是:4
    	int k = ++i;
    	System.out.println(i+" : "+j+" : "+k);    //5,3,5
    }
    //-----------判断输入月份----------------
    public void Month(int m){
    		switch((m -1)%3 ){
    		case 0: System.out.println("春天");  break;
    		case 1: System.out.println("夏天");  break; 
    		case 2: System.out.println("秋天");  break;
    		case 3: System.out.println("冬天");  break;
    		default : System.out.println("输入错误"); break;
        	}
    }
    //--------输出菱形--------
    public void PrintStart(int row){
    	for(int i = 0; i < row; i++){
    		for(int j = 0; j < row - i; j++){
    			System.out.print(" ");
    		}
    		for(int k = 0; k < 2*row-1; k++){   //k <  2*i-1  可以输出三角
    			System.out.print("*");
    		}
    		System.out.println();
    	}
    }
    //----------参数传递-------------
    public void SetParameter(){
    	StringBuffer s1 = new StringBuffer("yes");
    	StringBuffer s2 = new StringBuffer("no");
    	GetParameter(s1,s2);      //可以直接调用同类型普通方法...
    	System.out.println(s1 + " : " + s2);    // yes是的意思 :no
    }
    public void GetParameter(StringBuffer b1,StringBuffer b2){//这里会拷贝引用s1和s2,拷贝成b1和b2
    	b1.append("是的意思");//改变了b1,也影响到了引用s1,
    	b2 = b1;    //把引用b1指向了引用b2,b1,b2,s1指向同一个对象...
    }
    
    //------------String,StringBuffer,StringBuilder-----------------
    
    public void StringExam(){
    	String s1 = "Do The Best Yourself!";
    	String s2 = "do the best yourself!";
    	char c1 = s1.charAt(5);    //返回Index位置的char
    	int values = s1.compareTo(s2);   //比较大小,分大小写
    	String s3 = s1.concat(s2);      //把s1两字符串s2连接起来.原字符串不变
    	int a = 10;  	System.out.println(s1 + a + 6);   //s11020
    	//  ==   equals   区别
    	String s4 = new String("do the best yourself!");
    	boolean b1 = (s4 == s2);   //b1为false;
    	boolean b2 = (s4.equals(s2));      //b2为true;
    	byte[] Sbyte = s1.getBytes();    //将s1转变为byte数组..
    	int i = s1.indexOf('Y');          //返回字符'Y',所在Index
    	String s5 = s1.replace('B', 'f');       //替换字符生成新串..
    	boolean b3 = s1.startsWith("D");        //是否以字符D开头...
    	String s6 = s1.substring(3,6);         //截取字符串
    	char[] sbyte = s1.toCharArray();        //把字符串转换成char数组
    	String s7 = s1.toLowerCase();         //把字符大写的全转换成小写
    	
    	StringBuffer sb = new StringBuffer("user is amdin");
    	sb.append("good day");     //追加字符
    	sb.deleteCharAt(5);       //删除index位置上的字符...
    	sb.insert(5, false);
    	sb.setCharAt(3, 'Y');         //修改index 3  位置上的值
     }
   /*---------------System----------------------
    gc方法
    currentTimeMillis
    arraycopy
    exit方法
    getProperty 
     
	public static void main(String[] args) {
		TextWork t2 = new TextWork();
		t2.SetParameter();    
        TestStatic t1 = new TestExtends();   
        //多态  基类变量,子类实例,
        //t1能调基类的非private属性方法,和子类中重写了的方法,不能调用只类新写方法...
	}
}
//--------一些关键字--------------
    //final声明类,表示类不能被继承...
 final class Text{
	 //final声明变量为常量,不可更改...
	 private final int SIZE = 15;
	 //final声明方法,表示不能被重写...
	public final int run(){
		return 0;
	}
}
class TestStatic{
	//静态块的理解...
	static int s_a=1; 
    static 
    { 
       s_a=11; 
       s_b=22; 
     } 
     static int s_b=2; 
      //System.out.println(t1.s_a);  // 11;
	  //System.out.println(t1.s_b);  // 2;
     
	private static int SIZE;    //对SIZE实行封装
	
	public TestStatic(){
		System.out.println("如果没声明系统会默认构造方法..");
	}
	//构成方法重载,在同一个class里,方法名相同,参数个数或类型不同...
	public TestStatic(int i){
		this();  //this调用无参构方法
	}  //私有的属性不能直接调用,但能实现它的调用.
	public void SetSize(int size){
		this.SIZE = size;
	}
	public int GetSize(){
		return SIZE; 
	}
}
class TestExtends extends TestStatic{
	private int i = 10;
	public TestExtends(){
		super(); //调用基类的构造方法...
	}
	public int GetSize(){    //重写基类的GetSize方法...
		return i;
	}
	public void init(){
		System.out.println("这是子类的新方法");
	}
}
abstract class Animal{
	public abstract void run();
	public void init(){
		System.out.println();
	}
}
 interface People{
	static final int TABLE_SIZE = 10;
	void ok();  //默认静态,final
}
 interface People1{
    static final int TABLE_SIZE = 10;
	void yes();  //默认静态,final
}
class Tom extends Animal implements People,People1{
	public void run(){
		System.out.println("抽像类抽像方法必须被重写");
	}
	public void yes(){
		System.out.println("要实现接口中的所有方法");
	}
	public void ok(){
		
	}
}
//------------日期处理--------------
class MyDate {
	Date mydate;
	public void run(){
		//用Date输出当前日期:
		mydate = new Date();
		int year = mydate.getYear() + 1900;
		int month = mydate.getMonth() + 1;
		int date = mydate.getDate();
		int hour = mydate.getHours(); 
		int minute = mydate.getMinutes();
		int second = mydate.getSeconds();
		System.out.println("当前日期为:"+year+"年"+month+"月"
				+date+"日"+"  :"+hour+":"+minute+":"+second);
		//用Calendar输出当前时间:
		Calendar dat = Calendar.getInstance();
		int cyear = dat.get(Calendar.YEAR);
		int cmonth = dat.get(Calendar.MONTH) + 1;
		int cdate = dat.get(Calendar.DATE);
        int chour = dat.get(Calendar.HOUR_OF_DAY);
        int cminute = dat.get(Calendar.MINUTE);
        int csecond = dat.get(Calendar.SECOND);
    	System.out.println("当前日期为:"+cyear+"年"+cmonth+"月"
				+cdate+"日"+"  :"+chour+":"+cminute+":"+csecond);
    	//SimpleDateFormat  日期转换....
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd  HH:mm:ss"); 	
      	String jack = sdf.format(new Date());
      	System.out.println(jack);
      	try{
      	Date tom = sdf.parse(jack);
      	System.out.println(tom);
      	}catch(Exception e){
      		e.printStackTrace();
      	}
	}
}
 */
 
/**
 * 
 */
package org.anbo.msbexam;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author lyh
 * @authorMar 8, 2010
 */
public class ExamIO {
	
	FileInputStream fis = null;
	FileOutputStream fos = null;
	int count = 0;
	public void copyFile(){
		try {
			fis = new FileInputStream("d:/史啸吟-断桥.wma");
			fos = new FileOutputStream("d:/index.wma");
			
			byte[] buff = new byte[512];
			try{
			    int temp = fis.read(buff);
			    while(temp != -1){
			    	fos.write(buff);
			    	temp = fis.read(buff);
			    }
			}catch(IOException e){
				e.printStackTrace();
			}
			/*一个字节一个字节的读取
			try {
				int temp = fis.read();
				while(temp != -1){
					fos.write(temp);
					count++;
					temp = fis.read();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			*/
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("文件Copy成功");
	}
	//copy Object
	public void saveObject(){
		Tiger tiger = new Tiger(1,"tom");
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream("d:/index.dat");
			oos = new ObjectOutputStream(fos);
			oos.writeObject(tiger);
			oos.flush();
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("saveSuccess!");
	}
	public void readObject(){

		ObjectInputStream ois = null;
			try {
				fis = new FileInputStream("d:/index.dat");
				ois = new ObjectInputStream(fis);
				Tiger t = (Tiger)ois.readObject();
				System.out.println("读取到的对像名字:" + t.getName());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		
	}
	//--------------
    public static void main(String[] args){
    	new ExamIO().readObject();
    }
}
//保存Object一定要序列化
class Tiger implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	private int i;
	private String name;
	
	public Tiger(int i,String name){
		this.i = i;
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
}
 
public class ArrayExam {

	public static void main(String[] args){
		SyncStack ss = new SyncStack();
		Producer p = new Producer(ss);
		Consumer c = new Consumer(ss);
		new Thread(p).start();
		new Thread(c).start();
	}
}
class WoTou{
	int id;
	public WoTou(int id){
		this.id = id;
	}
	public String toString(){
		return "WoTou:" + id;
	}
}

class SyncStack{
	int index = 0;
	WoTou[] arrWT = new WoTou[6];
	public synchronized void push(WoTou wt){
		if(index == arrWT.length){
			try{
			this.wait();
			}catch(Exception e){
				e.printStackTrace();
			}
			this.notify();
		}
		arrWT[index] = wt;
		index++;
	}
	public synchronized WoTou pop(){
		if(index == 0){
			try{
			this.wait();
			}catch(Exception e){
				e.printStackTrace();
			}
			this.notify();
		}
		index--;
		return arrWT[index];
	}
}

class Producer implements Runnable{
	SyncStack ss;
	public Producer(SyncStack ss){
		this.ss = ss;
	}
	public void run(){
	    for(int i = 0; i < 20; i++){
	    	WoTou wt = new WoTou(i);
	    	ss.push(wt);
	    	System.out.println("生产了:" + wt);
	    	try{
	    		Thread.sleep((int)(Math.random() * 1000));
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	    }
	}
}
class Consumer implements Runnable{
	SyncStack ss;
	public Consumer(SyncStack ss){
		this.ss = ss;
	}
	public void run(){
	    for(int i = 0; i < 20; i++){
	    	WoTou wt = ss.pop();
	    	System.out.println("销费了:" + wt);
	    	try{
	    		Thread.sleep((int)(Math.random() * 1000));
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	    }
	}
}
 

你可能感兴趣的:(Java,学习)