一些练习题,未写完

package com.gotoheima;

/**
 * 看JDK多少位
 */
public class Test0
{
	public static void main(String[] args)
	{
		String banben = System.getProperty("sun.arch.data.model");
		System.out.println(banben);
	}
}


package com.gotoheima;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * 编写一个类,增强java.io.BufferedReader的ReadLine()方法,使之在读取某个文件时,能打印出行号。
 *	
 *	就是一个模拟BufferedReadLine的程序
 *	定义一个计数器,定义Reader
 *	模拟ReadLine方法,当读取一行,计数器+1,遇到
就表示应该换行
 *	关闭缓冲区
 *
 */
public class Test1 
{
	public static void main(String[] args)
	{
		
		MyLineNumReader mnr = null;
		try
		{
			mnr = new MyLineNumReader(new FileReader("Test.txt"));
			
			String line = null;
			
			while ((line = mnr.myReadLine()) != null)
			{
				System.out.println(mnr.getCount()+":	"+line);
			}
		}
		catch (IOException e)
		{
			System.out.println("读写错误");
		}
		finally
		{
			try
			{
				if (mnr != null)
					mnr.myClose();
			}
			catch (IOException e)
			{
				System.out.println("流关闭错误");
			}
		}
	}
}

//声明一个类,模拟BufferedReader方法
class MyLineNumReader
{
	private Reader r;
	private int count;
	
	//构造函数,使可以被创建对象
	MyLineNumReader(Reader r)
	{
		this.r = r;
	}
	
