黑马程序员入学测试题答案_第1页
黑马程序员入学测试题答案_第2页
黑马程序员入学测试题答案_第3页
黑马程序员入学测试题答案_第4页
黑马程序员入学测试题答案_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1.定义一个交通灯枚举,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法;例如:红灯获取下一个灯是绿灯,绿灯获取下一个灯是黄灯。public enum Lamp RED("GREEN"),GREEN("YELLOW"),YELLOW("RED");private String next;private Lamp(String next)this.next = next;public Lamp nextLamp()return Lamp.valueOf(next); 2、 写一个ArrayList类的代理,实现和ArrayList中完全相

2、同的功能,并可以计算每个方法运行的时间。public class test1 public static void main(String args) final ArrayList target = new ArrayList();List proxy = (List)Proxy.newProxyInstance(List.class.getClassLoader(), ArrayList.class.getInterfaces(), new InvocationHandler() Overridepublic Object invoke(Object proxy, Method metho

3、d, Object args)throws Throwable long beginTime = System.currentTimeMillis();Thread.sleep(10);Object reVal = method.invoke(target, args);long endTime = System.currentTimeMillis();System.out.println(method.getName()+" runing time is "+(endTime-beginTime);return reVal;);proxy.add("nihaoa

4、");proxy.add("nihaoa");proxy.add("nihaoa");proxy.remove("nihaoa");System.out.println(proxy.toString();3. ArrayList list = new ArrayList();在这个泛型为Integer的ArrayList中存放一个String类型的对象。public class test2 public static void main(String args) throws ExceptionArrayList<In

5、teger> list = new ArrayList<Integer>();Method method = list.getClass().getMethod("add", Object.class);method.invoke(list, "i am a String");System.out.println(list.toString();4、 一个ArrayList对象aList中存有若干个字符串元素, 现欲遍历该ArrayList对象, 删除其中所有值为"abc"的字符串元素, 请用代码实现。public

6、class test4 public static void main(String args) ArrayList<String> aList = new ArrayList<String>();aList.add("abc");aList.add("nihaoa");aList.add("nihaoa");aList.add("abc");aList.add("cdb");aList.add("abc");aList.add("cdb&q

7、uot;);System.out.println(aList.toString();Iterator<String> it = aList.iterator();while(it.hasNext()String str = it.next();if(str.equals("abc")it.remove();System.out.println(aList.toString();5、 编写一个类,增加一个实例方法用于打印一条字符串。 并使用反射手段创建该类的对象, 并调用该对象中的方法。public class test5 public static void m

8、ain(String args)throws Exception Class<myClass> clazz = myClass.class;Method method = clazz.getMethod("printStr", String.class);method.invoke(clazz.newInstance(), "ni hao ma? this my print str");class myClasspublic void printStr(String str)System.out.println(str);6 、 存在一个Ja

9、vaBean,它包含以下几种可能的属性: 1:boolean/Boolean 2:int/Integer 3:String4:double/Double 属性名未知,现在要给这些属性设置默认值,以下是要求的默认值: String类型的默认值为 字符串 int/Integer类型的默认值为100 boolean/Boolean类型的默认值为truedouble/Double的默认值为0.01D.只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现public class test7 public static void main(String

10、 args) throws Exception Class clazz = Class.forName("cn.heima.test.testBean");Object bean = clazz.newInstance();BeanInfo beanInfo = Introspector.getBeanInfo(clazz);/ System.out.println(beanInfo);PropertyDescriptor propertyDescriptors = beanInfo.getPropertyDescriptors();for (PropertyDescrip

11、tor pd : propertyDescriptors) / System.out.println(pd);/ 获取属性名Object name = pd.getName();/ 获取属性类型Object type = pd.getPropertyType();/ 获取get方法Method getMethod = pd.getReadMethod();/ 获取set方法Method setMethod = pd.getWriteMethod();if (!"class".equals(name) if (setMethod != null) if (type = boo

12、lean.class | type = Boolean.class) setMethod.invoke(bean, true);if (type = String.class) setMethod.invoke(bean, "");if (type = int.class | type = Integer.class) setMethod.invoke(bean, 100);if (type = double.class | type = Double.class) setMethod.invoke(bean, 0.01D);if (getMethod != null) S

13、ystem.out.println(type + " " + name + "="+ getMethod.invoke(bean, null);class testBean private boolean b;private Integer i;private String str;private Double d;public Boolean getB() return b;public void setB(Boolean b) this.b = b;public Integer getI() return i;public void setI(Int

14、eger i) this.i = i;public String getStr() return str;public void setStr(String str) this.str = str;public Double getD() return d;public void setD(Double d) this.d = d;7、 定义一个文件输入流,调用read(byte b) 方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5,不考虑中文编码问题)。public class test8 public static void main(String ar

15、gs) FileInputStream fr = null;try fr = new FileInputStream("d:/exercise.txt");byte bt = new byte5;int len = 0;while(len = fr.read(bt)!=-1)for (int i = 0; i < len; i+) System.out.print(char)bti); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();finallyif(fr!=nul

16、l)try fr.close(); catch (IOException e) e.printStackTrace();finallyfr = null;8、 编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位

17、的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。public class test9 public static void main(String args) Scanner sc = null;while (true) sc = new Scanner(System.in);String str = sc.nextLine();int a = 0;if (isOctNumers(str) a = Integer.valueOf(str); else System.out.println("输入不正确,请重新输入");c

18、ontinue;System.out.println(toBinary(a);sc.close();private static boolean isOctNumers(String str) try Integer.parseInt(str);return true; catch (NumberFormatException e) return false;public static String toBinary(Integer decimal) StringBuilder sb = new StringBuilder();int x = 0;while (decimal != 0) x

19、= decimal % 2;decimal = (int) (decimal / 2);sb.append(x);sb.reverse();return sb.toString(); 9、 金额转换,阿拉伯数字转换成中国传统形式。 例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整public class testt10 private static final char data = '零', '壹', '贰', '叄', '肆', '伍', '陆','柒

20、', '捌', '玖' ;private static final char units = '圆', '拾', '佰', '仟', '万', '拾', '佰','仟', '亿', '拾', '佰', '仟' ;SuppressWarnings("resource")public static void main(String args) whi

21、le (true) Scanner sc = new Scanner(System.in);long l = sc.nextLong();System.out.println(convert(l);public static String convert(long money) StringBuffer sbf = new StringBuffer();int uint = 0;while (money != 0) sbf.insert(0, unitsuint+);sbf.insert(0, data(int) (money % 10);money = money / 10;/ 去零retu

22、rn sbf.toString().replaceAll("零仟佰拾", "零").replaceAll("零+万", "万").replaceAll("零+亿", "亿").replaceAll("亿万", "亿零").replaceAll("零+", "零").replaceAll("零圆", "圆");10.取出一个字符串中字母出现的次数。如:字符串:

23、"abcde%kka27qoq" ,输出格式为:a(2)b(1)k(2).public class test1 public static void main(String args) String str = "abcdekka27qoA*&AAAq"CountChar(str);private static void CountChar(String str) char c = str.toCharArray();System.out.println(c);Map<Character, Integer> map = new Lin

24、kedHashMap<Character, Integer>();for (int i = 0; i < c.length; i+) if (ci <= 90 && ci >= 65)|(ci>=97&&ci<=112) if (!(map.keySet().contains(ci) map.put(ci, 1); else map.put(ci, map.get(ci) + 1);StringBuilder sb = new StringBuilder();Iterator<Map.Entry<Charac

25、ter, Integer>> it = map.entrySet().iterator();while (it.hasNext() Map.Entry<Character, Integer> entry = it.next();sb.append(entry.getKey() + "(" + entry.getValue() + ")");System.out.println(sb);/* *11、 将字符串中进行反转。abcde -> edcba */public class test5 public static voi

26、d main(String args) String str = "abcdgrdfgse"System.out.println(reverse(str);private static String reverse(String str) String result = ""char c = str.toCharArray();for (int i = c.length-1; i >= 0 ; i-) result += ci;return result;/* * 12、 * 已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编

27、写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b * .txt中的文件内容应为“abcd.”这样的顺序。 */public class test6 public static void main(String args) FileInputStream fis = null;FileOutputStream fos = null;try fis = new FileInputStream("D:/a.txt");fos = new FileOutputStream("D:/b.txt");byte c = new bytefis.a

28、vailable();while (fis.read(c) != -1) Arrays.sort(c);fos.write(c);fos.flush(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); finally try fis.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();try fos.close(); catch (IOException e) / TODO

29、Auto-generated catch blocke.printStackTrace();/* * 13、 编写一个程序,获取10个1至20的随机数,要求随机数不能重复。 */public class test7 public static void main(String args) System.out.println(getRandom();public static List<Integer> getRandom() Random rd = new Random();ArrayList<Integer> al = new ArrayList<Intege

30、r>();int i = 0;while(i!=10)int r = rd.nextInt(20);if(!al.contains(rd)al.add(r);i+;return al;/* * 14、 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、售票中心。售票中心分配一定数量的票, * 由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。 */public class test8 public static void main(String args) Ticket ticket = Ticket.getInstan

31、ce();ticket.setNumber(1000);new SealWindow("1号窗口").start();new SealWindow("2号窗口").start();new SealWindow("3号窗口").start();new SealWindow("4号窗口").start();class Ticket private static Ticket ticket = new Ticket();private Ticket() public static Ticket getInstance()

32、 return ticket;private int number;public void setNumber(int number) this.number = number;public int getNumber() return number;public boolean isHasTicket() if (number > 0)return true;return false;public void sealTicket() number-;class SealWindow private String name;public SealWindow(String name) t

33、 = name;public void start() Executors.newScheduledThreadPool(1).execute(new Runnable() Ticket ticket = Ticket.getInstance();Overridepublic void run() while (ticket.isHasTicket() synchronized (Ticket.class) if(!ticket.isHasTicket()continue;try Thread.sleep(10); catch (InterruptedException e)

34、/ TODO Auto-generated catch blocke.printStackTrace();ticket.sealTicket();System.out.println(name + "售出"+ (ticket.getNumber() + 1) + "号票"););class TicketSealCenter /* * * 15、 自定义枚举 Week 用于表示星期,Mon,Tue,Wed.要求每个枚举值都有toLocaleString * 方法,用于获得枚举所表示的星期的中文格式 星期一、星期二、星期三. * */public enum W

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论