java实验报告 2.doc_第1页
java实验报告 2.doc_第2页
java实验报告 2.doc_第3页
java实验报告 2.doc_第4页
java实验报告 2.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

实 验 报 告课程:Java 编程技术 班级:网络1203班 学号: 姓名: 实验 4 面向对象编程 一、实验目的 通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想,了解类的封装方法,以及 如何创建类和对象,了解成员变量和成员方法的特性,掌握 OOP 方式进行程序设计的方法,了解类的继承性和多态性的作用。二、实验内容1. 创建一个名为Dog的类,它具有重载的bark()方法。bark()方法应根据不同的基本数据类型的参数来进行重载,bark()方法可根据参数输出狗吠(barking)、咆哮(howling)等信息。编写main()方法来调用不同的bark()方法。源代码如下: public class Dog void f(int m)System.out.println(barking!);void f(double n)System.out.println(hoeling!);public static void main(String args)Dog d=new Dog();d.f(2);d.f(2.2);运行界面如下:2. 创建Rodent(啮齿动物)类,其子类有Mouse(老鼠)、Mole(鼹鼠)、Hamster(大颊鼠)。在父类中,提供对所有的Rodent通用的方法。在子类中,根据该子类特定的行为习性来覆盖这些方法。例如老鼠属杂食(omnivorous),鼹鼠主食昆虫(insect),大颊鼠主食植物种子(plant seed)。创建一个Rodent数组,填充不同的数据类型,然后调用父类的方法,观察会发生什么情况。源代码如下:public class Rodent void eat()System.out.println(zhushi);public static void main(String args)Rodent r=new Rodent4;Rodent rodent=new Rodent();Mouse mouse=new Mouse();Mole mole=new Mole();Hamster hamster=new Hamster();r0=rodent;r1=mouse;r2=mole;r3=hamster;r0.eat();r1.eat();r2.eat();r3.eat();class Mouse extends Rodentvoid eat()System.out.println(omniovrous!);class Mole extends Rodentvoid eat()System.out.println(insect!);class Hamster extends Rodentvoid eat()System.out.println(plant seed!);运行界面如下:3. 3.修改上述第9题中的Rodent类,使其成为一个抽象类。只要有可能,就将Rodent的方法声明为抽象方法。创建各种啮齿动物类,观察程序的输出。 源代码如下:public class Rodent1 public static void main(String args) Rodent r=new Rodent3; Mouse m=new Mouse(); Mole s=new Mole(); Hamster h=new Hamster(); r0=m; r1=s; r2=h; r0.eat(); r1.eat(); r2.eat();abstract class Rodentpublic abstract void eat();class Mouse extends Rodentpublic void eat()System.out.println(omnivorous!);class Mole extends Rodentpublic void eat()System.out.println(insects!);class Hamster extends Rodentpublic void eat()System.out.println(plant seeds!);运行界面如下:实验 6 图形用户界面 一、实验目的 了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,了解如何使用布局管理器对组件进行管理,以及如何使用 Java 的事件处理机制。 2、 实验内容1 编写程序界面,其中包括一个标签、一个文本框和一个按钮。当用户单击按钮时,程序把文本框中的内容复制到标签中。代码如下:import java.awt.*; import java.awt.event.*; public class CopyStringToLabel extends Frame implements ActionListener private Label label = new Label(); private TextField output = new TextField(); private Button copy = new Button(Copy); private class WindowCloser extends WindowAdapter public void windowClosing(WindowEvent we) System.exit(0); public CopyStringToLabel () super(Copy a String To Label); setup(); copy.addActionListener(this); addWindowListener(new WindowCloser(); public void actionPerformed(ActionEvent e) if(e.getSource() = copy) label.setText(output.getText(); private void setup() Panel textGrid = new Panel(); textGrid.setLayout(new GridLayout(2,1); textGrid.add(label); textGrid.add(output); setLayout(new BorderLayout(); add(Center,textGrid); add(South,copy); pack(); setVisible(true); public static void main(String args) CopyStringToLabel cop = new CopyStringToLabel(); 运行结果:2 编写一个组合处理鼠标事件和键盘事件的程序。该程序模拟一个电子白板,用户可用鼠标在白板上画画,并通过键盘在上面写字。代码如下:import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class MouseAndKeyDemo extends Applet protected int lastX = 0, lastY = 0; public void init() addMouseListener(new PositionRecorder(); addMouseMotionListener(new LineDrawer(); addKeyListener(new CharDrawer(); setForeground(Color.black); setBackground(Color.white); protected void record(int x, int y) lastX = x; lastY = y; private class PositionRecorder extends MouseAdapter public void mouseEnter(MouseEvent e) requestFocus(); record(e.getX(),e.getY(); public void mousePressed(MouseEvent e) record(e.getX(),e.getY(); private class LineDrawer extends MouseMotionAdapter public void mouseDragged(MouseEvent e) int x = e.getX(); int y = e.getY(); Graphics g = getGraphics(); g.drawLine(lastX, lastY, x, y); record(x, y); private class CharDrawer extends KeyAdapter public void keyTyped(KeyEvent event) String s = String.valueOf(event.getKeyChar(); getGraphics().drawString(s, lastX, lastY); record(lastX + 11, lastY); 运行结果:3、 编写一个程序,能够用鼠标画直线、圆、矩形、弧线等。代码如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;class PainterPanel extends JPanel implements MouseListener int shape = -1; Point point = new Point2; public PainterPanel () super (); this.setBackground(Color.white); point0 = new Point (-1, -1); point1 = new Point (-1, -1); addMouseListener (this); public void mouseReleased(MouseEvent e) point1=new Point(e.getX(),e.getY(); repaint(); public void mouseEntered (MouseEvent e) public void mouseExited (MouseEvent e) public void mouseClicked (MouseEvent e) public void mousePressed (MouseEvent e) point0 = new Point(e.getX(), e.getY(); public void paint(Graphics g) super.paint(g); switch (shape) case 0: g.drawLine (point0.x, point0.y, point1.x, point1.y); break; case 1: int width = point1.x - point0.x; int height= point1.y - point0.y; g.drawOval (point0.x, point0.y, width, width); break; case 2: width = point1.x - point0.x; height = point1.y - point0.y; g.drawRect(point0.x, point0.y, width, height); break; case 3: width = point1.x - point0.x; height = point1.y - point0.y; g.drawArc(point0.x,point0.y, width, height,0,95); break; public void drawShape(int shape) this.shape=shape; public class PainterDemo extends JFrame JButton button = new JButton4; PainterPanel painter = new PainterPanel(); public PainterDemo() super(Java画图小程序); String buttonName = 直线, 圆, 矩形,弧形; DrawShapeListener buttonListener = new DrawShapeListener(); JToolBar toolBar = new JToolBar(); ButtonGroup buttonGroup = new ButtonGroup(); for (int i = 0; i button.length; i +) buttoni = new JButton (buttonNamei); buttoni.addActionListener (buttonListener); buttonGroup.add(buttoni); toolBar.add(buttoni); Container container=getContentPane(); container.add (toolBar,BorderLayout.NORTH); container.add (painter,BorderLayout.CENTER); setSize(300,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); class DrawShapeListener implements ActionListener public void actionPerformed(ActionEvent e) for (int i = 0; i button.length; i +) if (e.getSource() = buttoni) painter.drawShape(i); public static void main(String args) new PainterDemo(); 运行结果: 实验 7 1、 实验目的:1、了解java循环控制。2、 实验内容:1、 用控制语句编写一个程序,求出1000以内的所有素数。然后再编写一个可以比较while语句和do-while语句的程序。代码如下:public class Prime public static void main(String args) int i,j,k=0,flag; for(i=2;i=1000;i+) flag = 1; for(j=2;ji;j+) if(i%j=0) flag=0; break; if(flag!=0) System.out.print(i+t); k+; if(k%15=0) System.out.print(n); 运行结果:比较while和do-while的代码:public class BiJiao public static void main(String args) int i = 1; int sum1=0;while(i=5)sum1+=i;i+; System.out.println(五以内的数和由while语句计算的结果);System.out.println(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论