	//定义一个myReadLine方法,功能是读取一行数据
	public String myReadLine() throws IOException
	{		
			StringBuffer sb = new StringBuffer ();
			
			int num = 0;
			count ++;
			while ((num = r.read()) != -1)
			{
				if (num == '
')
					continue;
				if (num == '
')
					return sb.toString();
				else
					sb.append((char)num);
			}
			if (sb.length() != 0)
				return sb.toString();
			return null;
	}
	
	//必要的读取属性的方法
	public int getCount() {
		return count;
	}
	
	////必要的修改属性的方法
	public void setCount(int count) {
		this.count = count;
	}	
	
	//关流方法
	public void myClose() throws IOException
	{
		r.close();
	}
}

package com.gotoheima;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

/**
 * 编写程序,将指定目录下所有的.java文件拷贝到另一个目的中,将扩展名改为.txt。
 * 	定义一个函数,函数功能是遍历当前目录所有非文件夹的文件
 * 	把后缀是.java文件的文件拷贝到另一个文件夹中
 * 	修改她们的后缀名为.txt,用String的replace方法。
 *
 */
public class Test3 
{
	public static void main(String[] args) 
	{
		File f = new File("F:\java\day20");
		copyFile(f);
	}
	
	//函数功能:把一个文件夹下面的.java结尾的文件复制到copy文件夹下面并改名
	public static void copyFile(File f)
	{
		//创建一个copy文件加,复制后的文件会被装进来
		File copy = new File("copy");
		copy.mkdir();

		//过滤。java文件
		File[] files = f.listFiles(new FilenameFilter()
		{
			public boolean accept(File f,String name)
			{
				return name.endsWith(".java");
			}
		});
		
		//复制文件并改名
		for (File fileName : files)
		{
			BufferedInputStream bis = null;
			BufferedOutputStream bos = null;
				
			try
			{
				bis = new BufferedInputStream(new FileInputStream(fileName));
				//把后缀名.java改成.txt
				String newName = fileName.getName().replace(".java",".txt");
				
				bos = new BufferedOutputStream(new FileOutputStream(new File(copy,newName)));
				
				byte[] by = new byte[1024];
				int num = 0;
				
				while ((num = bis.read()) != -1)
				{
					bos.write(by,0,num);
				}
			}
			catch(IOException e)
			{
				throw new RuntimeException("读写出错");
			}
			finally
			{
				try 
				{
					if (bis != null)
						bis.close();
				}
				catch(IOException e)
				{
					throw new RuntimeException("读取关闭出错");
				}
				try 
				{
					if (bos != null)
						bos.close();
				}
				catch(IOException e)
				{
					throw new RuntimeException("写入关闭出错");
				}
			}
		}
	}
}

package com.gotoheima;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * 
 *  ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。
 *  
 *  这里考察的是反射,获取ArrayList的字节码对象
 *  用Method的方法获取add的字段
 *  绕过编译阶段去添加String类型对象
 *
 */
public class Test4 
{
	public static void main(String[] args) throws Exception
	{
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		Method methodAdd = list.getClass().getMethod("add",Object.class);
		
		methodAdd.invoke(list,"String");
		
		System.out.println(list);
	}
}

package com.gotoheima;

import java.util.Collections;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * 编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
 *	用Scanner扫描多个字符串
 *	判断,如果扫描的字符串是end则结束循环
 *	如果不是存入集合中,因为考虑排序,所以用TreeSet集合
 *	定义其比较规则,实现comparable
 *	打印
 */
public class Test5 
{
	public static void main(String[] args)
	{
		TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("输入字符串(end结束):");
		
		while (true)
		{
			String str = sc.next();
			
			if (str.equals("end"))
				break;
			ts.add(str);
		}
		
		System.out.println(ts);
	}
}

package com.gotoheima;

import java.lang.reflect.Method;

/**
 * 
 * 编写一个类,增加一个实例方法用于打印一条字符串。并使用反射手段创建该类的对象,并调用该对象中的方法
 *
 */
public class Test6 
{
	public static void main(String[] args) throws Exception
	{
		Class<stringDemo> clazz = stringDemo.class;
		
		Method method = clazz.getMethod("printDemo",String.class);
		
		method.invoke(clazz.newInstance(),"Hello java");
	}
}

class stringDemo
{
	public void printDemo(String str)
	{
		System.out.println(str);
	}
}

package com.gotoheima;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * 
 * 定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5,不考虑中文编码问题)
 *
 */
public class Test7 
{
	public static void main(String[] args)
	{
		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream("exercise.txt");
			
			byte[] b = new byte[5];
			int num = 0;
			while ((num = fis.read(b)) != -1)
			{
				System.out.print(new String(b,0,num));				
			}
		}
		catch(IOException e)
		{
			throw new RuntimeException("读写出错!");
		}
		finally
		{
			try 
			{
				if (fis != null)
					fis.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException("关闭出错!");
			}
		}
			
		
		
	
	}
}

package com.gotoheima;

import java.util.Scanner;

/**
 * 编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。
 * 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。
 * 提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2 ,这次得到的余数就是次低位,如此循环,直到被除数为0为止。
 * 其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。
 *
 *
 */
public class Test8
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个字符串:");
		String str = sc.next();
		int in = 0;
		
		try
		{
			in = Integer.parseInt(str);
			long shang = in/2;
			long yu = in%2;
			
			StringBuffer sb = new StringBuffer();
			while (shang != 0)
			{
				yu = shang%2;
				shang = shang/2;
				sb.append(yu);
			}
			System.out.println(sb.reverse().toString());			
		}
		
		catch (Exception e)
		{
			String regx = ".*\D+.*";
			
			if (str.matches(regx))
				throw new RuntimeException("出现非数字");
			else
				throw new RuntimeException("范围超出");
		}	
	}
}

package com.gotoheima;
/**
 * 将字符串中进行反转。abcde -->edcba
 *	定义一个StringBuffer
 *	reverse方法
 *
 */
public class Test9 
{
	public static void main(String[] args)
	{
		StringBuffer sb = new StringBuffer("abcde");
		
		sb.reverse();
		
		System.out.print(sb.toString());
	}
}

package com.gotoheima;

import java.util.HashSet;
import java.util.Random;

/**
 * 编写一个程序,获取10个1至20的随机数,要求随机数不能重复。
 * 
 *
 */
