




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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) = 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 enum week mon overridepublic
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 创业城物业合同范本
- 纠纷收楼合同范本
- 合同范本写作
- 光纤外包安装合同范例
- 代理食品的合同范本
- 合同范本中英对照
- 买卖新房子合同范本
- 合同范本员工拒续签合同
- 合金采购合同范例
- it行业员工合同范本
- DB11∕512-2017 建筑装饰工程石材应用技术规程
- 职业技术学院《口腔颌面外科学》课程标准
- 员工二级安全教育培训试题及答案
- TSG ZF001-2006《安全阀安全技术监察规程》
- 2024年度中国AI大模型场景探索及产业应用调研报告-2024
- 2025年驾驶证资格考试科目一必刷题库及答案(共300题)
- 大学英语四级必背单词词汇资料表
- 保安培训课件(44张)
- DL∕T 796-2012 风力发电场安全规程
- 2024年泸西县惠民供水限公司公开招聘7人【重点基础提升】模拟试题(共500题)附带答案详解
- 《无损检测(第2版)》 课件绪论
评论
0/150
提交评论