Java实验报告模板.docx
《Java实验报告模板.docx》由会员分享,可在线阅读,更多相关《Java实验报告模板.docx(126页珍藏版)》请在冰点文库上搜索。
Java实验报告模板
实验报告
课程名称Java面向对象程序设计
实验名称检查危险品
姓名吴超益学号201424134114专业年级14物联网
一、实验目的:
本实验的目的是让学生掌握try-catch语句
二、实验内容:
车站检查危险品的设施,假如发现危险品就会发出警示。
编写Exception的子类DangeException,编写Machine类办理异样主类main方法中的try-catch办理machine类的实例调用
三、实验步骤
实验源代码:
importjava.io.*;
import.*;
importjava.util.*;
publicclassClient{
publicstaticvoidmain(Stringagrs[])
{
Scannerscanner=newScanner(System.in);
Socketmysocket=null;
ObjectInputStreaminObject=null;
ObjectOutputStreamoutObject=null;
Threadthread;
ReadWindowreadWindow=null;
try{
mysocket=newSocket();
readWindow=newReadWindow();
thread=newThread(readWindow);
System.out.println("输入服务器的IP");
StringIP=scanner.nextLine();
System.out.println("输入端口号:
");
intport=scanner.nextInt();
if(mysocket.isConnected())
{
}
else
{
InetAddressaddress=InetAddress.getByName(IP);
InetSocketAddresssocketAddress=newInetSocketAddress(address,port);
mysocket.connect(socketAddress);
InputStreamin=mysocket.getInputStream();
OutputStreamout=mysocket.getOutputStream();
inObject=newObjectInputStream(in);
outObject=newObjectOutputStream(out);
readWindow.setObjectInputStream(inObject);
thread.start();
}
}
catch(Exceptione)
{
System.out.println("服务器已经断开"+e);
}
}
}
classReadWindowimplementsRunnable
{
ObjectInputStreamin;
publicvoidsetObjectInputStream(ObjectInputStreamin)
{
this.in=in;
}
publicvoidrun()
{
doubleresult=0;
while(true)
{
try{
javax.swing.JFramewindow=(javax.swing.JFrame)in.readObject();
window.setTitle("这是从服务器上读入的窗口");
window.setVisible(true);
window.requestFocusInWindow();
window.setSize(600,800);
}
catch(Exceptione)
{
System.out.println("服务器已经断开"+e);
break;
}
}
}
}
//
/*
*异样类继承Exception
*当你要抛出的自定义的异样类,一定继承Exception,不然错误
*catch捕获抛出的异样
*/
publicclassDangerExceptionextendsException{
Stringmessage;
publicDangerException(){
message=危"险品!
";
}
publicvoidtoShow(){
System.out.print(message+"");
}
}
publicclassGoods{
publicbooleanisDanger;
Stringname;
publicvoidsetIsDanger(booleanboo){
isDanger=boo;
}
publicbooleanisDanger(){
returnisDanger;
}
publicvoidsetName(Strings){
name=s;
}
publicStringgetName(){
returnname;
}
}
/*
*异样的办理和抛出
*throws,申明异样的实例
*格式为:
throws异样类名
*exp:
throwsDangerExeption
*申明异样后应当在调用者里面对捕获的异样办理
*throw,抛出异样
*格式为:
throw异样实例
*exp:
thrownewDangerException
*假如捕获到申明的异样。
直接跳转相应的catch语句
*能够申明多个异样类,并用多重catch语句捕获
*/
publicclassMachine{
publicvoidcheckBag(Goodsgood)throwsDangerException{if(good.isDanger==true){
thrownewDangerException();
}else{
System.out.print(good.getName()+"不是危险品!
");
}
}
}
四、实验结果
五、总结
经过此次实验,熟习了try_catch语句,对此后学习java打下好的基础,对java能更为熟习
实验报告
课程名称
Java面向对象程序设计
实验名称
比较日期
姓名
吴超益
学号
201424134114
专业年级
14物联网
一、实验目的:
目的是让学生掌握
Data类以及
Calendar类的常用方法
二、实验内容:
编写一个java应用程序,用户从输入对话框输入两个日期,程序将判断两个日期的大小关系,以及两个日期之间的间隔天数
三、实验步骤
实验源代码:
package比较日期;
importjava.sql.Date;
importjava.util.Calendar;
importjava.util.Scanner;
publicclassCompareDate{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstubScannerscanner=newScanner(System.in);System.out.println("输入第一个年,月,日数据");System.out.println("输入年份");
shortyearOne=scanner.nextShort();
System.out.println("输入月份");
bytemonthOne=scanner.nextByte();
System.out.println("输入日期");
bytedayOne=scanner.nextByte();
System.out.println("输入第二年,月,日数据");
System.out.println("输入年份");
shortyearTwo=scanner.nextShort();
System.out.println("输入月份");
bytemonthTwo=scanner.nextByte();
System.out.println("输入日期");
bytedayTwo=scanner.nextByte();
Calendarcalendar=Calendar.getInstance();
calendar.set(yearOne,monthOne-1,dayOne);
longtimeOne=calendar.getTimeInMillis();
calendar.set(yearTwo,monthTwo-1,dayTwo);
longtimeTwo=calendar.getTimeInMillis();
Datedate1=newDate(timeOne);
Datedate2=newDate(timeTwo);
if(date2.equals(date1)){
System.out.println("两个日期的年,月,日完整同样");
}
elseif(date2.after(date1)){
System.out.println("您输入的第二个日期大于第一个日期");
}elseif(date2.before(date1)){
System.out.println("您输入第二个日期毛毛雨第一个日期");
}
longday=Math.abs(timeTwo-timeOne)/(1000*60*60*24);System.out.println(yearOne+"年"+monthOne+"月"+dayOne+"日和"+
yearTwo+"年"+monthTwo+"月"+dayTwo+"日相隔
"+day+"天");
}
}
四、实验结果
五、总结
此次实验熟习了Java应用程序,并将Data类以及Calendar类的常用方法掌握,
对此后学习java打下好的基础,对java能更为熟习。
实验报告
课程名称Java面向对象程序设计
实验名称华容道
姓名吴超益学号201424134114专业年级14物联网
一、实验目的:
学习焦点、鼠标和键盘事件
二、实验内容:
编写GUI程序,用户经过键盘和鼠标事件来实现曹操、关羽等人物的挪动。
三、实验步骤
实验源代码:
package华容道;
importjava.awt.*;
importjava.awt.event.*;
publicclasshuanrongdao{
publicstaticvoidmain(Stringargs[])
{
newHua_Rong_Road();
}
}
classPersonextendsButtonimplementsFocusListener
{
intnumber;
Colorc=newColor(255,245,170);
publicPerson(finalintnumber,finalStrings)
{
super(s);
setBackground(c);
this.number=number;
c=getBackground();
addFocusListener(this);
}
publicvoidfocusGained(finalFocusEvente)
{
setBackground(Color.red);
}
publicvoidfocusLost(finalFocusEvente)
{
setBackground(c);
}
}
classHua_Rong_RoadextendsFrameimplementsMouseListener,KeyListener,ActionListener
{
Personperson[]=newPerson[10];
Buttonleft,right,above,below;
Buttonrestart=newButton("从头开始");
publicHua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{
System.out.println(0);
}});
}
publicvoidinit()
{
setLayout(null);
add(restart);
restart.setBounds(100,320,120,25);
restart.addActionListener(this);
Stringname[]={"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};for(intk=0;k{
person[k]=newPerson(k,name[k]);
person[k].addMouseListener(this);
person[k].addKeyListener(this);
add(person[k]);
}
person[0].setBounds(104,54,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
person[3].setBounds(204,154,50,100);
person[4].setBounds(54,54,50,100);
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
person[9].requestFocus();
left=newButton();right=newButton();
above=newButton();below=newButton();
add(left);add(right);add(above);add(below);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();
}
publicvoidkeyTyped(KeyEvente){}
publicvoidKeyReleased(KeyEvente){}
publicvoidKeyPressed(KeyEvente)
{
Personman=(Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
go(man,below);
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
go(man,above);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
go(man,left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
go(man,right);
}
}
publicvoidmousePressed(MouseEvente)
{
Personman=(Person)e.getSource();
intx=-1,y=-1;
x=e.getX();
y=e.getY();
intw=man.getBounds().width;
inth=man.getBounds().height;
if(y>h/2)
{
go(man,below);
}
if(y{
go(man,above);
}
if(x{
go(man,left);
}
if(x>w/2)
{
go(man,right);
}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){}
publicvoidgo(Personman,Buttondirection)
{
booleanmove=true;
RectanglemanRect=man.getBounds();
intx=man.getBounds().x;
inty=man.getBounds().y;
if(direction==below)
y=y+50;
elseif(direction==above)
y=y-50;
elseif(direction==left)
x=x-50;
elseif(direction==right)
x=x+50;
manRect.setLocation(x,y);
RectangledirectionRect=direction.getBounds();
for(intk=0;k<10;k++)
{
RectanglepersonRect=person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!
=k))
{
move=false;
}
}
if(manRect.intersects(directionRect))
{
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
publicvoidactionPerformed(ActionEvente)
{
dispose();
newHua_Rong_Road();
}
publicvoidkeyPressed(KeyEventarg0){
}
publicvoidkeyReleased(KeyEventarg0){
}
}
四、实验结果
五、总结
此次实验让我认识了焦点、鼠标和键盘事件,此后学习java打下好的基础,对java能更为熟习
实验报告
课程名称
Java面向对象程序设计
实验名称
汉字输入练习
姓名
吴超益
学号
201424134114
专业年级
14物联网
一、实验目的:
掌握Thread的子类创立线程
二、实验内容:
编写一个java应用程序,在主线程序中再创立两个线程,第一个负责给出某个汉字,第二个线程负责让用户在命令行输入第一线程给出的汉字
三、实验步骤
实验源代码:
import
java.util.Scanner;
public
class
TepyChinese{
public
static
void
main(String
args[]){
System.
out
.println(
"输入汉字练习(输入
#结束程序)");
System.
out
.println(
"输入显示的汉字(回车)");
Chinese
hanzi;
hanzi
=newChinese();
GiveChineseThread
giveHanzi
;
InputChineseThread
typeHanzi
;
giveHanzi
=newGiveChineseThread();
giveHanzi
.setChinese(
hanzi
);
giveHanzi
.setSleepLength(6000);
typeHanzi
=newInputChineseThread();
typeHanzi
.setChinese(
hanzi
);
giveHanzi
.start();
try
{
Thread.
sleep
(200);
}
catch(Exceptionexp){
typeHanzi.start();
}
}
}
class
Chinese{
charc='\0'
publicvoid
;
setChinese(
char
c){
this.c=c;
}
publicchargetChinese(){
returnc;
}
}
class
GiveChineseThreadextends
Chinesehanzi;
charstartChar=(char)22909,
intsleepLength=5000;
publicvoidsetChinese(Chinese
this.hanzi=hanzi;
Thread{
endChar=(char)(startChar
hanzi){
+100);