public class Test10 
{
	public static void main(String[] args)
	{
		Random r = new Random();
		HashSet<Integer> hs = new HashSet<Integer>();
		
		while (hs.size()<10)
		{
			hs.add(r.nextInt(20)+1);
		}
		System.out.println(hs);
	}
}

package com.gotoheima;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
 *   把字符串存到数组中
 *   因为存在对应关系,所以可以考虑把每个字符存进map集合中
 *   遍历数组,如果出现字符就存
 *   因为要转成相应的格式,所以可以考虑StringBuilder
 *   
 *
 */
public class Test11 
{
	public static void main(String[] args)
	{
		String str = "asdasdasdada";
		
		char[] ch = str.toCharArray();
		
		TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
		
		for (int i=0;i<ch.length;i++)
		{
			Integer value = tm.get(ch[i]);
			
			if (value == null)
				tm.put(ch[i], 1);
			else
			{
				value += 1;
				tm.put(ch[i], value);
			}	
		}
		
		//通过StringBuilder转换成相应格式
		StringBuilder sb = new StringBuilder();
		
		Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
		
		for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator(); it.hasNext(); )
		{
			Map.Entry<Character,Integer> me = it.next();
			
			Character key = me.getKey();			
			Integer value =me.getValue();
			
			sb.append(key+"("+value+")");
		}
		System.out.println(sb.toString());		
	}
}

package com.gotoheima;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * 已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b.txt中的文件内容应为“abcd…………..”这样的顺序。
 * 
 * 因为已知为一个字符串,所以用字符流读取,因为有重复字符,所以不可以使用Set和Map集合
 * 所以存到ArrayList集合中,用collections中的sort方法进行排序
 * 写入到b文件中输出
 *
 */
