1、Java实验报告模板实验报告课程名称 Java 面向对象程序设计实验名称 检查危险品姓名 吴超益 学号 201424134114 专业年级 14 物联网一、实验目的:本实验的目的是让学生掌握 try-catch 语句二、实验内容:车站检查危险品的设施,假如发现危险品就会发出警示。编写 Exception 的子类 DangeException,编写 Machine 类办理异样主类 main 方法中的 try-catch 办理 machine 类的实例调用三、实验步骤实验源代码:import java.io.*;import .*;import java.util.*;public class C
2、lient public static void main(String agrs)Scanner scanner= new Scanner (System.in);Socket mysocket=null;ObjectInputStream inObject=null;ObjectOutputStream outObject=null;Thread thread;ReadWindow readWindow =null;try mysocket =new Socket();readWindow =new ReadWindow();thread =new Thread(readWindow);S
3、ystem.out.println(输入服务器的 IP);String IP=scanner.nextLine();System.out.println(输入端口号: );int port =scanner.nextInt();if(mysocket.isConnected()elseInetAddress address =InetAddress.getByName(IP);InetSocketAddress socketAddress=new InetSocketAddress(address,port);mysocket.connect(socketAddress);InputStrea
4、m in=mysocket.getInputStream();OutputStream out=mysocket.getOutputStream();inObject=new ObjectInputStream(in);outObject=new ObjectOutputStream(out);readWindow.setObjectInputStream(inObject);thread.start();catch(Exception e)System.out.println(服务器已经断开 +e);class ReadWindow implements RunnableObjectInpu
5、tStream in;public void setObjectInputStream( ObjectInputStream in)this .in=in;public void run()double result =0;while (true)tryjavax.swing.JFrame window=(javax.swing.JFrame)in.readObject();window.setTitle(这是从服务器上读入的窗口 );window.setVisible(true);window.requestFocusInWindow();window.setSize(600, 800);c
6、atch (Exception e)System.out.println(服务器已经断开 +e);break;/*异样类继承 Exception*当你要抛出的自定义的异样类,一定继承 Exception,不然错误*catch 捕获抛出的异样*/public class DangerException extends Exception String message;public DangerException() message = 危险品! ;public void toShow() System.out.print(message + );public class Goods public
7、 boolean isDanger;String name;public void setIsDanger(boolean boo) isDanger = boo;public boolean isDanger() return isDanger;public void setName(String s) name = s;public String getName() return name;/*异样的办理和抛出*throws,申明异样的实例*格式为: throws 异样类名*exp:throws DangerExeption*申明异样后应当在调用者里面对 捕获的异样办理*throw ,抛出
8、异样*格式为: throw 异样实例*exp:throw new DangerException*假如捕获到申明的异样。直接跳转相应的 catch 语句*能够申明多个异样类,并用多重 catch 语句捕获*/public class Machine public void checkBag(Goods good) throws DangerException if (good.isDanger = true) throw new DangerException(); else System.out.print(good.getName() + 不是危险品! );四、实验结果五、总结经过此次实验
9、,熟习了 try_catch 语句,对此后学习 java 打下好的基础,对 java 能更为熟习实验报告课程名称Java面向对象程序设计实验名称比较日期姓名吴超益学号201424134114专业年级14 物联网一、实验目的:目的是让学生掌握Data 类以及Calendar 类的常用方法二、实验内容:编写一个 java 应用程序,用户从输入对话框输入两个日期, 程序将判断两个日期的大小关系,以及两个日期之间的间隔天数三、实验步骤实验源代码:package 比较日期 ;import java.sql.Date;import java.util.Calendar;import java.util.S
10、canner;public class CompareDate public static void main(String args) / TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); System.out.println(输入第一个年,月,日数据 ); System.out.println(输入年份 );short yearOne = scanner.nextShort();System.out.println(输入月份 );byte monthOne = scanner.nextByte
11、();System.out.println(输入日期 );byte dayOne = scanner.nextByte();System.out.println(输入第二年,月,日数据 );System.out.println(输入年份 );short yearTwo = scanner.nextShort();System.out.println(输入月份 );byte monthTwo = scanner.nextByte();System.out.println(输入日期 );byte dayTwo = scanner.nextByte();Calendar calendar=Calen
12、dar.getInstance();calendar.set(yearOne , monthOne-1, dayOne);long timeOne = calendar.getTimeInMillis();calendar.set(yearTwo , monthTwo-1,dayTwo);long timeTwo = calendar.getTimeInMillis();Date date1 = new Date(timeOne);Date date2 = new Date(timeTwo);if (date2.equals(date1) System.out.println(两个日期的年,月
13、,日完整同样 );else if (date2.after(date1) System.out.println(您输入的第二个日期大于第一个日期 );else if (date2.before(date1) System.out.println(您输入第二个日期毛毛雨第一个日期 );long day = Math.abs(timeTwo-timeOne)/(1000*60*60*24); System.out.println(yearOne+年 +monthOne+月+dayOne+日和 +yearTwo+ 年 +monthTwo+ 月 +dayTwo+ 日相 隔+day+ 天 );四、实验结
14、果五、总结此次实验熟习了 Java 应用程序, 并将 Data 类以及 Calendar 类的常用方法掌握,对此后学习 java 打下好的基础,对 java 能更为熟习 。实验报告课程名称 Java 面向对象程序设计实验名称 华容道姓名 吴超益 学号 201424134114 专业年级 14 物联网一、实验目的:学习焦点、鼠标和键盘事件二、实验内容:编写 GUI 程序,用户经过键盘和鼠标事件来实现曹操、关羽等人物的挪动。三、实验步骤实验源代码:package 华容道 ;import java.awt.*;import java.awt.event.*;public class huanrong
15、dao public static void main(String args)new Hua_Rong_Road();class Person extends Button implements FocusListenerint number;Color c=new Color(255,245,170);public Person(final int number,final String s)super(s);setBackground(c);this.number=number;c=getBackground();addFocusListener(this);public void fo
16、cusGained(final FocusEvent e)setBackground(Color.red);public void focusLost(final FocusEvent e)setBackground(c);class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListenerPerson person=new Person10;Button left,right,above,below;Button restart=new Button( 从头开始 );public Hua_R
17、ong_Road()init();setBounds(100,100,320,360);setVisible(true);validate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.out.println(0););public void init()setLayout(null);add(restart);restart.setBounds(100,320,120,25);restart.addActionListener(this);String name= 曹
18、操 , 关羽 , 张飞 , 刘备 , 赵云 , 黄忠 , 兵 , 兵 , 兵 , 兵 ; for(int k=0;kh/2)go(man,below);if(yh/2)go(man,above);if(xw/2)go(man,right);public void mouseReleased(MouseEvent e)public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)public void go(Person man,Bu
19、tton direction)boolean move=true;Rectangle manRect=man.getBounds();int x=man.getBounds().x;int y=man.getBounds().y;if(direction=below)y=y+50;else if(direction=above)y=y-50;else if(direction=left)x=x-50;else if(direction=right)x=x+50;manRect.setLocation(x,y);Rectangle directionRect=direction.getBound
20、s();for(int k=0;k10;k+)Rectangle personRect=personk.getBounds();if(manRect.intersects(personRect)&(man.number!=k)move=false;if(manRect.intersects(directionRect)move=false;if(move=true)man.setLocation(x,y);public void actionPerformed(ActionEvent e)dispose();new Hua_Rong_Road();public void keyPressed(
21、KeyEvent arg0) public void keyReleased(KeyEvent arg0) 四、实验结果五、总结此次实验让我认识了焦点、 鼠标和键盘事件, 此后学习 java 打下好的基础,对 java 能更为熟习实验报告课程名称Java面向对象程序设计实验名称汉字输入练习姓名吴超益学号201424134114专业年级14 物联网一、实验目的:掌握 Thread 的子类创立线程二、实验内容:编写一个 java 应用程序,在主线程序中再创立两个线程, 第一个负责给出某个汉字,第二个线程负责让用户在命令行输入第一线程给出的汉字三、实验步骤实验源代码:importjava.util.
22、Scanner;publicclassTepyChinese publicstaticvoidmain(Stringargs )System.out.println( 输入汉字练习(输入#结束程序) );System.out.println( 输入显示的汉字(回车) );Chinesehanzi ;hanzi= new Chinese();GiveChineseThreadgiveHanzi;InputChineseThreadtypeHanzi;giveHanzi=new GiveChineseThread();giveHanzi.setChinese(hanzi);giveHanzi.se
23、tSleepLength(6000);typeHanzi=new InputChineseThread();typeHanzi.setChinese(hanzi);giveHanzi.start();tryThread.sleep(200);catch (Exception exp )typeHanzi .start();classChinesechar c =0public void;setChinese(charc )this . c = c ;public char getChinese()return c ;classGiveChineseThread extendsChinese hanzi ;char startChar =( char )22909,int sleepLength =5000;public void setChinese(Chinesethis . hanzi = hanzi ;ThreadendChar =( char )( startCharhanzi )+100);