




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java程序设计实验报告学 号:姓 名:班 级:指导老师:陈业斌实验一、面向对象编程实验一、 实验目的 掌握接口的编写及使用理解继承、多态掌握包的编写以及如何使用包中的类二、预习内容 java的基本语法知识三、实验设备与环境 装有java语言工具软件 (jcreator )的微机若干四、实验内容 接口的编写(1) 编辑interfaceclass.java,设保存在d:myjava目录下。interface interfaceclass int i=4; int k=5; void func1(); int func2(int x);(2) 编辑useinterface.java,设保存在d:
2、myjava目录下。class useinterface implements interfaceclass int j;public void func1() /在使用接口的类中一定要实现接口中的所有抽象方法 system.out.println(func1=+1); public int func2(int i) system.out.println(func2=+1);return i; public static void main(string srgs ) /interfaceclass.class x=new interfaceclass.class();不能对接口进行实例化us
3、einterface x=new useinterface();x.func1();x.func2(k);多态在工资系统中的应用下面给出一个根据雇员类型利用abstract方法和多态性完成工资单计算的程序。 employee是抽象类,employee的子类有boss(每星期发给他固定工资,而不计工作时间)、 commissionworker(除基本工资外还根据销售额发放浮动工资)、pieceworker(按其生产的产品数发放工资)、hourlyworker(根据工作时间长短发放工资)。该例的employee的每个子类都声明为final,因为不需要再继承它们生成子类。对所有雇员类型都使用earn
4、ings()方法,但每个人挣的工资按他所属的雇员类计算,所有雇员类都是从超类earnings()派出生的。所有在超类中声明earnings()为抽象方法,并且对于每个子类都提供恰当的earnings()的实现方法。为了计算雇员的工资,程序仅仅使用雇员对象的一个超类引导 并调用earnings()方法。在一个实际的工资系统中,各种employee对象的引用可以通过一个employee引用数组来实现。程序依次使用数组的每个元素(employee引用)调用每个对象的employee()方法。(1)编辑test.java,设保存在d:myjava目录下。 abstract class employee
5、 private string firstname; private string lastname; public employee(string first,string last) firstname=first; lastname=last; public string getemployeename() return firstname;public string getlastname() return lastname;public string tostring() return firstname+lastname;public abstract string earning
6、s();/定义boss类,为employee的子类final class boss extends employee private double weeklysalary; public boss(string frist,string last,double s) super(frist,last);setweeklysalary(s); public void setweeklysalary(double s) weeklysalary=(s0?s:0); public string earnings() return boss+super.tostring();/定义commissio
7、mworker类,为employee的子类final class commissionworker extends employee private double salary; private double commission; private int quantity; public commissionworker(string first,string last,double s,double c,int q) super(first,last); setsalary(s); setcommission(c); setqusntily(q); /*public string earn
8、ings() */public void setqusntily(double s) salary=(s0?s:0);public double setcommission(double c) return commission=(c0?c:0); public string earnings() double i=salary+commission+quantity; return double.valueof(i).tostring(); public string tostring() returncommissionworker+super.tostring(); public voi
9、d setsalary(double s) salary=s; /定义pieceworker类,为employee的子类 final class pieceworker extends employee private double wagepiece;private int quantity;public pieceworker(string first,string last,double w,int q) super(first,last); setwage(w); setquantity(q); public void setwage(double w) wagepiece=(w0?w
10、:0); public double setquantity(int q) return q+wagepiece; public string tostring() return piecewoeker+super.tostring(); public string earnings() returndouble.valueof(wagepiece+quantity).tostring(); /定义hourlyworker类,为employee的子类 class hourlyworker extends employee private double wage; private double
11、hours; public hourlyworker(string first,string last ,double w,double h) super(first,last); setwage(w); sethours(h); public void setwage (double w) wage=(w0?w:0); public void sethours(double h) hours=(h=0&h168?h:0); public string earnings() return hourlyworker+super.tostring(); class text public stat
12、ic void main(string args ) /使用超类声明ref employee ref; string out=;/分别定义各子类 boss b=new boss(hohn,smith,800.00); commissionworker c=new commissionworker(sue,hones,400.00,3.0,150); pieceworker p=new pieceworker(bob,lewis,2.5,200); hourlyworker h=new hourlyworker(karen,price,13.75,40);/使用子类分别实例化ref=b;out+
13、=ref.tostring()+earned $+ref.earnings()+n+b.tostring()+earned $+b.earnings()+n;system.out.print(out);ref=c;out+=ref.tostring()+earned $+ref.earnings()+n+c.tostring()+earned $+c.earnings()+n;system.out.print(out);ref=p;out+=ref.tostring()+earned $+ref.earnings()+n+p.tostring()+earned $+p.earnings()+n
14、;system.out.print(out);ref=h;out+=ref.tostring()+earned $+ref.earnings()+n+h.tostring()+earned $+h.earnings()+n;system.out.print(out); 包的建立与使用(1) 编辑calculate.java,设保存在d:myjava目录下。package mypackage;public class calculate private int a; public calculate(int a) this.a=a;system.out.println(from constrar
15、tion+this.a); public double volume(double height,double width,double depth) return height*width*depth; int add(int x, int y) return x+y; protected void a() system .out.println(from constration+a); 编译,查看d:myjava目录下是否生成了myoackage文件夹,在该文件夹中是否有calculate.class文件。 (2) 编辑interfaceclass.java,设保存在d:myjava目录下
16、。 import mypackage.calculate;class packagedemo public static void main(string ags ) calculate c=new calculate(10); double s=c.volume(5,6,7); system.out.println(s); /int b=c.add(5,6); /c.a(); 五、实验结果实验二、异常处理实验一、实验目的 异常的生产及捕获自定义异常及其使用二、预习内容 面向对象的基本知识三、实验设备与环境 装有java语言工具软件 (jcreator )的微机若干四、实验内容 异常的捕获计算
17、两数相除并输出结果。使用两个catch子句,分别捕捉除数为0的异常和参数输入有误异常。编辑ex1.java,保存在d:myjava目录下。import java.io.*;class ex1public static void main(string args ) try bufferedreader strin=new bufferedreader( new inputstreamreader(system.in); system.out.print(请输入除数:); string cl=strin.readline(); int a=integer .parseint(cl); syste
18、m .out .print(请输入被除数:); cl=strin .readline(); int b=integer .parseint(cl); int c=b/a; system .out .println(商为:+c); /捕获与i/o有关的异常 catch(ioexception e)e .printstacktrace () ; /捕获数值转化时的异常,如不能将字符转化成数值 catch(numberformatexception e) system.out.println(请输入整数!); /e .printstacktrace(); /捕获除数为0的异常 catch(arith
19、meticexception e) system .out .println(除数不可以0!); /e .printstacktrace(); 编译并运行,当输入除数为0时,将有异常出现,当输入的不是整数时,如将30输成了3o,出现的是另一种异常。定义异常编写程序包含自定义异常,当输入数值为13和4时抛出该异常。编辑ex2.java,设保存在d:myjava目录下。 class ex2 extends exception ex2(string msg) super(msg); class myex private int x; void setx(int x) this.x=x; void f
20、1() throws ex2 if(x=13) throw new ex2(i dont like 13!); else if(x=4) throw new ex2(i dont like 4!); else system .out.println(100/x);public static void main(string args ) myex a=new myex(); try a.setx(5); /a.setx(13);/a.setx(4);/a.setx(0);a.f1(); catch(exception e) system.out.println(get message:+e.g
21、etmessage(); 编译并运行,分别取消注释上面程序中被注释的语句。当释放a.setx(13)语句后,查看运行结果,当释放a.setx(4)语句后,查看运行结果。五、实验结果实验三、gui(图形用户接口)实验一、实验目的 掌握用mouselistener和mousemotionlistener接口处处理鼠标事件mouse event的方法。掌握制作菜单及处理菜单事件的方法掌握创建对话框及定位、显示、激活和关闭对话框的方法二、预习内容 图形用户接口编程所需的基本类及使用方法三、实验设备与环境 装有java语言工具软件 (jcreator )的微机若干四、实验内容 制作一个简单的画板编辑mo
22、u.java,设保存在d:myjava目录下。import java.applet.*;import java.awt.*;import java.awt.event.*;public class mou extends applet implements mousemotionlistener int x= -1,y= -1; public void init() setbackground(color.cyan); addmousemotionlistener(this); public void paint(graphics g) if(x!= -1&y!= -1) g.setcolor
23、(color.red); g.drawline(x,y,x,y); public void mousedragged(mouseevent e) x=(int)e.getx(); y=(int)e.gety(); public void mousemoved(mouseevent e) /由于使用的是listener,需要将其他不重载的方/法,列举在这里 public void update(graphics g) paint(g); public static void main(string args) mou mou=new mou(); mou.setvisible(true); mo
24、u.init(); mou.paint() 编译并运行查看结果.菜单的编写编辑testmenu.java,设保存在d:myjava目录下。import java.awt.*;import java.awt.event.*;import java.awt.event.itemevent;class mymenhframe extends frame implements actionlistener,itemlistener menubar m_menubar; menu menufile,menuedit,m_edit_paste; menuitem mi_file_open,mi_file_
25、close,mi_file_exit,mi_edit_copy; menuitem pi_new,pi_del,pi_pro,mi_paste_all,mi_paste_part; checkboxmenuitem mi_edit_cut; popupmenu popm; textarea ta; public void itemstatechanged(itemevent e) mymenhframe() super(拥有菜单的窗口); /指定窗口标题 ta=new textarea(no choice,5,20); ta.addmouselistener(new handlemouse(t
26、his); /文本域响应鼠标事件 add(center,ta); /创建弹出式菜单 popm=new popupmenu(); pi_new=new menuitem(新建); pi_new.addactionlistener(this); popm.add(pi_new); pi_del=new menuitem(删除); pi_del.addactionlistener(this); popm.add(pi_del); pi_pro=new menuitem(属性); pi_pro.addactionlistener(this); popm.add(pi_pro); /将弹出式菜单加在文本
27、域上 ta.add(popm); m_menubar=new menubar(); /创建菜单栏 menufile=new menu(文件); /创建菜单项、菜单自项并指定快捷键 mi_file_open=new menuitem(打开,new menushortcut(o); mi_file_close=new menuitem(关闭); mi_file_exit=new menuitem(退出); mi_file_exit.setshortcut(new menushortcut(x); mi_file_open.setactioncommand(打开); mi_file_close.se
28、tactioncommand(退出); mi_file_open.addactionlistener(this); mi_file_close.addactionlistener(this); /自身作为监视器 mi_file_exit.addactionlistener(this); menufile.add(mi_file_open); menufile.add(mi_file_close); menufile.addseparator(); menufile.add(mi_file_exit); m_menubar.add(menufile); menuedit=new menu(编辑)
29、; mi_edit_copy=new menuitem(复制); mi_edit_cut=new checkboxmenuitem(剪切); m_edit_paste=new menu(粘贴); mi_paste_all=new menuitem(全部粘贴); mi_paste_part=new menuitem(部分粘贴); mi_edit_copy.addactionlistener(this); mi_edit_cut.additemlistener(this); m_edit_paste.add(mi_paste_part);/ m_edit_paste.additemlistener
30、(mi_paste_all); mi_paste_part .addactionlistener(this); mi_paste_all.addactionlistener(this); menuedit.add(mi_edit_copy); menuedit.add(mi_edit_cut); menuedit.addseparator(); menuedit.add(m_edit_paste); m_menubar.add(menuedit); this.setmenubar(m_menubar); /在窗口中添加菜单栏 public void actionperformed(action
31、event e) if(e.getactioncommand()=退出) dispose(); system.exit(0); elseta.settext(e.getactioncommand(); public void itenstatechanged(itemevent e) if(e.getsource()=mi_edit_cut)if(checkboxmenuitem)e.getsource().getstate() ta.settext(choose+(checkboxmenuitem)e.getsource().getlabel();/将时间小时在文/本框中显示else ta.
32、settext(no choose+(checkboxmenuitem)e.getsource().getlabel(); class handlemouse extends mouseadapter /处理鼠标事件类 mymenhframe m_parent; handlemouse(mymenhframe mf) m_parent=mf; public void mousereleased(mouseevent e) /鼠标按键松开事件弹出菜单 if(e.ispopuptrigger()/检查鼠标事件是否是由弹出菜单引发的m_parent.popm.show(component)e.get
33、source(),e.getx(),e.gety();public static void main(string args)mymenhframe mmf=new mymenhframe();mmf.setsize(400,300);mmf.setvisible(true);编译并运行testmenu.class查看结果。使用dialog实验消息对话框和一般对话框编辑testdialog.java,设保存在d:myjava目录下。 import java.awt.*;import java.awt.event.*;public class testdialog public static v
34、oid main(string args ) mydialogframe df=new mydialogframe(); class mydialogframe extends frameimplements actionlistener,componentlistener dialog megdlg,inoutdlg; button btn1,btn2,btny,btnn,btnr; textfield tf=new textfield(no imformation,45); textfield getmeg=new textfield(inout imformation,10); mydi
35、alogframe() super(use dialog); btn1=new button(隐藏); btn2=new button(询问); btny=new button(是); btnn=new button(否); btnr=new button(返回); setlayout(new flowlayout(); add(tf); add(btn1); add(btn2); btn1.addcomponentlistener(this); this.addwindowlistener(new winadpt();/frame响应窗口关闭事件 btn1.addactionlistener
36、(this); btn2.addactionlistener(this); btny.addactionlistener(this); btnn.addactionlistener(this); btnr.addactionlistener(this); setsize(350,150); show(); public void actionperformed(actionevent e) /实现action listener中的方法 if(e.getactioncommand()=隐藏) megdlg=new dialog(this, true?,true); panel p1=new pa
37、nel(); p1.add(new label(continue?); megdlg.add(center,p1); panel p2=new panel(); p2.add(btny); p2.add(btnn); megdlg.add(south,p2); megdlg.setsize(200,100); megdlg.show();else if(e.getactioncommand()=响应) inoutdlg=new dialog(this, input the imformation); inoutdlg.add(center,getmeg); inoutdlg.add(south
38、,btnr); inoutdlg.setsize(200,100); /inoutdlg.addfocuslistener(new mydialogframe();/addfocuslistener(this); inoutdlg.show(); else if(e.getactioncommand()=是) megdlg.dispose(); btn1.setvisible(false); else if(e.getactioncommand()=否) megdlg.dispose(); else if(e.getactioncommand()=返回) tf.settext(getmeg.g
39、ettext()+是对话框的输入); inoutdlg.dispose(); /列举component listener中未重载的方法public void componentshown(componentevent e) public void componentresized(componentevent e) public void componentmoved(componentevent e) public void componenthidden(componentevent e) /实现componentlistener中的方法 tf.settext(按钮+(button)e.g
40、etcomponent().getlabel()+获得了注意的焦点); public void fousgained(focusevent e) /处理focuslistener中的方法 getmeg.settext(对话框+(dialog)e.getcomponent().gettitle()+获得了注意的焦点); public void focuslost(focusevent e) class winadpt extends windowadapter public void windowclosing(windowevent e) /处理关闭窗口事件 (frame)e.getwindo
41、w().dispose();system.exit(0);编译并运行查看结果五、实验结果实验四、多线程实验一、实验目的 掌握多线程的实现方法学会利用多线程来显示动画二、预习内容 线程与进程的基础知识三、实验设备与环境 装有java语言工具软件 (jcreator )的微机若干四、实验内容使用runnable接口的方法实现多线程编辑testrunnable.java,保存在d:myjava目录下。import java.applet.*;import java.awt.*;public class testrunnable extends applet implements runnable l
42、abel prompt1=new lable(“the first thread”); label prompt2=new lable(“the second thread”); textfield threadfirst=new textfield(14); textfield threadsecond=new textfield(14);thread thread1, thread2;int count1=0,count2=o0;public void inin() add(prompt1); add(threadfirst); add(prompt2); add(threadsecond
43、); public void start() thread1=new thread(this, “firstthread”); thread2=new thread(this, “secondthread”); thread1.start(); thraed2.start(); public void run() string currentrunning; while(true) try thread.sleep(int)(math.random()*10000); catch(exception e) currentrunning=thread.currebttheard().getnam
44、e(); if(currentrunning.equals(“firsttheard”) count1+; threadfirst.settext(“the first thread”+count1+“use”); else if(currentrunning.equals(“secondthread”) count2+; threadsecond.settext(“the second thread”+count2+“use”); (1) 编译testrunnable.java。(2) 编辑testrunnable.htm,要求与testrunnable.class在同一目录下。(3) 运行
45、testrunnable.htm。 实现简单动画实现一个简单动画,效果为一个球由小到大,从屏幕左侧滚动到右侧。编辑testrunnable.java,设保存在d:myjava目录下。import java.applet.*;import java.awt.*;public class mov extends applet int x1=50,x2=5,y1=25,y2=5; public void paint(graphics g) int w=this.getwhidth(); int h=this.get.height(); if(x1=w) x1=50; if(x2h) x2=5; g.
46、setcolor(color.blue); g.filloval(x1,y1,x2,x2); g.drawoval(x1,y1,x2,x2); x1+=50; x2+=5; try thread.sleep(500); catch(exception e) repaint();(1) 编译mov.java(2) 编辑mov.htm,要求与mov.class在同一目录下。 (3) 运行mov.htm。五、实验结果实验五、输入输出流实验一、实验目的 了解文件的概念和文件对象的创建方法了解fileinputstream和fileoutoutstream的基本概念学会创建文件输入输出流掌握使用文件输入
47、输出流读写文件的方法二、预习内容 输入输出类的使用方法三、实验设备与环境 装有java语言工具软件 (jcreator )的微机若干四、实验内容 编写程序读取文本文件内容编辑testfiledialog.java,设保存在d:myjava目录下。import java.io.*;import java.awt.*;import java.awt.event*;public class testfiledialog public static void main(string args ) new fileframe(); class fileframe extends frame implem
48、ents actionlistener textarea ta; button open,quit; filedialog fd; filefrane() super(“获取显示文本文件”); ta=new textarea(10,45); open=new button(“打开”) quit=new button(“关闭”) open.addactionlistener(this); quit.addactionlistener(this); setlayout(new flowlayout(); add(ta); add(open); add(quit); show(); public v
49、oid actionperformed(actionevent e) if (e.getactioncommand()=“打开”) fd=new filedialog(this, “打开文件”,filedialog.load;) fd.setdirectory(“d:”); fd.show(); try file myfile=new file(fd.getdirectory(),fd.getfile(); system.out.print(myfile); filereader inb=new bufferedreaser(infile); string filecountent=“”,ste=“”; while(filecontent-inb.teadline()!=null) str=srt+filecontent+“n”; ta.append(filecontent+“n”); catch(toexception ie) system.out.println(ie.t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 宁夏银川外国语实验学校2026届数学九上期末考试试题含解析
- 新型健康产业:养生知识付费内容工厂行业的机遇与挑战
- 基于核心素养的现代教育技术课程建设研究
- 2025标准版授权买卖合同范本
- 四川省峨眉山市2026届数学八年级第一学期期末学业水平测试模拟试题含解析
- 邮储银行驻马店市汝南县2025秋招笔试思维策略题专练及答案
- 邮储银行柳州市鱼峰区2025秋招笔试管理营销专练及答案
- 中国银行包头市九原区2025秋招笔试言语理解题专练及答案
- 邮储银行周口市西华县2025秋招笔试计算机基础专练及答案
- 工商银行百色市田林县2025秋招笔试经济学专练及答案
- 《人与动物的关系》课件
- 注射相关感染预防与控制
- 二年级语文《坐井观天》说课课件
- DL∕T 741-2019 架空输电线路运行规程
- 数学同步练习册基础模块(上册)参考答案
- DZ∕T 0173-2022 大地电磁测深法技术规程(正式版)
- 锅炉专工试题
- 医疗质量与安全教育培训
- 2024年江苏省生态环境监测专业技术人员大比武竞赛备考试题库(含答案)
- 《手足口病》课件
- 山东省地震安全性评价收费项目及标准
评论
0/150
提交评论