[黑马程序员](第45天)一些平时刷的题(2)

------- android培训java培训、期待与您交流! ----------
[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.Collections;  
  4. import java.util.Scanner;  
  5. import java.util.TreeSet;  
  6.   
  7. /** 
  8.  * 编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。 
  9.  *  用Scanner扫描多个字符串 
  10.  *  判断,如果扫描的字符串是end则结束循环 
  11.  *  如果不是存入集合中,因为考虑排序,所以用TreeSet集合 
  12.  *  定义其比较规则,实现comparable 
  13.  *  打印 
  14.  */  
  15. public class Test5   
  16. {  
  17.     public static void main(String[] args)  
  18.     {  
  19.         TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());  
  20.           
  21.         Scanner sc = new Scanner(System.in);  
  22.           
  23.         System.out.print("输入字符串(end结束):");  
  24.           
  25.         while (true)  
  26.         {  
  27.             String str = sc.next();  
  28.               
  29.             if (str.equals("end"))  
  30.                 break;  
  31.             ts.add(str);  
  32.         }  
  33.           
  34.         System.out.println(ts);  
  35.     }  
  36. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. /** 
  6.  *  
  7.  * 编写一个类,增加一个实例方法用于打印一条字符串。并使用反射手段创建该类的对象,并调用该对象中的方法 
  8.  * 
  9.  */  
  10. public class Test6   
  11. {  
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         Class<stringDemo> clazz = stringDemo.class;  
  15.           
  16.         Method method = clazz.getMethod("printDemo",String.class);  
  17.           
  18.         method.invoke(clazz.newInstance(),"Hello java");  
  19.     }  
  20. }  
  21.   
  22. class stringDemo  
  23. {  
  24.     public void printDemo(String str)  
  25.     {  
  26.         System.out.println(str);  
  27.     }  
  28. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5.   
  6. /** 
  7.  *  
  8.  * 定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5,不考虑中文编码问题) 
  9.  * 
  10.  */  
  11. public class Test7   
  12. {  
  13.     public static void main(String[] args)  
  14.     {  
  15.         FileInputStream fis = null;  
  16.         try  
  17.         {  
  18.             fis = new FileInputStream("exercise.txt");  
  19.               
  20.             byte[] b = new byte[5];  
  21.             int num = 0;  
  22.             while ((num = fis.read(b)) != -1)  
  23.             {  
  24.                 System.out.print(new String(b,0,num));                
  25.             }  
  26.         }  
  27.         catch(IOException e)  
  28.         {  
  29.             throw new RuntimeException("读写出错!");  
  30.         }  
  31.         finally  
  32.         {  
  33.             try   
  34.             {  
  35.                 if (fis != null)  
  36.                     fis.close();  
  37.             }  
  38.             catch(IOException e)  
  39.             {  
  40.                 throw new RuntimeException("关闭出错!");  
  41.             }  
  42.         }  
  43.               
  44.           
  45.           
  46.       
  47.     }  
  48. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. /** 
  6.  * 编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 
  7.  * 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 
  8.  * 提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2 ,这次得到的余数就是次低位,如此循环,直到被除数为0为止。 
  9.  * 其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。 
  10.  * 
  11.  * 
  12.  */  
  13. public class Test8  
  14. {  
  15.     public static void main(String[] args)  
  16.     {  
  17.         Scanner sc = new Scanner(System.in);  
  18.         System.out.println("请输入一个字符串:");  
  19.         String str = sc.next();  
  20.         int in = 0;  
  21.           
  22.         try  
  23.         {  
  24.             in = Integer.parseInt(str);  
  25.             long shang = in/2;  
  26.             long yu = in%2;  
  27.               
  28.             StringBuffer sb = new StringBuffer();  
  29.             while (shang != 0)  
  30.             {  
  31.                 yu = shang%2;  
  32.                 shang = shang/2;  
  33.                 sb.append(yu);  
  34.             }  
  35.             System.out.println(sb.reverse().toString());              
  36.         }  
  37.           
  38.         catch (Exception e)  
  39.         {  
  40.             String regx = ".*\\D+.*";  
  41.               
  42.             if (str.matches(regx))  
  43.                 throw new RuntimeException("出现非数字");  
  44.             else  
  45.                 throw new RuntimeException("范围超出");  
  46.         }     
  47.     }  
  48. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2. /** 
  3.  * 将字符串中进行反转。abcde -->edcba 
  4.  *  定义一个StringBuffer 
  5.  *  reverse方法 
  6.  * 
  7.  */  
  8. public class Test9   
  9. {  
  10.     public static void main(String[] args)  
  11.     {  
  12.         StringBuffer sb = new StringBuffer("abcde");  
  13.           
  14.         sb.reverse();  
  15.           
  16.         System.out.print(sb.toString());  
  17.     }  
  18. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Random;  
  5.   
  6. /** 
  7.  * 编写一个程序,获取10个1至20的随机数,要求随机数不能重复。 
  8.  *  
  9.  * 
  10.  */  
  11. public class Test10   
  12. {  
  13.     public static void main(String[] args)  
  14.     {  
  15.         Random r = new Random();  
  16.         HashSet<Integer> hs = new HashSet<Integer>();  
  17.           
  18.         while (hs.size()<10)  
  19.         {  
  20.             hs.add(r.nextInt(20)+1);  
  21.         }  
  22.         System.out.println(hs);  
  23.     }  
  24. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6. import java.util.TreeMap;  
  7.   
  8. /** 
  9.  * 取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)... 
  10.  *   把字符串存到数组中 
  11.  *   因为存在对应关系,所以可以考虑把每个字符存进map集合中 
  12.  *   遍历数组,如果出现字符就存 
  13.  *   因为要转成相应的格式,所以可以考虑StringBuilder 
  14.  *    
  15.  * 
  16.  */  
  17. public class Test11   
  18. {  
  19.     public static void main(String[] args)  
  20.     {  
  21.         String str = "asdasdasdada";  
  22.           
  23.         char[] ch = str.toCharArray();  
  24.           
  25.         TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();  
  26.           
  27.         for (int i=0;i<ch.length;i++)  
  28.         {  
  29.             Integer value = tm.get(ch[i]);  
  30.               
  31.             if (value == null)  
  32.                 tm.put(ch[i], 1);  
  33.             else  
  34.             {  
  35.                 value += 1;  
  36.                 tm.put(ch[i], value);  
  37.             }     
  38.         }  
  39.           
  40.         //通过StringBuilder转换成相应格式  
  41.         StringBuilder sb = new StringBuilder();  
  42.           
  43.         Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();  
  44.           
  45.         for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator(); it.hasNext(); )  
  46.         {  
  47.             Map.Entry<Character,Integer> me = it.next();  
  48.               
  49.             Character key = me.getKey();              
  50.             Integer value =me.getValue();  
  51.               
  52.             sb.append(key+"("+value+")");  
  53.         }  
  54.         System.out.println(sb.toString());        
  55.     }  
  56. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.util.Arrays;  
  7.   
  8. /** 
  9.  * 已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b.txt中的文件内容应为“abcd…………..”这样的顺序。 
  10.  *  
  11.  * 因为已知为一个字符串,所以用字符流读取,因为有重复字符,所以不可以使用Set和Map集合 
  12.  * 所以存到ArrayList集合中,用collections中的sort方法进行排序 
  13.  * 写入到b文件中输出 
  14.  * 
  15.  */  
  16. public class Test12   
  17. {  
  18.     public static void main(String[] args) throws IOException  
  19.     {  
  20.         FileInputStream fis = null;  
  21.         FileOutputStream fos = null;  
  22.           
  23.         try  
  24.         {  
  25.             fis = new FileInputStream("a.txt");  
  26.             fos = new FileOutputStream("b.txt");  
  27.           
  28.             byte[] by = new byte[1024];  
  29.             int num = 0;  
  30.             while ((num = fis.read(by)) != -1)  
  31.             {  
  32.                 Arrays.sort(by);  
  33.                 fos.write(by,0,num);  
  34.             }  
  35.         }  
  36.         catch(IOException e)  
  37.         {  
  38.             e.printStackTrace();  
  39.         }  
  40.         finally  
  41.         {  
  42.             try  
  43.             {  
  44.                 fis.close();  
  45.             }  
  46.             catch(IOException e)  
  47.             {  
  48.                 e.printStackTrace();  
  49.             }  
  50.             try  
  51.             {  
  52.                 fos.close();  
  53.             }  
  54.             catch (IOException e)  
  55.             {  
  56.                 e.printStackTrace();  
  57.             }  
  58.         }     
  59.     }  
  60. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  * 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、售票中心。售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。 
  5.  *  
  6.  * 多个售票窗口一起工作,这是一个多线程考题 
  7.  *  
  8.  * 
  9.  */  
  10. public class Test13   
  11. {  
  12.     public static void main(String[] args)  
  13.     {  
  14.         SealWindow sw = new SealWindow();  
  15.           
  16.         Thread t1 = new Thread(sw);  
  17.         Thread t2 = new Thread(sw);  
  18.         Thread t3 = new Thread(sw);  
  19.           
  20.         t1.start();  
  21.         t2.start();  
  22.         t3.start();  
  23.           
  24.     }  
  25. }  
  26.   
  27.   
  28. class SealWindow implements Runnable  
  29. {  
  30.     TicketSealCenter tsc = new TicketSealCenter(100);  
  31.       
  32.     private int ticket;  
  33.       
  34.     public void run()  
  35.     {  
  36.         System.out.println(Thread.currentThread().getName()+"------"+ticket--);  
  37.     }  
  38. }  
  39.   
  40. class TicketSealCenter  
  41. {  
  42.     private int ticket;  
  43.       
  44.     TicketSealCenter(int ticket)  
  45.     {  
  46.         this.ticket = ticket;  
  47.     }  
  48.   
  49.     public int getTicket()   
  50.     {  
  51.         return ticket;  
  52.     }  
  53.   
  54.     public void setTicket(int ticket)  
  55.     {  
  56.         this.ticket = ticket;  
  57.     }     
  58. }  
  59.   
  60. class Ticket  
  61. {  
  62.     private String name;//车次  
  63.     private int num;//座位号  
  64.       
  65.     public String getName()   
  66.     {  
  67.         return name;  
  68.     }  
  69.       
  70.     public int getNum()   
  71.     {  
  72.         return num;  
  73.     }  
  74. }  

 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  *  
  5.  * 已知一个int类型的数组,用冒泡排序法将数组中的元素进行升序排列。 
  6.  * 
  7.  */  
  8. public class Test14   
  9. {  
  10.     public static void main(String[] args)  
  11.     {  
  12.         int[] arr = {3,5,6,1,3,8,9,3,9};  
  13.           
  14.         for (int x=0;x<arr.length-1;x++)  
  15.         {  
  16.             for (int y=0;y<arr.length-x-1;y++)  
  17.             {  
  18.                 if (arr[y] > arr[y+1])  
  19.                 {  
  20.                     swap(arr,y,y+1);  
  21.                 }  
  22.             }  
  23.         }  
  24.         for (int i=0;i<arr.length;i++)  
  25.         {  
  26.             System.out.println(arr[i]);  
  27.         }  
  28.     }  
  29.       
  30.     public static void swap(int[] arr,int x,int y)  
  31.     {  
  32.         int temp = arr[x];  
  33.         arr[x] = arr[y];  
  34.         arr[y] = temp;  
  35.     }  
  36. }  


 

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. /** 
  4.  * 假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。 
  5.  * 请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。 
  6.  *  
  7.  * 
  8.  */  
  9. public class Test15  
  10. {  
  11.     public static void main(String[] args)  
  12.     {  
  13.         Worker e = new Worker("leilei","007",5000);    
  14.         Manager m = new Manager("hanmeimei","001",10000,20000);    
  15.             
  16.         System.out.println("员工姓名:"+e.getName()+"\t工号:"+e.getId()+"\t工资:"+e.getMoney());    
  17.         System.out.println("经理姓名:"+m.getName()+"\t工号:"+m.getId()+"\t工资:"+m.getMoney()+"\t奖金:"+m.getBouns());    
  18.         }  
  19. }  
  20.   
  21. class Worker  
  22. {  
  23.     private String name;  
  24.     private String id;  
  25.     private long money;  
  26.       
  27.     Worker(String name,String id,long money)  
  28.     {  
  29.         this.name = name;  
  30.         this.id = id;  
  31.         this.money = money;  
  32.     }  
  33.   
  34.     public String getName() {  
  35.         return name;  
  36.     }  
  37.   
  38.     public void setName(String name) {  
  39.         this.name = name;  
  40.     }  
  41.   
  42.     public String getId() {  
  43.         return id;  
  44.     }  
  45.   
  46.     public void setId(String id) {  
  47.         this.id = id;  
  48.     }  
  49.   
  50.     public long getMoney() {  
  51.         return money;  
  52.     }  
  53.   
  54.     public void setMoney(long money) {  
  55.         this.money = money;  
  56.     }  
  57. }  
  58.   
  59. class Manager extends Worker  
  60. {  
  61.     private double bonus;  
  62.       
  63.     Manager(String name,String id,long money,double bouns)  
  64.     {  
  65.         super(name, id, money);  
  66.         this.bonus = bouns;  
  67.     }  
  68.   
  69.     public double getBouns() {  
  70.         return bonus;  
  71.     }  
  72.   
  73.     public void setBouns(double bouns) {  
  74.         this.bonus = bouns;  
  75.     }  
  76. }  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值