版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
《Java语言程序设计》答案第一套题一、简答题1什么是多态性?方法的重载和覆盖有何区别?阅读下列代码,指出其中存在的重载和覆盖,写出输出结果是什么?解释为什么这样输出?(15分)classC1{ publicvoidf(){ System.out.println("C1.f"); }}classC2extendsC1{ publicvoidf(){ System.out.println("C2.f"); }}classC3{ publicvoidg(C1one){ System.out.println("g(C1)"); one.f(); } publicvoidg(C2two){ System.out.println("g(C2)"); two.f(); }}publicclassMain1{ publicstaticvoidmain(String[]args){ C1one=newC2(); C3three=newC3(); three.g(one); }}答:在Java语言中,多态是指一个方法可以有多种实现版本,类的多态性表现为方法的多态性。方法重载,指的是在类中创建多个具有相同名称,但使用不同参数的方法,Java虚拟机根据传递给方法的参数个数的类型决定调用哪个重载方法。方法的覆盖指在子类中重新定义父类中已有的方法,对于重写的方法,运行时系统根据调用该方法的实例的类型来决定选择哪个方法调用。输出结果为g(C1)C2.f解释:变量three的类型是C3,变量one声明为C1类型。C3的g()方法有两个重载实现,当传入的参数为C1类型时,输出为g(C1)。又因为变量one指向的是C1的子类C2的对象,C2的f方法覆盖了C1的f方法,所以在C3.g()的方法中执行时,执行的是C2.f(),所以输出为C2.f。2、请用自己的语言介绍throw/throws有什么联系和区别?在程序中应如何使用?另外谈谈final、finally的区别和作用,必须举例说明用法。(15分)答:throw是在代码块内的,即在捕获方法内的异常并抛出时用的;throws是针对方法的,即将方法的异常信息抛出去。可以理解为throw是主动(在方法内容里我们是主动捕获并throw的),而throws是被动(在方法上是没有捕获异常进行处理,直接throws的)例子:publicvoidstr2int(Stringstr)throwsException{//这里将得到的异常向外抛出try{System.out.println(Integer.parseInt(str));}catch(NumberFormatExceptione){//TODO这里可以做一些处理,处理完成后将异常报出,让外层可以得到异常信息thrownewException("格式化异常");}}final是用来修饰名词的,即它是用来表达最终的某个东西的。比如,“最后的战役”,“最后的晚餐”,“最后的结局”之类的。finally是用来修饰名词以外的词的(经常是修饰动词用的),即它是用来表达最终的一个行为或动作的。比如“他最后还是来了”“你最后赢了没”“钱最终怎么算的”finalize是动词,它不修饰其他词,它就是表达了一种“了结,完成,完善,搞定”的一个行为。就比如“咱把这事了结了吧”“把题目做完”“把工作做完”三个词都跟“完结”有关,前两个只是表达某件事情已经完结时是什么样的。而第三个词,它的作用在于把一个没完结的东西变到已经完结的状态,是一种行为。3、编写一个描述学生基本情况的类,属性包括姓名,学号,语文成绩,数学成绩和英语成绩,方法包括信息输出,设置姓名和学号,设置三门课程成绩,计算总成绩和平均成绩。在main方法中对方法进行测试(15分)4、Java中实现多线程有几种方式?这几种方式有什么区别?然后采取其中一种方式设计一个线程例子,在例子中构造4个线程对象实现对同一数据类对象进行操作(数据初始值为0),其中线程对象1对数据执行+10的操作,线程对象2对数据执行+20的操作,对象3对数据执行乘以3的操作,线程对象4对数据执行除以4的操作,,要求考虑线程同步,保证数据操作的正确性。要求提供程序代码以及运行结果截图(15分)有三种方式:通过实现Runnable接口;通过继承Thread类;以及通过Callable和Future创建线程。采用实现Runnable、Callable接口的方式创建多线程时,线程类只是实现了Runnable接口或Callable接口,还可以继承其他类。使用继承Thread类的方式创建多线程时,编写简单,如果需要访问当前线程,则无需使用Thread.currentThread()方法,直接使用this即可获得当前线程。二、编程题1、编写一个图形用户界面程序,包含两个按钮,一个信息标签(label)和一个显示面板,两个按钮分别为“掷色子”和“移动”,在显示面板中显示一个小人(用小圆以及线绘制),随机设定小人的初始位置,当点击“掷色子”按钮,随机产生移动信息(上移,下移,左移,右移,移动几步),并显示在信息标签中,点击移动,按照产生的移动信息,让小人进行移动。要求提供完整程序代码以及运行结果截图(20分)答:/**Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.*Tochangethistemplatefile,chooseTools|Templates*andopenthetemplateintheeditor.*/packagetest;importjava.awt.Color;importjava.awt.EventQueue;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.util.Random;importjavax.swing.JFrame;/****@authorldl*/publicclassTestLableextendsjavax.swing.JFrame{privatejavax.swing.JButtonjButton1;privatejavax.swing.JButtonjButton2;privatejavax.swing.JLabeljLabel1; publicTestLable(){initComponents();}@SuppressWarnings("unchecked")privatevoidinitComponents(){jLabel1=newjavax.swing.JLabel();jButton1=newjavax.swing.JButton();jButton2=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jLabel1.setText("开始点击掷");jButton1.setText("掷色子");jButton1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton1ActionPerformed(evt);}});jButton2.setText("移动");jButton2.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton2ActionPerformed(evt);}});javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(34,34,34).addComponent(jLabel1)).addGroup(layout.createSequentialGroup().addGap(24,24,24).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false).addComponent(jButton1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(jButton2,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)))).addContainerGap(306,Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(18,18,18).addComponent(jLabel1).addGap(26,26,26).addComponent(jButton1).addGap(18,18,18).addComponent(jButton2).addContainerGap(177,Short.MAX_VALUE)));pack();}intrandomNum=0;intrandomPosition=0;String[]strs={"上移","下移","左移","右移"};privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt){/Randomrandom=newRandom();randomNum=random.nextInt(4)+1;randomPosition=random.nextInt(4);jLabel1.setText(strs[randomPosition]+randomNum+"步");}privatevoidjButton2ActionPerformed(java.awt.event.ActionEventevt){repaint();}publicstaticvoidmain(Stringargs[]){try{for(javax.swing.UIManager.LookAndFeelInfoinfo:javax.swing.UIManager.getInstalledLookAndFeels()){if("Nimbus".equals(info.getName())){javax.swing.UIManager.setLookAndFeel(info.getClassName());break;}}}catch(ClassNotFoundExceptionex){java.util.logging.Logger.getLogger(TestLable.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(InstantiationExceptionex){java.util.logging.Logger.getLogger(TestLable.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(IllegalAccessExceptionex){java.util.logging.Logger.getLogger(TestLable.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(javax.swing.UnsupportedLookAndFeelExceptionex){java.util.logging.Logger.getLogger(TestLable.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}/*Createanddisplaytheform*/java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newTestLable().setVisible(true);}});}@Overridepublicvoidpaint(Graphicsg){super.paint(g);//String[]strs={"上移","下移","左移","右移"};switch(randomPosition){case0:y=y-randomNum*10;y1=y1-randomNum*10;y2=y2-randomNum*10;y11=y11-randomNum*10;y22=y22-randomNum*10;break;case1:y=y+randomNum*10;y1=y1+randomNum*10;y2=y2+randomNum*10;y11=y11+randomNum*10;y22=y22+randomNum*10;break;case2:x=x-randomNum*10;x1=x1-randomNum*10;x11=x11-randomNum*10;x2=x2-randomNum*10;x22=x22-randomNum*10;break;case3:x=x+randomNum*10;x1=x1+randomNum*10;x11=x11+randomNum*10;x2=x2+randomNum*10;x22=x22+randomNum*10;break;default:break;}g.drawOval(x,y,20,20);g.drawLine(x1,y1,x11,y11);g.drawLine(x2,y2,x22,y22);}intx=200;inty=150;intx1=200;inty1=165;intx11=199;inty11=180;intx2=220;inty2=165;intx22=220;inty22=182;}2、编写一个班级推优(优秀学生干部)投票管理程序。列出参与推优的学生名单(6名),可以勾选进行投票,要求每个参选学生前面有图标表示候选人的职务,每人可以投3票,每次投票后能够显示当前投票人数以及每名候选者得票数,图形化柱状图显示得票数,可以保存投票结果到文本文件。要求提供完整程序代码以及运行结果截图(20分)答:/**Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.*Tochangethistemplatefile,chooseTools|Templates*andopenthetemplateintheeditor.*/packagetest;importjava.awt.BorderLayout;importjava.awt.Font;importjava.util.List;importjavax.swing.JCheckBox;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartPanel;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.axis.CategoryAxis;importorg.jfree.chart.axis.NumberAxis;importorg.jfree.chart.axis.ValueAxis;importorg.jfree.chart.plot.CategoryPlot;importorg.jfree.chart.plot.PlotOrientation;importorg.jfree.data.category.CategoryDataset;importorg.jfree.data.category.DefaultCategoryDataset;/****@authorldl*/publicclassTestClassextendsjavax.swing.JFrame{privateDefaultCategoryDatasetdataset;publicTestClass(){initComponents();CategoryDatasetdataset=getDataSet();JFreeChartchart=ChartFactory.createBarChart("统计",//图表标题"投票统计",//目录轴的显示标签"数量",//数值轴的显示标签dataset,//数据集PlotOrientation.VERTICAL,//图表方向:水平、垂直true,false,false);CategoryPlotplot=chart.getCategoryPlot();//获取图表区域对象NumberAxisnumberaxis=(NumberAxis)plot.getRangeAxis();numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());CategoryAxisdomainAxis=plot.getDomainAxis();//水平底部列表domainAxis.setLabelFont(newFont("黑体",Font.BOLD,14));//水平底部标题domainAxis.setTickLabelFont(newFont("宋体",Font.BOLD,12));//垂直标题ValueAxisrangeAxis=plot.getRangeAxis();//获取柱状rangeAxis.setLabelFont(newFont("黑体",Font.BOLD,15));chart.getLegend().setItemFont(newFont("黑体",Font.BOLD,15));chart.getTitle().setFont(newFont("宋体",Font.BOLD,20));//设置标题字体ChartPanelchartPanel=newChartPanel(chart,true);jPanel1.setLayout(newBorderLayout(0,0));jPanel1.add(chartPanel);jPanel1.repaint();}/***Thismethodiscalledfromwithintheconstructortoinitializetheform.*WARNING:DoNOTmodifythiscode.Thecontentofthismethodisalways*regeneratedbytheFormEditor.*/@SuppressWarnings("unchecked")//<editor-folddefaultstate="collapsed"desc="GeneratedCode">privatevoidinitComponents(){jCheckBox1=newjavax.swing.JCheckBox();jPanel1=newjavax.swing.JPanel();jCheckBox5=newjavax.swing.JCheckBox();jCheckBox2=newjavax.swing.JCheckBox();jCheckBox3=newjavax.swing.JCheckBox();jCheckBox6=newjavax.swing.JCheckBox();jCheckBox4=newjavax.swing.JCheckBox();jButton1=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jCheckBox1.setText("班长张三");jCheckBox1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jCheckBox1ActionPerformed(evt);}});javax.swing.GroupLayoutjPanel1Layout=newjavax.swing.GroupLayout(jPanel1);jPanel1.setLayout(jPanel1Layout);jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,411,Short.MAX_VALUE));jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,0,Short.MAX_VALUE));jCheckBox5.setText("学习张三4");jCheckBox5.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jCheckBox5ActionPerformed(evt);}});jCheckBox2.setText("卫生张三1");jCheckBox2.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jCheckBox2ActionPerformed(evt);}});jCheckBox3.setText("学习张三2");jCheckBox3.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jCheckBox3ActionPerformed(evt);}});jCheckBox6.setText("学习张三5");jCheckBox6.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jCheckBox6ActionPerformed(evt);}});jCheckBox4.setText("学习张三3");jCheckBox4.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jCheckBox4ActionPerformed(evt);}});jButton1.setText("投票");javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jCheckBox1).addComponent(jCheckBox2).addComponent(jCheckBox3).addComponent(jCheckBox4).addComponent(jCheckBox5).addComponent(jCheckBox6).addComponent(jButton1)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(14,14,14).addComponent(jCheckBox1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jCheckBox2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jCheckBox3).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jCheckBox4).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jCheckBox5).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jCheckBox6).addGap(18,18,18).addComponent(jButton1).addContainerGap(83,Short.MAX_VALUE)).addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE));pack();}//</editor-fold>privatevoidjCheckBox1ActionPerformed(java.awt.event.ActionEventevt){JCheckBoxjCheckBox=(JCheckBox)evt.getSource();intintValue=dataset.getValue(0,0).intValue();if(jCheckBox.isSelected()){intValue++;}else{intValue--;}dataset.setValue(intValue,"张三1","张三1");}privatevoidjCheckBox5ActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:JCheckBoxjCheckBox=(JCheckBox)evt.getSource();intintValue=dataset.getValue(4,4).intValue();if(jCheckBox.isSelected()){intValue++;}else{intValue--;}dataset.setValue(intValue,"张三4","张三4");}privatevoidjCheckBox2ActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:JCheckBoxjCheckBox=(JCheckBox)evt.getSource();intintValue=dataset.getValue(1,1).intValue();if(jCheckBox.isSelected()){intValue++;}else{intValue--;}dataset.setValue(intValue,"张三2","张三2");}privatevoidjCheckBox3ActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:JCheckBoxjCheckBox=(JCheckBox)evt.getSource();intintValue=dataset.getValue(2,2).intValue();if(jCheckBox.isSelected()){intValue++;}else{intValue--;}dataset.setValue(intValue,"张三3","张三3");}privatevoidjCheckBox6ActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:JCheckBoxjCheckBox=(JCheckBox)evt.getSource();intintValue=dataset.getValue(5,5).intValue();if(jCheckBox.isSelected()){intValue++;}else{intValue--;}dataset.setValue(intValue,"张三6","张三6");}privatevoidjCheckBox4ActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:JCheckBoxjCheckBox=(JCheckBox)evt.getSource();intintValue=dataset.getValue(3,3).intValue();if(jCheckBox.isSelected()){intValue++;}else{intValue--;}dataset.setValue(intValue,"张三4","张三4");}privateCategoryDatasetgetDataSet(){dataset=newDefaultCategoryDataset();dataset.addValue(0,"张三1","张三1");dataset.addValue(0,"张三2","张三2");dataset.addValue(0,"张三3","张三3");dataset.addValue(0,"张三4","张三4");dataset.addValue(0,"张三5","张三5");dataset.addValue(0,"张三6","张三6");returndatas
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 常州市溧阳中学高三地理一轮复习8海洋地理作业
- 第2讲 分层作业
- 2025年中职道路桥梁(桥梁施工)试题及答案
- 2025年高职机械电子工程技术(机电控制技术)试题及答案
- 2026年中职第三学年(市场营销策划)促销方案阶段测试题及答案
- 中职第二学年(制冷和空调设备运行与维护)空调安装调试2026年综合测试题及答案
- 2025-2026年八年级语文(综合巩固)上学期试题及答案
- 2025年大学电力系统继电保护与自动化(继电保护应用)试题及答案
- 2025年大学水利水电工程管理(水利工程管理)试题及答案
- 2026年中职第二学年(国际贸易)国际结算综合测试题及答案
- 医学影像云存储:容灾备份与数据恢复方案
- 2025年卫生系统招聘(临床专业知识)考试题库(含答案)
- 基建工程索赔管理人员索赔管理经典文献
- 工业机器人专业大学生职业生涯规划书
- 农贸市场消防安全管理制度
- 良品铺子营运能力分析及对策研究
- 手术室中的团队协作与沟通
- 五人制足球技术智慧树知到课后章节答案2023年下电子科技大学
- 涉密人员汇总表
- 其他方便食品(冲调谷物制品)
- S7-200SMARTPLC应用技术PPT完整全套教学课件
评论
0/150
提交评论