public class Test12 
{
	public static void main(String[] args) throws IOException
	{
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try
		{
			fis = new FileInputStream("a.txt");
			fos = new FileOutputStream("b.txt");
		
			byte[] by = new byte[1024];
			int num = 0;
			while ((num = fis.read(by)) != -1)
			{
				Arrays.sort(by);
				fos.write(by,0,num);
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				fis.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
			try
			{
				fos.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}	
	}
}

package com.gotoheima;

/**
 * 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、售票中心。售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。
 * 
 * 多个售票窗口一起工作,这是一个多线程考题
 * 
 *
 */
public class Test13 
{
	public static void main(String[] args)
	{
		SealWindow sw = new SealWindow();
		
		Thread t1 = new Thread(sw);
		Thread t2 = new Thread(sw);
		Thread t3 = new Thread(sw);
		
		t1.start();
		t2.start();
		t3.start();
		
	}
}


class SealWindow implements Runnable
{
	TicketSealCenter tsc = new TicketSealCenter(100);
	
	private int ticket;
	
	public void run()
	{
		System.out.println(Thread.currentThread().getName()+"------"+ticket--);
	}
}

class TicketSealCenter
{
	private int ticket;
	
	TicketSealCenter(int ticket)
	{
		this.ticket = ticket;
	}

	public int getTicket() 
	{
		return ticket;
	}

	public void setTicket(int ticket)
	{
		this.ticket = ticket;
	}	
}

class Ticket
{
	private String name;//车次
	private int num;//座位号
	
	public String getName() 
	{
		return name;
	}
	
	public int getNum() 
	{
		return num;
	}
}
package com.gotoheima;

/**
 * 
 * 已知一个int类型的数组,用冒泡排序法将数组中的元素进行升序排列。
 *
 */
public class Test14 
{
	public static void main(String[] args)
	{
		int[] arr = {3,5,6,1,3,8,9,3,9};
		
		for (int x=0;x<arr.length-1;x++)
		{
			for (int y=0;y<arr.length-x-1;y++)
			{
				if (arr[y] > arr[y+1])
				{
					swap(arr,y,y+1);
				}
			}
		}
		for (int i=0;i<arr.length;i++)
		{
			System.out.println(arr[i]);
		}
	}
	
	public static void swap(int[] arr,int x,int y)
	{
		int temp = arr[x];
		arr[x] = arr[y];
		arr[y] = temp;
	}
}


 

package com.gotoheima;

/**
 * 假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。
 * 请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。
 * 
 *
 */
public class Test15
{
	public static void main(String[] args)
	{
		Worker e = new Worker("leilei","007",5000);  
	    Manager m = new Manager("hanmeimei","001",10000,20000);  
	      
	    System.out.println("员工姓名:"+e.getName()+"	工号:"+e.getId()+"	工资:"+e.getMoney());  
	    System.out.println("经理姓名:"+m.getName()+"	工号:"+m.getId()+"	工资:"+m.getMoney()+"	奖金:"+m.getBouns());  
		}
}

class Worker
{
	private String name;
	private String id;
	private long money;
	
	Worker(String name,String id,long money)
	{
		this.name = name;
		this.id = id;
		this.money = money;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public long getMoney() {
		return money;
	}

	public void setMoney(long money) {
		this.money = money;
	}
}

class Manager extends Worker
{
	private double bonus;
	
	Manager(String name,String id,long money,double bouns)
	{
		super(name, id, money);
		this.bonus = bouns;
	}

	public double getBouns() {
		return bonus;
	}

	public void setBouns(double bouns) {
		this.bonus = bouns;
	}
}



 

package com.gotoheima;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * 编写程序,生成5个1至10之间的随机整数,存入一个List集合,编写方法对List集合进行排序(自定义排序算法,禁用Collections.sort方法和TreeSet),然后遍历集合输出。
 * 
 *
 */
public class Test16 
{
	public static void main(String[] args)
	{
		ArrayList<Integer> al = new ArrayList<Integer>();
		Random r= new Random();
		
		while (al.size()<5)
		{
			al.add(r.nextInt(10)+1);
		}
		
		//排序
		for (int x=0;x<al.size()-1;x++)
		{
			for (int y=0;y<al.size()-x-1;y++)
			{
				if (al.get(y)>al.get(y+1))
					Collections.swap(al, y, y+1);
			}
		}
		
		System.out.println(al);
	}
}


 

package com.gotoheima;

import java.util.Arrays;

/**
 * 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
 * 61.54.231.245
 * 61.54.231.9
 * 61.54.231.246
 * 61.54.231.48
 * 61.53.231.249
 * 
 *
 */
public class Test17
{
	public static void main(String[] args)
	{
		String[] ip = {"61.54.231.245","61.54.231.9","61.54.231.246","61.54.231.48","61.53.231.249"};
		Arrays.sort(ip);
		
		for (int i=0;i<ip.length;i++)
		{
			System.out.println(ip[i]);
		}
	}
}

package com.gotoheima;

/**
 * 
 * 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印:
 * 1	2	3	4
 * 12	13	14	5
 * 11	16	15	6
 * 10	9	8	7
 *
 */
public class Test18 
{
	public static void main(String[] args)
	{
		printMath(60);
	}
	
	public static void printMath(int n)
	{
		int[][] arr = new int[n][n];
		int x = 0;
		int y = 0;
		int max = arr.length-1;
		int min = 0;
		int num = 1;
		
		while (min<=max)
		{
			//right
			while (y<max)
			{
				arr[x][y++] = num++;
			}
			//down
			while (x<max)
			{
				arr[x++][y] = num++;
			}
			//left
			while (y>min)
			{
				arr[x][y--] = num++;
			}
			//up
			while (x>min)
			{
				arr[x--][y] = num++;
			}
			//如果是奇数,可能赋值不上
			if (min == max)
			{
				arr[x][y] = num;
			}
			
			max--;
			min++;
			
			x++;
			y++;
		}
		
		for (int i=0;i<arr.length;i++)
		{
			for (int j=0;j<arr.length;j++)
			{
				System.out.print(arr[i][j]+"	");
			}
			System.out.println();
		}
	}
}


 

package com.gotoheima;

/**
 *  28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路)
 * 	
 * 		三个瓶盖就是一个可乐和一个瓶盖
 *
 */
public class Test19 
{
	public static void main(String[] args)
	{
		System.out.println(buyCoke(28));
		System.out.println(buyCoke(50));
	}
	
	public static int buyCoke(int num)
	{
		int coke = 0;
		int cap = 0;
		int buy = 0;
		
		while (coke < num)
		{
			buy++;
			coke++;
			cap++;
			
			if (cap == 3)
			{
				coke++;
				cap = 1;
			}
		}
		return buy;
	}
}


 

package com.gotoheima;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
 *
 */
public class Test20 
{
	public static void main(String[] args)
	{
		MyLineNumberReader mlnr = null;
		
		try
		{
			mlnr = new MyLineNumberReader(new FileReader("F:\java\day09\Demo.java"));
			
			String len = null;
			
			while ((len = mlnr.myReadLine()) != null)
			{
				System.out.println(mlnr.getCount()+":	"+len);
			}
		}
		catch (IOException e)
		{
			System.out.println("读取出错!");
		}
		finally
		{
			try
			{
				if (mlnr != null)
					mlnr.myclose();
			}
			catch (IOException e)
			{
				System.out.println("关闭出错");
			}
		}
	}
}

class MyLineNumberReader
{
	private Reader r;
	private int count;

	MyLineNumberReader(Reader r)
	{
		this.r = r;
	}
	
	public String myReadLine() throws IOException
	{
		StringBuffer sb = new StringBuffer();
		
		int num = 0;
		count++;
		
		while ((num = r.read()) != -1)
		{
			if (num == '
')
				continue;
			if (num == '
')
				return sb.toString();
			else
				sb.append((char)num);
		}
		if (sb.length() != 0)
			return sb.toString();
		return null;
	}
	
	public void myclose() throws IOException
	{
		r.close();
	}
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}


 

package com.gotoheima;

/**
 * 使用TCP协议写一个可以上传文件的服务器和客户端。
 * 
 *
 */
public class Test21 
{
	public static void main(String[] args)
	{
		
	}
}


 

import java.io.*;
import java.util.*;

/**
	把一个文件夹下的包括子文件夹里的所有.java文件复制到另一个文件夹下面,并改名.txt

	对一个文件夹进行递归,筛选出.java文件,并存到集合中
	用IO字符流操作复制文件,并将.java文件改成.txt
*/

class DirPrintListDemo 
{
	public static void main(String[] args) throws IOException
	{
		//源文件夹
		File dir = new File("F:\java");

		//目标文件夹
		File copy = new File("F:\copy_java");
		if (!copy.exists())
			copy.mkdir();

		ArrayList<File> dirlist = new ArrayList<File>();
		fileToArray(dir,dirlist);
		copy_ReName(dirlist,copy);
		
	}

	//函数功能,把一个文件夹中包括子文件夹的所有.java文件存到dirlist集合中
	public static void fileToArray(File dir,List<File> dirlist)
	{
		File[] files = dir.listFiles();

		for (File file : files )
		{
			if (file.isDirectory())
				fileToArray(file,dirlist);
			else
			{
				if (file.getName().endsWith(".java"))
					dirlist.add(file);
			}
		}
	}

	//函数功能,用IO字符流操作复制文件,并将.java文件改成.txt
	public static void copy_ReName(List<File> dirlist,File copy) throws IOException
	{
		//遍历集合
		for (File files : dirlist)
		{
			BufferedReader br = new BufferedReader(new FileReader(files));
			String newName = files.getName().replace(".java",".txt");
			BufferedWriter bw = new BufferedWriter(new FileWriter(new File(copy,newName)));

			String len = null;
			while ((len = br.readLine()) != null)
			{
				bw.write(len);
				bw.newLine();
				bw.flush();
			}

			br.close();
			bw.close();
		}
	}
}


 

package com.gotoheima;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 编写一个类,在main方法中定义一个Map对象(采用泛型),加入若干个对象,然后遍历并打印出各元素的key和value。
 * 
 *	
 */
public class Test22 
{
	public static void main(String[] args)
	{
		Map<String,Integer> m = new TreeMap<String,Integer>();
		m.put("Lilei", 10);
		m.put("hanmeimei", 9);
		
		//第一种
		Set<Map.Entry<String,Integer>> entrySet = m.entrySet();
		
		for (Iterator<Map.Entry<String,Integer>> it = entrySet.iterator();it.hasNext(); )
		{
			Map.Entry<String,Integer> me = it.next();
			
			String key = me.getKey();
			Integer value = me.getValue();
			System.out.println(key+"-------"+value);
		}
		
		//第二种
		Set<String> keySet = m.keySet();
		
		for (Iterator<String> it = keySet.iterator();it.hasNext(); )
		{
			String key = it.next();
			
			Integer value = m.get(key);
			System.out.println(key+"-------"+value);
		}
	}
}


 

package com.gotoheima;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
  a: 21 次 
  b: 15 次
  c:: 15 次
  把: 7 次
  当: 9 次
  前: 3 次
  ,:30 次
 * 
 *
 */
public class Test23 
{
	public static void main(String[] args) throws IOException
	{
		    BufferedReader br = new BufferedReader(new FileReader("面试题.txt"));
		    BufferedWriter bw = new BufferedWriter(new FileWriter("tongji.txt"));
			TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
			
			String len = null;
			while ((len = br.readLine()) != null)
			{
				char[] chs = len.toCharArray();
				
				for (int i=0;i<chs.length;i++)
				{
					Integer value = tm.get(chs[i]);
					if (value == null)
					{
						tm.put(chs[i],1);
					}
					else
					{
						value += 1;
						tm.put(chs[i], value);
					}
				}
			}
	
			Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
			for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();it.hasNext(); )
			{
				Map.Entry<Character, Integer> me = it.next();
				Character key = me.getKey();
				Integer value = me.getValue();
				
				bw.write(key+": "+value+"次");
				bw.newLine();
				bw.flush();
			}
			br.close();
			bw.close();
	}
}


 

package com.gotoheima;

/**
 * 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。
 * 要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null, 应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,
 *  例如,字符不存在,字符存在,传入的数组为null等。
 * 
 *
 */
public class Test24 
{
	public static void main(String[] args)
	{
		char[] chs = {'z','a','c','t','c'};
		
		IsTest it = new IsTest();
		
		System.out.println(it.isTest(chs, 'c'));
		System.out.println(it.isTest(chs, 'v'));
		System.out.println(it.isTest(null, 'c'));
		
	}
}

class IsTest
{
	public int isTest(char[] chs,char ch)
	{
		if (chs == null)
			throw new IllegalArgumentException("传入字符数组不能为空!");
		
		for (int i=0;i<chs.length;i++)
		{
			if (ch == chs[i])//基本数据类型不能用equals,用==
				return i;
		}
		return -1;
	}
}


 

package com.gotoheima;

import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * 已知一个类,定义如下:
  package cn.itcast.heima;
  public class DemoClass {
    public void run()
    {
      System.out.println("welcome to heima!");
    } 
  }
  (1) 写一个Properties格式的配置文件,配置类的完整名称。
  (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。
 * 
 *
 */
public class Test25 
{
	public static void main(String[] args) throws Exception
	{
		Properties prop = new Properties();
		prop.load(new FileReader("cn\itcast\heima\class.propertise"));
		
		String className = prop.getProperty("name");
		Class clazz = Class.forName(className);
		Method method = clazz.getMethod("run", null);
		method.invoke(clazz.newInstance());
	}
}


 

package com.gotoheima;

import java.util.Scanner;

/**
 * 金额转换,阿拉伯数字转换成中国传统形式。例如:101000001010 转换为壹仟零壹拾亿零壹仟零壹拾圆整
 * 
 * 扫描一个数转换字符串存进一个数组中
 * 定义两个字符数组,分别是零壹贰叁肆伍陆柒捌玖,圆拾佰扦萬拾万佰萬仟萬亿拾亿佰亿仟亿萬亿
 *
 */
public class Test26 
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入金额:");
		String str = sc.nextLine();
		
		System.out.println(getChinese(str));
		
		
	}
	
	public static String getChinese(String str)
	{
		String[] str1 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
		String[] str2 = {"整圆","拾","佰","扦","萬","拾万","佰萬","仟萬","亿","拾亿","佰亿","仟亿","萬亿"};
		StringBuilder sb = new StringBuilder();
		
		char[] chs = str.toCharArray();
	
		for (int i=chs.length-1;i>=0;i--)
		{
			
		}
		
		return sb.toString();
	}
}


 

package com.gotoheima;

/**
 * 方法中的内部类能不能访问方法中的局部变量,为什么?
 * 
 * 	因为内部类的生存周期比方法中的局部变量长,局部变量再作用域完成后就会被栈内存释放销毁。要想访问局部变量,那么局部变量必须被final修饰。
 *
 */
public class Test27 {

}


 

package com.gotoheima;

/**
 * 有一个类为ClassA,有一个类为ClassB,在ClassB中有一个方法b,此方法抛出异常,在ClassA类中有一个方法a,
 * 请在这个方法中调用b,然后抛出异常。在客户端有一个类为TestC,有一个方法为c ,请在这个方法中捕捉异常的信息。
 * 完成这个例子,请说出java中针对异常的处理机制。
 * 
 * java中的异常处理机制,谁调用谁处理,如果一直抛出最终会抛给虚拟机
 *
 */
public class Test28 
{
	public static void main(String[] args)
	{
		C.c();
	}
}

class A
{
	public static void a() throws Exception
	{
		B.b();
	}
}	

class B
{
	public static void b() throws Exception
	{
		throw new Exception("b");
	}
} 

class C
{
	public static void c()
	{
		try
		{
			A.a();
		}
		catch(Exception e)
		{
			System.out.println("处理");
		}
	}
}


 

package com.gotoheima;

import java.lang.reflect.Field;

/**
 * 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value. 
 * 
 * public void setProperty(Object obj, String propertyName, Object value) 
 *   {    
 *   } 
 *   
 *	考点是反射
 */
public class Test29
{
	public static void main(String[] args)
	{
		Person p = new Person();  
	    System.out.println(p.name+"---"+p.age); 
	    
	    setProperty(p,"name","Hanmeimei");  
        setProperty(p,"age",19);  
        System.out.println(p.name+"---"+p.age);  
	}
	
	public static void setProperty(Object obj,String propertyName,Object value) 
	{
		try
		{
			Field f = obj.getClass().getDeclaredField(propertyName);
			
			f.setAccessible(true);
			
			f.set(obj, value);
			
			f.setAccessible(false);
		}
		catch (Exception e)
		{
			System.out.println("修改错误");
		}
	}
	
	public static class Person
	{
		public String name = "Lilei";
		private int age = 18;
	}
}


 

package com.gotoheima;

import java.util.LinkedList;

/**
 * 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
 *
 */
public class Test30 
{
	public static void main(String[] args)
	{
		final int num = 14;
		int count = -1;
		
		LinkedList<Integer> ll = new LinkedList<Integer>();
		
		for (int i=0;i<100;i++)
		{
			ll.add(i+1);
		}
		
		while (ll.size() != 1)
		{
			for (int x=1;x<=num;x++)
			{
				count ++;
				if (count>=ll.size())
					count = 0;
			}
			ll.remove(count);
			count--;
		}
		System.out.print(ll.get(0));
	}
}


 

原文地址:https://www.cnblogs.com/runwind/p/4212170.html