-------
android培训、
java培训、期待与您交流! ----------
- 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()+"\t工号:"+e.getId()+"\t工资:"+e.getMoney());
- System.out.println("经理姓名:"+m.getName()+"\t工号:"+m.getId()+"\t工资:"+m.getMoney()+"\t奖金:"+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;
- }
- }