《Java实验报告模板.docx》由会员分享,可在线阅读,更多相关《Java实验报告模板.docx(77页珍藏版)》请在课桌文档上搜索。
1、实验报告课程名称Java面对对象程序设计试验名称检查危急品吴超姓名学号201424134114专业年级14物联网益一、试验目的一木试验的目的是让学生驾驭try-catch语句二、试验内容:车站检杳危急品的设备,假如发觉危急品就会发出铸告。编写Exception的子类DangeException.编写Machine类处理异样主类main方法中的try-catch处理machine类的实例调用三、试验步骤试验源代码:/Check,javaimportjava.io.*:.*;importjava.util.*;publicclassClient(publicslaticvoidnain(Strin
2、gagrs)Scannerscanner=newScanner(System,in):Socketmysocket=nul1:ObjectInputStreaniinbject=nulI;ObjectOulpulStreamout0bject=nul1:Threadthread;ReadWindowreadWinclow=nul1:try(mysocket=newSockel();readWindow=newReadWindowO;thread=newThread(readWindow);System.out.PrintIn(输入服务器的IP);StringIP=scanner.next1.i
3、ne();System.OUt.println(输入端口号:*);intport=scanner.nextInlO;if(mysocket.isConnectedO)else(InetAddressaddress-InctAddress.getByName(IP);InetSocketAddressSoCkeIAddreSS=newInetSocketAddress(address,port);nysocket.connect(SOCkelAddreSS);InputStreamin=mysocket.getInputStreamO;OutputStreainout=mysocket.geIO
4、utputStream();inbject=newObjectInputStream(in);outObject=newObjectOuIpulStream(out);reaiWindow.SetObjectInputStream(inObject):thread,startO;)catch(Exceptione)(System.OUt.printin(服务罂己经断开+e);)classReadWindowimplementsRunnable(ObjectInputStreaniin;publicvoidSetObjectInputStream(ObjectInputStrcamin)this
5、.in=in;publicvoidrun()(doubleresult=0;while(true)(try(javax.swing.JFramewindow=(javax.swing.JFrame)in.readbjecl():window.SetTitlee这是从服务器上读入的窗口;window.setVisible(true);window.FequestFocusInWindow();window.setSize(600,800):)catch(Exceptione)(System,out.PrinlIn(服务器已经断开+e);break;)DangerException.java*异样
6、类维承EXCePlion*当你要抛出的自定义的异样类,必需继承EXCePtion,否则错误*CatCh捕获抛出的异样*/publicclassDangerExceptionextendsExceptionStringmessage;publicDangerException()message-危急品!”;)publicvoidtoShow()System,out.print(message+;)/Good,javapublicclassGoods(publicbooleanisDanger;Stringname:publicvoidsetIsDanger(boo1eanboo)isDanger
7、=boo;)publicbooleanisDanger()returnisDanger:)publicvoidsetName(Strings)name=s;)publicStringgetNameOreturnname;)/Machine,java* 异样的处理和抛出* throws,声明异样的实例* 格式为:throws异样类名* exp:throwsDangerExeption* 声明异样后应当在调用者里面对捕获的异样处理* throw,抛出异样* 格式为:throw异样实例* exp:thrownewDangerException* 假如捕获到申明的异样。干脆跳转相应的CalCh语句*
8、可以声明多个异样类,并用多由CatCh语句捕获* /publicclassMachinepublicvoidcheckBag(Goodsgood)throwsDangerExceplionif(good,isDanger=true)thrownewDangerException();elseSystem,out.print(good.gelName()+不是危急品!”);)二二二工一四、血结果CheckJavaApplicationC:Progi危险品!炸药被禁止西服不是危险品!西服检有通过危险品!统酸被禁止手表不是危险品!手表检查通过危险品!畸诺被禁止五、总结经过此次试验,熟识了try_ca
9、tch对以后学习java打下好的基础,对JaVa能更加熟识实验报告课程名JaVa面对对象程序设计称试验名比较日期称姓名吴超益学号201424134114专业年级M物联网一、试验目的,U的是让学生驾驭Data类以与Calendar类的常用方法二、试验内容:编写个java应用程存,用户从输入对话框输入两个日期,程序将推断两个日期的大小关系,以与两个日期之间的间隔天数三、试验步骤试验源代码:package比较日期;importjava.sql.Date:importjava.util.Calendar;importjava,til.Scanner;publicclassCompareDatepubl
10、icstaticvoidmain(Stringargs)/TODOAUlo-generaledmethodstubScannerscanner=newScanner(Systen).in);SySlein.oul.prinlln(输入第一个年,月,日数据”);System.out.PrintIn(输入年份”);shortyeane=scanner.nextShort();System.out.PrintIne输入月份”);byteInonthOne=scanner.nextByte();System.OUt.priniln(输入日期”);bytedayne=scanner,nex1.Byte(
11、);SyStem.out.println(输入其次年,月,日数据);System.OUI.prinlln(输入年份;shortyearTwo-scanner.nextShort();System.out.PrinCIn(输入月份);bytemonthTwo=scanner.nextByte();System,out.PrintIn(输入日期”);bytedayTwo=scanner.nextByte():CalendarCalendar=Calendar.getInslanceO;calendar,set(yearne,monthOne-1,dayne);longti11ere=calenda
12、r.getTi11eIrMi11is();calendar.set(yearTwo,monthTwo-1,dayTwo);longtimeTwo=calendar.getTimeInMi11is();DatedateI=newDate(timene);Datedate2=newDate(IimeTwo):if(iate2.equals(iatel)(SyStem.oul.prinlln(两个日期的年,月,日完全相同”);elseif(date2.after(date1)System.out.PrintIn(您输入的其次个口期大于第,个口期”):elseif(date2.before(datel
13、)System.OUaPrinIIn(您输入其次个日期小雨第一个日期”);)longday=Math,abs(timeTwo-time0ne)(1000*60*60*24);System,out.printIn(yearne+w年”+monlhOne+”月+dayne+日和”+yearTwo+年+monlhTwo+月+dayTx,o+日相隔+day+天”);四、试验结果输入第一个年,月,E鞭括福人年份2022输入月份愉入日期输入第二年,月,日数据临入年份2011输入月份猫入日期您愉入第二个日期小雨第一个日期2022年7月旧和2011年1月5日相隔4295天五、总结这次试验熟识了Java应用程序
14、,并将Data类以与Calendar类的常用方法驾驭,对以后学习java打下好的基础,对java能更加熟识.实疆报告课程名称JaVa面对对象程序设计试验名称华容道姓名吴超学号201424134114专业年级14物联网益一、试验目的:学习焦点、鼠标和键盘事务二、试殴内容:编写GUl程序,用户通过键盘和鼠标事务来实现普操、关羽等人物的移动。三、试殴步骤试验源代码:package华容道;importjava.awt.*:importjava.awt.event.*;publicclasshanrongdaopublicstaticvoidmain(Stringargs)(newHua_Rong_Ro
15、adO;)classPersonextendsButtonimplementsFocus1.istener(intnumber;Colorc=newColor(255,245,170);publicPerson(finalintnumber,finalStrings)super(三);SetBackground(c);this,nmber=n11ber:c=gelBackground();addFocus1.istener(this):publicvoidfocusGained(finalFocusEvente)SetBackground(Color,red);)publicvoidfocus
16、1.ost(finalFocusEvente)(SetBackground(c);)implementsclassliua_Rong_RoadextendsFrameMouse1.istener,Key1.istener,Action1.istenerIPersonperson=newPerson10;Buttonleft,right,above,below;Buttonrestart=newBUtton(重新起先”);publicllua_Rong_Road()init();SetBounds(100,100,320,360):setVisible(true);validate();addW
17、indow1.isIener(newWindowAdapterOpublicvoidwindowClosing(WindowEvente)(System,out.println(0);1):publicvoidinit()Set1.ayoul(null);add(restart):restart.SelBoUndS(100,320,120,25);restart.aidAction1.istener(this);Stringname口=曹操J关羽张飞刘备赵云,黄忠,兵兵,兵兵);for(intk=Ojkh2)(go(man,below);)if(yh2)(go(man,above);)if(x
18、w2)(go(man,right);)publicvoidInouseReleased(MouseEvenle)publicvoidIuouseEntered(MouseEvente)publicvoidInouseExited(MouseEvente)(publicvoidmousedickei(MouseEvente)publicvoidgo(Personman,Buttondirection)booleanmove=true:RectanglemanRect=man.getBounds();intx=man.getBounds().x:inty=man.getBoundsO.y;if(d
19、irection=be1OW)y=y+50:elseif(direction=above)y=y-50;elseif(direction=lefl)x=-50;elseif(direclion=right)x=x+50;11arRect.set1.ocation(x,y);Rectangle(IirectionRect=direction.getBounds();for(intk=0;kenChar)C=StartChar:)classInpulChineseThreadextendsThread(Scannerreader:Chinesehanzi:intscore=0;InpulChine
20、seThreadOreadernewScanner(Systen).in);)publicvoidSetChinese(Chinesehanzi)this,hanzi=hanzi;)publicvoidrunOwhile(true)Stringstr=reader.next1.ine();charc=str.charAt(0):if(c=hanzi.getChinese()score+;System,wprintf(tl输入对了,目前分数*dn”,score);)elseSystem.OUuPrinlf(ll输入错f.目前分数%dn”,score);System,exit(O);)四、试验结果
21、涌入汉字练习(幅入$结束程序)愉入显示的汉字(回车)显示的汉字:好.显示的汉字:她26显示的汉字:越显示的汉字:改显示的汉字:妁显示的汉字:如显示的汉字:妃显示的汉字:妄显示的汉字:虹显示的汉字:妆显示的汉字:妇显示的汉字:螭显示的汉字:枕兄示的W支:算五、总结这次试黯熟识了Java应用程序,并将Thread的子类的常用方法驾驭,对以后学习java打下好的基础,对java能更加熟识.实舱报告试验名称读取服务器端文件姓名吴超益学号201424134114专业年级14物联网一、试验目的I.学习运用UR1.课程名称Java面对对象程序设计二、试验内容:创建一个运用UR1.对象,然后让UR1.对象返回
22、输入流,通过该输入流读取UR1.所包含的资源文件三、试验步骤试验源代码:packageone;importjava.awt.Bordcr1.ayout;importjava.awt.event.ActionEvent:importjava.awt.event.Action1.istencr;importjava.io.IOException:importjava.io.InputStream:.MalformedUR1.Exception:.UR1.;importjavax.swing.JBulton:importjavax.swing.JFrame;importjavax.swing.J1.
23、abel:importjavax.swing.JPanel;importjavax.swing.JTextArea;importjavax.swing.JTextField;publicclassReadUR1.Source(publicstaticvoidmain(String11args)newNetWinO:)classNetWinextendsJFrameimplementsAction1.istener,RunnableJButtonbutton;UR1.url:JTextFieldinputUR1.Text;JTextAreaarea;byteb=newbytel18J;Threa
24、dthread;publicNetWinO/TODOuto-generatedconstructorstubinputUR1.Text=newJTextField(20);area=newJTextArea(12,12):button=newJButton(确定);button.addAction1.istener(this);thread=newThread(this);JPanelp=newJPanel();p.add(newJ1.abeM”请输入网址:”);p.add(inputUR1.Text);p.add(button);add(area,Border1.ayout.CENTER);
25、add(p,Border1.ayout.NORTH);SelBounds(60.60,560,300);setVisible(true);validale();SetDefaultcioseOperation(JFrame.EX1T_(XM_CIX)SE);)publicvoidactionPerfoimed(ActionEvente)if(thread.isAlive()thread=newThread(this):)try(thread,start();catch(Exceptionee)inputUR1.ext.setText(我正在读取+url);)publicvoidrun()try
26、(intn-1;area.setText(null);Stringname=inputliR1.Text.getText().trim();url=newUR1.(name):StringhostName-Ur1.gCtHOSt();intUrlPortNumber=url.getPortO:Stringfi1eName=url.getl7ile();InputStreamin=url.openStream();area,append(*n主机:+HostName+”端口:UrlPortNumber+”包含的文件名字:”+fiIeName);area,append(*n文件的内容如下:”);w
27、hile(n=in.read(b)!=-1)Strings=newString(b,O,n);area,append(三):catch(MalfOrmedUR1.Exceptionel)inputUR1.Text.setText+el);return;catch(IOExceptionel)inputUR1.Text.selText(*,+el);return:)四、试验结果彳图-ftX请希入网址:JavanetConnectException:CcInneCtiOnr定五、总结学习运用UR1.,以后学习java打下好的基础,对java能更加熟识实验报告课程名称Java面对对象程序设计试殴名称
28、读取服务器端的窗口姓名吴超益学号201424134114专业年级14物联网一、试验目的:.学会运用套接字读取服务器端的对象二、试验内容,客服端利用套接字连接将服务器端的JFram对象读取到客服端。首先将服务器端的程序编译通过,并运行起来,等待恳求套接字连接三、试验步骤试验源代码:/.Client,javapackageclient;i三portjava,io.*;.;importjava.util.;publicclassClientpublicstaticvoidmain(Stringagrs)(Scannerscanner=newScanner(System,in);Socketmysoc
29、ket=null:ObjectInputStreaminbjectull;ObjectOutputStreanout0bject=null;Threadthread;ReadVindovreadlindow=null;trynysockt=newSocket0;readVindov=newReadYindovO;thread=newThread(readWindov);System.out.Println(输入服务器的IP);StringIP=scanner.next1.ine();Systea.out.PrintIn(输入蟠口号:);intport=scanner.nextlnt();ifC
30、nysocket.isConnected0)()else(InetAddressaddress=InetAddress.getByName(IP);InetSocketAddresssocketAddess=nwInetSocketAddress(address,port);mysocket,connect(socketAddress);InputStreaninaysocket.getInputStream0;OutputStreamout=三ysocket.getOutputStrean0;inbject=newObjectInputStreaa(In);outObject=newObje
31、ctOutputStreain(out);readfindow.SetObjectInputStream(inObject);thread,start0;)catch(Exceptione)(SySteD1.out.PrintIn(服务器已经断开+e);)classReadVindowinplenentsRunnable(ObjectInputStreainin;publicvoidSetObjectInputStrean(ObjectInputStreamin)this.in=in;publicvoidnmdoubleresult=0;while(true)(tryjavax.swing.J
32、Franewindow=(javax.swing.JFraae)in.readbjct0;window.SetTitIe(“这是从服务器上读入的窗口,);window.setVisibl(true);window,requestFocusInWindowO;window.setSize(600,800);catch(Exceptione)(System.out.Println(服务器已羟断开+e);break;)/Server.javapackageclient;importjava.io.*:.*:importjava.util.*;importjava.awl.*:importjavax.
33、swing.*;publicclassServerSUPPreSSWarningS(resource)publicstaticvoidmain(String11args)SerxrerSocketserver=null:ServerThreadthread;Socketyou=null:while(true)(tryserver-newServerSocket(4331);catch(IOExceptionel)System,out.printin(正在监听);try,yu=server.accept();System.OUt.prinln(客户的地址:+you.getInetAddressO
34、):catch(10Exccptione)System,out.PrintIn(正在等待客户”);if(you!=null)(newServerThread(you).startO;)classServerThreadextendsThreadSocketsocket:Objec11nputStreamin=nul1;ObjectOu1.putStreamout=nul1;JFramewindow;JTexlAreatext;ServerThread(Sockett)(socket=t;try(out=newObJectOutpulStream(socket,geIOutputStream();in=newObjectInputStream(socket,getInputStream():)catch(IOExceptione)window=newJFrameO;text=newJTextAreaO;for(inti=l;i=20:i+)(text,append,你好,我是服务器上的文件区组件n):