Java实验报告模板.doc
实 验 报 告
课程名称Java面向对象程序设计实验名称检查危险品姓名吴超益学号24134114专业年级14物联网一、实验目的:
本实验的目的是让学生掌握try-catch语句二、实验内容:
车站检查危险品的设备,如果发现危险品就会发出警告。
编写Exception的子类DangeException,编写Machine类处理异常
主类main方法中的try-catch处理machine类的实例调用三、实验步骤
实验源代码:
//Check.java
import java.io.*;
import .*;
import java.util.*;
public class Client {
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);
System.out.println("输入服务器的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);
InputStream 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 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.requestFocusInWindow();
window.setSize(600, 800);
}
catch (Exception e)
{
System.out.println("服务器已经断开"+e);
break;
}
}
}
}
// DangerException.java
/*
* 异常类继承Exce