版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实 验 报 告 课程名称Java面向对象程序设计实验名称检查危险品姓名吴超益学号201424134114专业年级14物联网一、实验目的: 本实验的目的是让学生掌握try-catch语句二、实验内容:车站检查危险品的设备,如果发现危险品就会发出警告。编写Exception的子类DangeException,编写Machine类处理异常主类main方法中的try-catch处理machine类的实例调用三、实验步骤实验源代码:/Check.javaimport java.io.*;import .*;import java.util.*;public class Client public sta
2、tic 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);System.out.printl
3、n("输入服务器的IP");String IP=scanner.nextLine();System.out.println("输入端口号:");int port =scanner.nextInt();if(mysocket.isConnected()else InetAddress address =InetAddress.getByName(IP);InetSocketAddress socketAddress=new InetSocketAddress(address,port);mysocket.connect(socketAddress);Inp
4、utStream 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 implem
5、ents Runnable ObjectInputStream in; public void setObjectInputStream( ObjectInputStream in) this .in=in; public void run() double result =0; while (true) try javax.swing.JFrame window=(javax.swing.JFrame)in.readObject(); window.setTitle("这是从服务器上读入的窗口"); window.setVisible(true); window.requ
6、estFocusInWindow(); window.setSize(600, 800); catch (Exception e) System.out.println("服务器已经断开"+e); break; / DangerException.java/* * 异常类继承Exception * 当你要抛出的自定义的异常类,必须继承Exception,否则错误 * catch捕捉抛出的异常 */public class DangerException extends Exception String message;public DangerException() mes
7、sage = "危险品!"public void toShow() System.out.print(message + " ");/Good.javapublic class Goods public 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 ge
8、tName() return name;/ Machine.java/* * 异常的处理和抛出 * throws,声明异常的实例 * 格式为:throws 异常类名 * exp:throws DangerExeption * 声明异常后应该在调用者里面对 捕获的异常处理 * throw,抛出异常 * 格式为:throw 异常实例 * exp:throw new DangerException * 如果捕获到申明的异常。直接跳转相应的catch语句 * 可以声明多个异常类,并用多重catch语句捕捉 */public class Machine public void checkBag(Good
9、s good) throws DangerException if (good.isDanger = true) throw new DangerException(); else System.out.print(good.getName() + "不是危险品!");四、实验结果五、总结经过此次实验,熟悉了try_catch语句,对以后学习java打下好的基础,对java能更加熟悉实 验 报 告 课程名称Java面向对象程序设计实验名称 比较日期姓名吴超益学号201424134114专业年级14物联网一、实验目的: 目的是让学生掌握Data类以及Calendar类的常用方
10、法二、实验内容:编写一个java应用程序,用户从输入对话框输入两个日期,程序将判断两个日期的大小关系,以及两个日期之间的间隔天数三、实验步骤实验源代码:package 比较日期;import java.sql.Date;import java.util.Calendar;import java.util.Scanner;public class CompareDate public static void main(String args) / TODO Auto-generated method stubScanner scanner = new Scanner(System.in);Sys
11、tem.out.println("输入第一个年,月,日数据");System.out.println("输入年份");short yearOne = scanner.nextShort();System.out.println("输入月份");byte monthOne = scanner.nextByte();System.out.println("输入日期");byte dayOne = scanner.nextByte();System.out.println("输入第二年,月,日数据")
12、;System.out.println("输入年份");short yearTwo = scanner.nextShort();System.out.println("输入月份");byte monthTwo = scanner.nextByte();System.out.println("输入日期");byte dayTwo = scanner.nextByte();Calendar calendar=Calendar.getInstance();calendar.set(yearOne, monthOne-1, dayOne);l
13、ong 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("两个日期的年,月,日完全相同"); else if (date2.after(date1) System.out.
14、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+"月"
15、;+dayTwo+"日相隔"+day+"天");四、实验结果五、总结这次实验熟悉了Java应用程序,并将Data类以及Calendar类的常用方法掌握,对以后学习java打下好的基础,对java能更加熟悉。实 验 报 告 课程名称Java面向对象程序设计实验名称 华容道姓名吴超益学号201424134114专业年级14物联网一、实验目的: 学习焦点、鼠标和键盘事件二、实验内容: 编写GUI程序,用户通过键盘和鼠标事件来实现曹操、关羽等人物的移动。三、实验步骤实验源代码:package 华容道;import java.awt.*;import java.a
16、wt.event.*;public class huanrongdao public static void main(String args) new Hua_Rong_Road(); class Person extends Button implements FocusListener int number; Color c=new Color(255,245,170); public Person(final int number,final String s) super(s); setBackground(c); this.number=number; c=getBackgroun
17、d(); addFocusListener(this); public void focusGained(final FocusEvent e) setBackground(Color.red); public void focusLost(final FocusEvent e) setBackground(c); class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener Person person=new Person10; Button left,right,above,bel
18、ow; Button restart=new Button("重新开始"); public Hua_Rong_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
19、.setBounds(100,320,120,25); restart.addActionListener(this); String name="曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵" for(int k=0;k<name.length;k+) personk=new Person(k,namek); personk.addMouseLis
20、tener(this); personk.addKeyListener(this); add(personk); person0.setBounds(104,54,100,100); person1.setBounds(104,154,100,50); person2.setBounds(54,154,50,100); person3.setBounds(204,154,50,100); person4.setBounds(54,54,50,100); person5.setBounds(204,54,50,100); person6.setBounds(54,254,50,50); pers
21、on7.setBounds(204,254,50,50); person8.setBounds(104,204,50,50); person9.setBounds(154,204,50,50); person9.requestFocus(); left=new Button();right=new Button(); above=new Button();below=new Button(); add(left);add(right);add(above);add(below); left.setBounds(49,49,5,260); right.setBounds(254,49,5,260
22、); above.setBounds(49,49,210,5); below.setBounds(49,304,210,5); validate(); public void keyTyped(KeyEvent e) public void KeyReleased(KeyEvent e) public void KeyPressed(KeyEvent e) Person man=(Person)e.getSource(); if(e.getKeyCode()=KeyEvent.VK_DOWN) go(man,below); if(e.getKeyCode()=KeyEvent.VK_UP) g
23、o(man,above); if(e.getKeyCode()=KeyEvent.VK_LEFT) go(man,left); if(e.getKeyCode()=KeyEvent.VK_RIGHT) go(man,right); public void mousePressed(MouseEvent e) Person man=(Person)e.getSource(); int x=-1,y=-1; x=e.getX(); y=e.getY(); int w=man.getBounds().width; int h=man.getBounds().height; if(y>h/2)
24、go(man,below); if(y<h/2) go(man,above); if(x<w/2) go(man,left); if(x>w/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,Button direction) bo
25、olean 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.getBounds();
26、 for(int k=0;k<10;k+) Rectangle personRect=personk.getBounds(); if(manRersects(personRect)&&(man.number!=k) move=false; if(manRersects(directionRect) move=false; if(move=true) man.setLocation(x,y); public void actionPerformed(ActionEvent e) dispose(); new Hua_Rong_Road(); pu
27、blic void keyPressed(KeyEvent arg0) public void keyReleased(KeyEvent arg0) 四、实验结果五、总结这次实验让我认识了焦点、鼠标和键盘事件,以后学习java打下好的基础,对java能更加熟悉实 验 报 告 课程名称Java面向对象程序设计实验名称 汉字输入练习姓名吴超益学号201424134114专业年级14物联网一、实验目的: 掌握Thread的子类创建线程二、实验内容:编写一个java应用程序,在主线程序中再创建两个线程,第一个负责给出某个汉字,第二个线程负责让用户在命令行输入第一线程给出的汉字 三、实验步骤实验源代码:
28、import java.util.Scanner;public class TepyChinese public static void main(String args)System.out.println("输入汉字练习(输入#结束程序)");System.out.println("输入显示的汉字(回车)");Chinese hanzi;hanzi=new Chinese();GiveChineseThread giveHanzi;InputChineseThread typeHanzi;giveHanzi=new GiveChineseThread
29、();giveHanzi.setChinese(hanzi);giveHanzi.setSleepLength(6000);typeHanzi=new InputChineseThread();typeHanzi.setChinese(hanzi);giveHanzi.start();tryThread.sleep(200);catch(Exception exp)typeHanzi.start();class Chinesechar c='0'public void setChinese(char c)this.c=c;public char getChinese()retu
30、rn c;class GiveChineseThread extends ThreadChinese hanzi;char startChar=(char)22909,endChar=(char)(startChar+100);int sleepLength=5000;public void setChinese(Chinese hanzi)this.hanzi=hanzi;public void setSleepLength(int n)sleepLength=n;public void run()char c=startChar;while(true)hanzi.setChinese(c)
31、;System.out.printf("显示的汉字:%cn",hanzi.getChinese();tryThread.sleep(sleepLength);catch(InterruptedException e)c=(char)(c+1);if(c>endChar)c=startChar;class InputChineseThread extends ThreadScanner reader;Chinese hanzi;int score=0;InputChineseThread()reader=new Scanner(System.in);public voi
32、d setChinese(Chinese hanzi)this.hanzi=hanzi;public void run()while (true)String str=reader.nextLine();char c=str.charAt(0);if(c=hanzi.getChinese()score+;System.out.printf("tt输入对了,目前分数%dn",score);elseSystem.out.printf("tt输入错了,目前分数%dn",score);if(c='#')System.exit(0);四、实验结果五
33、、总结这次实验熟悉了Java应用程序,并将Thread的子类的常用方法掌握,对以后学习java打下好的基础,对java能更加熟悉。实 验 报 告 课程名称Java面向对象程序设计实验名称 读取服务器端文件姓名吴超益学号201424134114专业年级14物联网一、实验目的: 学习使用URL二、实验内容: 创建一个使用URL对象,然后让URL对象返回输入流,通过该输入流读取URL所包含的资源文件三、实验步骤实验源代码:package one;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt
34、.event.ActionListener;import java.io.IOException;import java.io.InputStream;import .MalformedURLException;import .URL;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;public class ReadU
35、RLSource public static void main(String args) new NetWin();class NetWin extends JFrame implements ActionListener, Runnable JButton button;URL url;JTextField inputURLText;JTextArea area;byte b = new byte118;Thread thread;public NetWin() / TODO Auto-generated constructor stubinputURLText = new JTextFi
36、eld(20);area = new JTextArea(12, 12);button = new JButton("确定");button.addActionListener(this);thread = new Thread(this);JPanel p = new JPanel();p.add(new JLabel("请输入网址:");p.add(inputURLText);p.add(button);add(area, BorderLayout.CENTER);add(p, BorderLayout.NORTH);setBounds(60, 60
37、, 560, 300);setVisible(true);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e) if (!(thread.isAlive() thread = new Thread(this);try thread.start(); catch (Exception ee) inputURLText.setText("我正在读取" + url);public void run() try int n = -1;a
38、rea.setText(null);String name = inputURLText.getText().trim();url = new URL(name);String hostName = url.getHost();int urlPortNumber = url.getPort();String fileName = url.getFile();InputStream in = url.openStream();area.append("n主机:" + hostName + "端口:" + urlPortNumber + "包含的文
39、件名字:" + fileName);area.append("n文件的内容如下:");while (n = in.read(b) != -1) String s = new String(b, 0, n);area.append(s); catch (MalformedURLException e1) inputURLText.setText("" + e1);return; catch (IOException e1) inputURLText.setText("" + e1);return;四、实验结果五、总结学习使用U
40、RL,以后学习java打下好的基础,对java能更加熟悉实 验 报 告 课程名称Java面向对象程序设计实验名称 读取服务器端的窗口姓名吴超益学号201424134114专业年级14物联网一、实验目的: 学会使用套接字读取服务器端的对象二、实验内容: 客服端利用套接字连接将服务器端的JFram对象读取到客服端。首先将服务器端的程序编译通过,并运行起来,等待请求套接字连接三、实验步骤实验源代码:/ Client.javapackage client;import java.io.*;import .*;import java.util.*;public class Client public s
41、tatic 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);System.out.prin
42、tln("输入服务器的IP");String IP=scanner.nextLine();System.out.println("输入端口号:");int port =scanner.nextInt();if(mysocket.isConnected()else InetAddress address =InetAddress.getByName(IP);InetSocketAddress socketAddress=new InetSocketAddress(address,port);mysocket.connect(socketAddress);I
43、nputStream 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 impl
44、ements Runnable ObjectInputStream in; public void setObjectInputStream( ObjectInputStream in) this .in=in; public void run() double result =0; while (true) try javax.swing.JFrame window=(javax.swing.JFrame)in.readObject(); window.setTitle("这是从服务器上读入的窗口"); window.setVisible(true); window.re
45、questFocusInWindow(); window.setSize(600, 800); catch (Exception e) System.out.println("服务器已经断开"+e); break; / Server.javapackage client;import java.io.*;import .*;import java.util.*;import java.awt.*;import javax.swing.*;public class Server SuppressWarnings("resource")public stat
46、ic void main(String args) ServerSocket server =null;ServerThread thread;Socket you= null;while (true)tryserver = new ServerSocket(4331);catch (IOException e1)System.out.println("正在监听");tryyou=server.accept();System.out.println("客户的地址:"+you.getInetAddress();catch(IOException e)Sys
47、tem.out.println("正在等待客户");if(you!=null)new ServerThread(you).start();class ServerThread extends ThreadSocket socket;ObjectInputStream in=null;ObjectOutputStream out =null;JFrame window;JTextArea text;ServerThread(Socket t)socket =t;tryout=new ObjectOutputStream(socket.getOutputStream();in
48、=new ObjectInputStream(socket.getInputStream();catch (IOException e) window =new JFrame(); text =new JTextArea(); for(int i=1;i<=20;i+) text.append("你好,我是服务器上的文件区组件n"); text.setBackground(Color.yellow); window.add(text); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void
49、run()try out.writeObject(window);catch (IOException e)System.out.println("客户离开");四、实验结果五、总结学会使用套接字读取服务器端的对象,为以后学习java打下好的基础,对java能更加熟悉实 验 报 告 课程名称Java面向对象程序设计实验名称 酒店管理系统项目实训姓名吴超益学号201424134114专业年级14物联网一、实验目的: 综合所学的知识,设计出简单的酒店管理系统二、实验内容: 1)业务需求功能1.登录模块:实现登录功能的数据处理2.用户管理模块:使用用户账号登录,实现房间查询和预定的功能,能够下订单和查询订单3.管理员管理模块:实现房间信息和用户信息的增、删、改、查2)数据库关系模块1.商品
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行业性欠薪问题的特点与对策
- 企业社会责任与可持续发展的关系研究
- 高层建筑深基坑支护施工方案
- 社交网络公司:提高员工工作积极性的措施
- 企业管理中的决策分析与模型构建
- 2024-2025学年高中历史 专题三 北魏孝文帝改革 一 励精图治的孝文帝改革(3)教学教学设计 人民版选修1
- 21.5 鸟类的生殖与发育 教学设计-苏教版生物八年级下册
- 2026江苏无锡职业技术大学招聘3人备考题库附完整答案详解(全优)
- 2026中共温岭市委机构编制委员会办公室招聘编外人员1人备考题库附答案详解【完整版】
- 2026湖南娄底市人力资源和社会保障局娄底市就业见习岗位备考题库附完整答案详解(历年真题)
- 2026年江苏经贸职业技术学院单招综合素质考试题库附答案详解
- 2026河北衡水恒通热力有限责任公司公开招聘工作人员28名笔试备考试题及答案解析
- 《工程勘察设计收费标准》(2002年修订本)-完整版-1
- 高等教育学(第十章:高等教育改革与发展的现状与趋势)
- 各类仪器仪表校验记录表18篇
- 电子元器件选型规范
- 多彩贵州,魅力贵州
- 厦门医学院辅导员考试真题2022
- 有限公司450m3高炉项目初步设计安全专篇
- 热学李椿 电子
- 教学能力比赛决赛 《英语》教案
评论
0/150
提交评论