Java程序设计教程(第2版)课后习题及答案10课后习题.docx





《Java程序设计教程(第2版)课后习题及答案10课后习题.docx》由会员分享,可在线阅读,更多相关《Java程序设计教程(第2版)课后习题及答案10课后习题.docx(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第10章课后习题参考答案1.有三个雇员,其信息如下。姓名职务年龄工资王振经理405000刘莉莉出纳333200王红秘书252800对雇员信息进行如下操作。(1)创建一个Array Li st对象,将这三个雇员信息存储到这个ArrayList对象中。 (2)将ArrayList对象中的元素显示一遍。(3)在“刘莉莉”前插入一个新雇员,其信息为:张芳副经理38 4200插入后将ArrayList对象中的元素再显示出来。(4)从ArrayList对象中删除“刘莉莉”后再将其中的元素显示出来。 答:import java.util. ArrayList;import java.util.*; clas
2、s Employee String name;String occupation;int age;int salary;public Employee(String n,String o,int age,int s) (name=n;occupations; age二this, age; salary = s; )public String getName() (return name; )public void output() (System. out.println(name+”_”+occupation+“_+age+”_”+salary);public class exercise
3、10_01public static void main(String args)Scanner reader=new Scanner(System.in);String nm,occu;int age,salary;ArrayList employeeList=new ArrayList();Employee e;e=new Employee(“王振,“经理”,40,5000);employeeList.add(e);e=new Employee(文lj莉莉” J 出纳”,33,3200);employeeList.add(e);e二new Employee(“王红 J秘书”,25,2800
4、);employeeList.add(e);Iterator it = employeeList.iterator();while (it.hasNext() (e=(Employee)it.next();e.output();)System.out.println();for(int i=0; iemployeeList.size(); i+) (if(Employee)employeeList.get).getName().equals(“刘莉莉”) (e=new Employee(张芳J副经理”,38,4200);employeeList.add(i,e);break;)it = emp
5、loyeeList.iterator();while (it.hasNext() (e=(Employee)it.next();e.output();)System.out.println();for(int i=0; iemployeeList.size(); i+) (if(Employee)employeeList.get ).getName().equals(刘莉莉) (employeeList.remove(i);break;)it = empl oy eeList. i terator();while (it.hasNext()e=(Employee)it.next(); e.ou
6、tput();)2 .定义注解类用于描述“水果”的信息,水果的信息有:名称、颜色、经销商(包括经销商编 号和名称)、供货商(包括供货商编号、姓名和地址),用自定义的水果注解类标注水果,并能从 注解中提取水果信息。答:import java.Iang.reflect.*;import java.lang.annotation.*;Retention(RetentionPolicy.RUNTIME)元注解,运行时保留注解,必须有,否则注解值读不出 interface FruitDealer定义注解(定义注解元素,都有缺省值public String code() default n001n;pub
7、lic String name() default ”旺旺生鲜超市”; )Retention(RetentionPoIicy.RUNTIME)interface FruitSupplier/定义注解(public String name() default ”南国梨庄园”;public String code() default 002”;public String address() default鞍山;)Retention(RetentionPolicy.RUNTIME)interface FruitColor定义注解(public String value() default 绿色”;)
8、Retention(RetentionPolicy.RUNTIME) in terface FruitName/定义注解public String value() default ”苹果”;) class Fruit FruitName(“香蕉”)public String name;FruitColor(黄色)public String color;为域dealer加注解,给部分元素赋值,其余用缺省值如果注解元素都用缺省值,则直接写尸疝1口匕附FruitDealer(name=n 果果,code= “201 )public String dealer;FruitSupplier(name=莱
9、阳梨”,code=222,address=11l 东”)public String supplier;/域有注解public void setDealer(String m)(dealer=m;)public String getDealer()(return dealer;)public void setSupplier(String supplier)(this.supplier=supplier;)public String getSupplier()(return supplier;)public void setName(String name)(this.name二name;)pu
10、blic String getName()(return name;)public void setColor(String color)(this.color=color;public String getColor()return color;public class exercise 10_02(public static void main(String argsJ)(System.out.println(readAnnotation(Fruit.class);)读注解信息private static String readAnnotation(Class aClass) (Strin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 程序设计 教程 课后 习题 答案 10

限制150内