JAVA程序设计实验报告.doc_第1页
JAVA程序设计实验报告.doc_第2页
JAVA程序设计实验报告.doc_第3页
JAVA程序设计实验报告.doc_第4页
JAVA程序设计实验报告.doc_第5页
免费预览已结束,剩余22页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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: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();不能对接口进行实例化UseInterface x=new UseInterface();x.func1();x.func2(k);多态在工资系统中的应用下面给出一个根据雇员类型利用abstract方法和多态性完成工资单计算的程序。 Employee是抽象类,Employee的子类有Boss(每星期发给他固定工资,而不计工作时间)、 CommissionWorker(除基本工资外还根据销售额发放浮动工资)、PieceWorker(按其生产的产品数发放工资)、HourlyWorker(根据工作时间长短发放工资)。该例的Employee的每个子类都声明为final,因为不需要再继承它们生成子类。对所有雇员类型都使用earnings()方法,但每个人挣的工资按他所属的雇员类计算,所有雇员类都是从超类Earnings()派出生的。所有在超类中声明earnings()为抽象方法,并且对于每个子类都提供恰当的earnings()的实现方法。为了计算雇员的工资,程序仅仅使用雇员对象的一个超类引导 并调用earnings()方法。在一个实际的工资系统中,各种Employee对象的引用可以通过一个Employee引用数组来实现。程序依次使用数组的每个元素(Employee引用)调用每个对象的employee()方法。(1)编辑Test.java,设保存在D:myjava目录下。 abstract class Employee 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 earnings();/定义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();/定义CommissiomWorker类,为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 earnings() */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 void 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: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 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 static 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+=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;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 constrartion+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目录下。 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 )的微机若干四、实验内容 异常的捕获计算两数相除并输出结果。使用两个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); System .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(ArithmeticException 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 f1() 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.getMessage(); 编译并运行,分别取消注释上面程序中被注释的语句。当释放a.setX(13)语句后,查看运行结果,当释放a.setX(4)语句后,查看运行结果。五、实验结果实验三、GUI(图形用户接口)实验一、实验目的 掌握用MouseListener和MouseMotionListener接口处处理鼠标事件Mouse Event的方法。掌握制作菜单及处理菜单事件的方法掌握创建对话框及定位、显示、激活和关闭对话框的方法二、预习内容 图形用户接口编程所需的基本类及使用方法三、实验设备与环境 装有JAVA语言工具软件 (JCreator )的微机若干四、实验内容 制作一个简单的画板编辑Mou.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(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); mou.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_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(this); /文本域响应鼠标事件 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); /将弹出式菜单加在文本域上 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.setActionCommand(退出); 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(编辑); 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(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(ActionEvent 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.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.getSource(),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 void 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); MyDialogFrame() 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(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 Panel(); 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,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.getText()+是对话框的输入); 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.getComponent().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.getWindow().dispose();System.exit(0);编译并运行查看结果五、实验结果实验四、多线程实验一、实验目的 掌握多线程的实现方法学会利用多线程来显示动画二、预习内容 线程与进程的基础知识三、实验设备与环境 装有JAVA语言工具软件 (JCreator )的微机若干四、实验内容使用Runnable接口的方法实现多线程编辑TestRunnable.java,保存在D:myjava目录下。import java.applet.*;import java.awt.*;public class TestRunnable extends Applet implements Runnable Label 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); 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().getName(); 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) 运行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.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的基本概念学会创建文件输入输出流掌握使用文件输入输出流读写文件的方法二、预习内容 输入输出类的使用方法三、实验设备与环境 装有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 implements 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 void 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论