Java实验报告三

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

天津商业大学学生实验报告共13页,第1页制表单位:设备处开课实验室:现代信息交流中心403开课时间:2015年**月**日实验报告:2015年04月17日学院名称信息工程学院年级、专业、班计科学号姓名同组姓名无课程名称Java程序设计实验项目名称Java面向对象程序设计(2)指导教师尉斌实验类型验证√综合□设计□创新□成绩教师评语教师签名:年月日一、实验目的:熟悉类、对象、方法、继承和多态的使用方法。二、实验内容:1.创建一个Point类,包含坐标x、y。然后创建一个Line类,定义两个读写属性start和end,数据类型为Point,定义Line类方法(计算start和end之间的距离,并打印start和end坐标以及距离信息)。2.根据下面类图,设计一个名为Student的类,包括属性“学号”、“姓名”以及3门课程“数学”、“英语”和“计算机”的成绩,包括的方法有计算3门课程的“总分”、“平均分”、“最高分”及“最低分”。在此基础上,使用数组来实现对多个学生的管理。编写一个控制台应用程序,实现如下菜单功能。天津商业大学学生实验报告共13页,第2页制表单位:设备处3.题目:品尝饮料要求:(1)使用键盘输入参数(饮料类型),输出该饮料类型的味道,如:当键盘输入参数为1时,结果见图1:(2)如果没有该种饮料,结果见图2:天津商业大学学生实验报告共13页,第3页制表单位:设备处实现步骤:(1)建立一个Java抽象类Drink,应当:a、声明一个抽象方法taste(),该方法负责输出饮料的味道;b、声明int型常量来代表不同的饮料类型(咖啡、啤酒、牛奶)c、声明静态工厂方法getDrink(intdrinkType),根据传入的参数创建不同的饮料对象,并返回该对象,建议使用switch语句。(2)建立Drink的具体子类:a、分别建立Drink的子类:Coffee(代表咖啡),Beer(代表啤酒),Milk(代表牛奶);b、实现taste()方法,要求在屏幕输出中打印各自的味道特征。(3)建立Test测试类,测试以上内容的正确性a、编写main方法,通过命令行传参的方式传入某种饮料的类型。b、在main方法中,调用Drink类的getDrink方法,获得相应的饮料对象。c、然后调用该饮料的taste()方法,输出该饮料的味道。三、源代码清单:1.源代码清单publicclassPoint{publicstaticvoidmain(String[]args){Pointpoint1=newPoint(1,2);Pointpoint2=newPoint(4,6);Lineline1=newLine(point1,point2);System.out.println(Thedistanceis+line1.line()+\nThestartis+(+line1.start.x+,+line1.start.y+)+\nTheendis+(+line1.end.x+,+line1.end.y+));}doublex;doubley;Point(){}天津商业大学学生实验报告共13页,第4页制表单位:设备处Point(doublex,doubley){this.x=x;this.y=y;}}classLine{Pointstart;Pointend;Line(){}Line(PointnewStart,PointnewEnd){start=newStart;end=newEnd;}publicdoubleline(){returnMath.sqrt((end.x-start.x)*(end.x-start.x)+(end.y-start.y)*(end.y-start.y));}}2.源代码清单publicclassStudent{Stringstuno;Stringname;floatmath;floatenglish;floatcomputer;Student(){}Student(Stringstuno,Stringname,floatmath,floatenglish,floatcomputer){this.stuno=stuno;this.name=name;this.math=math;this.english=english;this.computer=computer;}publicStringgetStuno(){returnstuno;}天津商业大学学生实验报告共13页,第5页制表单位:设备处publicvoidsetStuno(Strings){this.stuno=s;}publicStringgetName(){returnname;}publicvoidsetName(Stringn){this.name=n;}publicfloatgetMath(){returnmath;}publicvoidsetMath(floatm){this.math=m;}publicfloatgetEnglish(){returnenglish;}publicvoidsetEnglish(floate){this.english=e;}publicfloatgetComputer(){returncomputer;}publicvoidsetComputer(floatc){this.computer=c;}publicdoublesum(){returnmath+english+computer;}publicdoubleavg(){return(math+english+computer)/3;}publicdoublemax(doublemax){if(math=english){if(math=computer)max=math;elsemax=computer;}elseif(mathenglish){if(englishcomputer)math=computer;天津商业大学学生实验报告共13页,第6页制表单位:设备处elsemax=english;}returnmax;}publicdoublemin(doublemin){if(math=english){if(math=computer)min=math;elsemin=computer;}elseif(mathenglish){if(computerenglish)min=english;elsemin=computer;}returnmin;}}publicclassManagerextendsStudent{privateStudent[]student=newStudent[100];privatestaticintnumberOfStudent=0;intgetNumberOfStudent(){returnnumberOfStudent;}Student[]getStudent(){returnstudent;}booleanaddStudent(Studentstudent){try{this.student[numberOfStudent]=student;numberOfStudent++;returntrue;}catch(Exceptione){returnfalse;}天津商业大学学生实验报告共13页,第7页制表单位:设备处}booleanremoveStudent(Stringstuno){booleantarget=false;for(inti=0;inumberOfStudent;i++)if(student[i].getStuno().equals(stuno)){student[i]=null;target=true;}returntarget;}StudentfindStudent(Strings){for(inti=0;inumberOfStudent;i++)if(student[i]!=null)if(student[i].getStuno().equals(s)||student[i].getName().equals(s))returnstudent[i];returnnull;}booleanchangeStudent(Studentstudent){for(inti=0;inumberOfStudent;i++){if(this.student[i].getStuno().equals(student.getStuno())||this.student[i].getName().equals(student.getName())){this.student[i]=student;returntrue;}}returnfalse;}voidprint(){System.out.println(学号\t姓名\t数学\t英语\t计算机);for(inti=0;inumberOfStudent;i++)if(student[i]!=null){System.out.println(student[i].getStuno()+\t+student[i].getName()+\t+student[i].getMath()+\t+student[i].getEnglish()+\t+student[i].getComputer());}}天津商业大学学生实验报告共13页,第8页制表单位:设备处voidcaculate(){floatmax_math=0,min_math=student[0].getMath(),sum_math=0;for(inti=0;inumberOfStudent;i++){sum_math+=student[i].getMath();if(student[i].getMath()max_math)max_math=student[i].getMath();elseif(student[i].getMath()min_math)min_math=student[i].getMath();}floatavg_math=sum_math/numberOfStudent;floatmax_english=0,min_english=student[0].getEnglish(),sum_english=0;for(inti=0;inumberOfStudent;i++){sum_english+=student[i].getEnglish();if(student[i].getEnglish()max_english)max_english=student[i].getEnglish();elseif(student[i].getEnglish()min_english)min_english=student[i].getEnglish();}floatavg_english=sum_english/numberOfStudent;floatmax_computer=0,min_computer=student[0].getComputer(),sum_computer=0;for(inti=0;inumberOfStudent;i++){sum_computer+=student[i].getComputer();if(student[i].getComputer()max_computer)max_computer=student[i].getComputer();elseif(student[i].getComputer()min_computer)min_computer=student[i].getComputer();}floatavg_computer=sum_computer/numberOfStudent;System.out.println(学科\t平均成绩\t最高分\t最低分);System.out.println(数学\t+avg

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功