黑马入学测试题答案(整理).doc

上传人:文库蛋蛋多 文档编号:4171576 上传时间:2023-04-08 格式:DOC 页数:15 大小:73.50KB
返回 下载 相关 举报
黑马入学测试题答案(整理).doc_第1页
第1页 / 共15页
黑马入学测试题答案(整理).doc_第2页
第2页 / 共15页
黑马入学测试题答案(整理).doc_第3页
第3页 / 共15页
黑马入学测试题答案(整理).doc_第4页
第4页 / 共15页
黑马入学测试题答案(整理).doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《黑马入学测试题答案(整理).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

17、true; catch (NumberFormatException e) return false;public static String toBinary(Integer decimal) StringBuilder sb = new StringBuilder();int x = 0;while (decimal != 0) x = decimal % 2;decimal = (int) (decimal / 2);sb.append(x);sb.reverse();return sb.toString(); 9、 金额转换,阿拉伯数字转换成中国传统形式。 例如:10100000101

18、0 转换为 壹仟零壹拾亿零壹仟零壹拾圆整public class testt10 private static final char data = 零, 壹, 贰, 叄, 肆, 伍, 陆,柒, 捌, 玖 ;private static final char units = 圆, 拾, 佰, 仟, 万, 拾, 佰,仟, 亿, 拾, 佰, 仟 ;SuppressWarnings(resource)public static void main(String args) while (true) Scanner sc = new Scanner(System.in);long l = sc.next

19、Long();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;/ 去零return sbf.toString().replaceAll(零仟佰拾, 零).replaceAll(零+万, 万).repla

20、ceAll(零+亿, 亿).replaceAll(亿万, 亿零).replaceAll(零+, 零).replaceAll(零圆, 圆);10.取出一个字符串中字母出现的次数。如:字符串: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(

21、);System.out.println(c);Map map = new LinkedHashMap();for (int i = 0; i c.length; i+) if (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();IteratorMap.Entry it = map.entrySet().iterator();while (it.hasNex

22、t() Map.Entry entry = it.next();sb.append(entry.getKey() + ( + entry.getValue() + );System.out.println(sb);/* *11、 将字符串中进行反转。abcde - edcba */public class test5 public static void main(String args) String str = abcdgrdfgse;System.out.println(reverse(str);private static String reverse(String str) Stri

23、ng result = ;char c = str.toCharArray();for (int i = c.length-1; i = 0 ; i-) result += ci;return result;/* * 12、 * 已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b * .txt中的文件内容应为“abcd.”这样的顺序。 */public class test6 public static void main(String args) FileInputStream fis = null;

24、FileOutputStream fos = null;try fis = new FileInputStream(D:/a.txt);fos = new FileOutputStream(D:/b.txt);byte c = new bytefis.available();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.clo

25、se(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();try fos.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/* * 13、 编写一个程序,获取10个1至20的随机数,要求随机数不能重复。 */public class test7 public static void main(String args) System.out.println(getRandom

26、();public static List getRandom() Random rd = new Random();ArrayList al = new ArrayList();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

27、 class test8 public static void main(String args) Ticket ticket = Ticket.getInstance();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()

28、public static Ticket getInstance() .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;pu

29、blic SealWindow(String name) this.name = 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); c

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

31、 enum Week Mon Overridepublic String toLocaleString() return 星期一;,Tue Overridepublic String toLocaleString() return 星期二;,Wed Overridepublic String toLocaleString() return 星期三;,Thu Overridepublic String toLocaleString() return 星期四;,Fri Overridepublic String toLocaleString() return 星期五;,Sat Overridepu

32、blic String toLocaleString() return 星期六;,Sun Overridepublic String toLocaleString() return 星期天;/*private String next;private Week(String next)*/public abstract String toLocaleString();/* * 16、 已知一个int类型的数组,用冒泡排序法将数组中的元素进行升序排列。 * author Administrator * */public class test1 public static void main(Str

33、ing args) /定义一个数组int arr = new int2,1,5,4,6,4,5,4,5,4,10,9;/调用BubbleSort方法对数组进行排序BubbleSort(arr);/输出排序后的数组System.out.println(Arrays.toString(arr);/* * 这一个使用冒泡排序使int型数组内元素按照升序重新排序的算法 * param arr 接收一个int型数组 */public static void BubbleSort(int arr)/定义一个临时变量int temp = 0; for (int i = arr.length - 1; i 0; -i) for (int j = 0; j arrj+1) /重复比较相邻元素,如果前一个比后一个大,则交换. /利用中间变量temp,对arrj和arrj+1对换 temp = arrj; arrj = arrj + 1; arrj + 1 = temp;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号