天津商业大学java实验报告_第1页
天津商业大学java实验报告_第2页
天津商业大学java实验报告_第3页
天津商业大学java实验报告_第4页
天津商业大学java实验报告_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

天津商业大学学生实验报告共13页,第12页制表单位:设备处开课实验室:现代信息交流中心40*开课时间:2012年**月**日实验报告:年月日学院名称信息工程学院年级、专业、班学号姓名同组姓名无课程名称Java程序设计实验项目名称三、面向对象程序开发指导教师尉斌实验类型验证√综合□设计□创新□成绩教师评语教师签名:年月日实验目的:熟悉类、对象、方法、继承和多态的使用方法。实验内容:创建一个Point类,包含坐标x、y。然后创建一个Line类,定义两个读写属性start和end,数据类型为Point,定义Line类方法(计算start和end之间的距离,并打印start和end坐标以及距离信息)。根据下面类图,设计一个名为Student的类,包括属性“学号”、“姓名”以及3门课程“数学”、“英语”和“计算机”的成绩,包括的方法有计算3门课程的“总分”、“平均分”、“最高分”及“最低分”。在此基础上,使用数组来实现对多个学生的管理。编写一个控制台应用程序,实现如下菜单功能。题目:品尝饮料要求:(1)使用键盘输入参数(饮料类型),输出该饮料类型的味道,如:当键盘输入参数为1时,结果见图1:(2)如果没有该种饮料,结果见图2:实现步骤:建立一个Java抽象类Drink,应当:声明一个抽象方法taste(),该方法负责输出饮料的味道;声明int型常量来代表不同的饮料类型(咖啡、啤酒、牛奶)声明静态工厂方法getDrink(intdrinkType),根据传入的参数创建不同的饮料对象,并返回该对象,建议使用switch语句。建立Drink的具体子类:分别建立Drink的子类:Coffee(代表咖啡),Beer(代表啤酒),Milk(代表牛奶);实现taste()方法,要求在屏幕输出中打印各自的味道特征。建立Test测试类,测试以上内容的正确性编写main方法,通过命令行传参的方式传入某种饮料的类型。在main方法中,调用Drink类的getDrink方法,获得相应的饮料对象。然后调用该饮料的taste()方法,输出该饮料的味道。源代码清单:1.源代码清单packageex1;publicclassPoint{floatx;floaty;}packageex1;publicclassLine{Pointstart,end;publicPointgetStart(){ returnstart;}publicvoidsetStart(Pointstart){ this.start=start;}publicPointgetEnd(){returnend; }publicvoidsetEnd(Pointend){ this.end=end;}publicdoublegetDistance(){ doubledistance; distance=Math.sqrt((start.x-end.x)*(start.x-end.x)+(start.y-end.y)*(start.y-end.y)); returndistance;}publicvoidprintInformation(){ System.out.println("start.x="+start.x+",start.y="+start.y); System.out.println("end.x="+end.x+",end.y="+end.y); System.out.println("Thedistancebetweenstartandendis"+getDistance()); }publicstaticvoidmain(String[]args){ LineL=newLine(); Pointstart=newPoint(); start.x=1; start.y=1; L.setStart(start); Pointend=newPoint(); end.x=10; end.y=10; L.setEnd(end); L.printInformation(); }}2.源代码清单packageEx2;publicclassstudentOperation{ StudentstuArray[]; inti,a; publicstudentOperation(){ stuArray=newStudent[a]; } publicStudent[]getstuArry(){ returnstuArray; } publicintgetstuNum(){ returna; } publicbooleanaddStudent(Studentstu){ if(a<30){ stuArray[a]=stu; returntrue; } else returnfalse; } publicbooleandelStudent(Stringstuno){ for(inti=0;i<a;i++){ if(stuArray[i].stuno.equals(stuno)) for(intj=i;j<=a;j++) stuArray[j]=stuArray[j+1]; stuArray[29]=null; returntrue; } returnfalse; } publicStudentqueryStuno(Stringstuno){ if(stuArray[i].stuno.equals(stuno)) returnstuArray[i]; else returnnull; } publicStudentqueryName(Stringname){ if(stuArray[i].name.equals(name)) returnstuArray[i]; else returnnull; } publicbooleanupdStu(Studentstu){ for(inti=0;i<=a;i++){ if(stuArray[i].getStuno().equals(stu.stuno)) stuArray[i]=stu; returntrue; } returnfalse; } publicvoidprint(){ System.out.println("学号"+"/t"+"姓名"+"/t"+"数学"+"/t"+"英语"+"/t"+"计算机"); for(inti=0;i<=stuArray.length;i++){ System.out.println("stuArray[i].getstuno"+"/t"+"stuArray[i].getstuname"+"/t"+"stuArray[i].getmath"+"/t"+"stuArray[i].getenglish"+"/t"+"stuArray[i].getcomputer"); } } publicvoidStatistics(){ System.out.println("学科"+"/t"+"平均成绩"+"/t"+"最高分"+"/t"+"最低分"); floatsum=0; for(inti=0;i<=stuArray.length;i++){ sum+=stuArray[i].getMath(); sum=sum/stuArray.length; } floatmax=stuArray[1].getMath(); for(inti=0;i<=stuArray.length;i++){ if(stuArray[i].getMath()>max) max=stuArray[i].getMath(); } floatmin=stuArray[1].getMath(); for(inti=0;i<=stuArray.length;i++){ if(stuArray[i].getMath()>min) min=stuArray[i].getMath(); } }}packageEx2;importjava.io.*;publicclassstudentManagement{ publicintprintMenu(){ System.out.println("请选择(1-6)"); System.out.println("1、添加学生"); System.out.println("2、删除学生"); System.out.println("3、查询学生"); System.out.println("4、修改学生"); System.out.println("5、打印学生"); System.out.println("6、统计学生"); BufferedReaderbuf; Stringstr=null; intnum=0; buf=newBufferedReader(newInputStreamReader(System.in)); System.out.println("请输入:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("您输入:"+num); } num=Integer.parseInt(str); returnnum; } publicStudentaddStudent(){ Studentstu=newStudent(); BufferedReaderbuf; Stringstr=null; floatf=0; buf=newBufferedReader(newInputStreamReader(System.in)); System.out.println("请输入学号:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("stuno="+str); } System.out.println("请输入姓名:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("name="+str); } System.out.println("请输入数学:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("math="+f); } Float.parseFloat(str); System.out.println("请输入英语:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("english="+f); } Float.parseFloat(str); System.out.println("请输入计算机:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("computer="+f); } Float.parseFloat(str); returnstu; } publicStringquery(){ BufferedReaderbuf; Stringstr=null; buf=newBufferedReader(newInputStreamReader(System.in)); System.out.println("请输入学号:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("您输入:"+str); } returnstr; } publicStringsquery(){ BufferedReaderbuf; Stringstr=null; buf=newBufferedReader(newInputStreamReader(System.in)); System.out.println("请输入学号或姓名:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("您输入:"+str); } returnstr; } publicStringupdateStudent(){ BufferedReaderbuf; Stringstr=null; buf=newBufferedReader(newInputStreamReader(System.in)); System.out.println("请输入学号:"); try{ str=buf.readLine(); } catch(Exceptione){ System.out.println("您输入:"+str); } returnstr; } publicstaticvoidmain(Stringarg[]){ studentManagementsm=newstudentManagement(); studentOperationso=newstudentOperation(); intmenu=sm.printMenu(); while(true){ if(menu==1){ Studentstu=sm.addStudent(); so.addStudent(stu); } if(menu==2){ Stringstuno=sm.query(); so.delStudent(stuno); } if(menu==3){ Stringstuno=sm.squery(); so.delStudent(stuno); so.delStudent(stuno); } if(menu==4){ Stringstuno=sm.updateStudent(); Studentstu=null; so.updStu(stu); } if(menu==5){ so.print(); } if(menu==6){ so.Statistics(); } if(menu==7){ break; } } }} 3.源代码清单packagesy3;publicclassBEERextendsDrink{ publicvoidtaste(){ System.out.println("BEER,微苦的"); }}packagesy3;publicclassCOFFEEextendsDrink{ publicvoidtaste(){ System.out.println("COFFEE,苦的"); }}packagesy3;publicabstractclassDrink{ finalstaticintCOFFEE=1; finalstaticintBEER=2; finalstaticintMILK=3; publicabstractvoidtaste();publicstaticDrinkgetdrink(intdrinktype){ switch(drinktype){ caseCOFFEE: returnnewCOFFEE(); caseBEER: returnnewBEER(); caseMILK: retu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论