黑马入学测试题答案(整理).doc
《黑马入学测试题答案(整理).doc》由会员分享,可在线阅读,更多相关《黑马入学测试题答案(整理).doc(15页珍藏版)》请在三一办公上搜索。
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中完全相同的功能,并可以计算每个方法运行的时间。public class tes
2、t1 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 method, Object args)throws Throwable long
3、 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);proxy.add(nihaoa);proxy.add(nihaoa);proxy.remove(nih
4、aoa);System.out.println(proxy.toString();3. ArrayList list = new ArrayList();在这个泛型为Integer的ArrayList中存放一个String类型的对象。public class test2 public static void main(String args) throws ExceptionArrayList list = new ArrayList();Method method = list.getClass().getMethod(add, Object.class);method.invoke(lis
5、t, i am a String);System.out.println(list.toString();4、 一个ArrayList对象aList中存有若干个字符串元素, 现欲遍历该ArrayList对象, 删除其中所有值为abc的字符串元素, 请用代码实现。public class test4 public static void main(String args) ArrayList aList = new ArrayList();aList.add(abc);aList.add(nihaoa);aList.add(nihaoa);aList.add(abc);aList.add(cdb
6、);aList.add(abc);aList.add(cdb);System.out.println(aList.toString();Iterator 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
7、ain(String args)throws Exception Class 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 、 存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/Boolean
8、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 args) throws Exception Class clazz = C
9、lass.forName(cn.heima.test.testBean);Object bean = clazz.newInstance();BeanInfo beanInfo = Introspector.getBeanInfo(clazz);/ System.out.println(beanInfo);PropertyDescriptor propertyDescriptors = beanInfo.getPropertyDescriptors();for (PropertyDescriptor pd : propertyDescriptors) / System.out.println(
10、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 = boolean.class | type = Boolean.class) setMethod.invoke(bean, true)
11、;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) System.out.println(type + + name + =+ getMethod.invoke(bean, null);class tes
12、tBean 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(Integer i) this.i = i;public String getStr() return str;public void setStr(String str) this.str = str;pu
13、blic 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 args) FileInputStream fr = null;try fr = new FileInputStream(d:/exercise.txt);byte bt = new byte5;int l
14、en = 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!=null)try fr.close(); catch (IOException e) e.printStackTrace();finallyfr = null;8、 编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十
15、进制整数对应的二进制形式。 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。public class test9 public static void main(String args) Scanner sc
16、= 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(输入不正确,请重新输入);continue;System.out.println(toBinary(a);sc.close();private static boolean isOctNumers(String str) try Integer.parseInt(str);return
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 黑马 入学 测试 答案 整理
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-4171576.html