




已阅读5页,还剩67页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JAVA实验报告JAVA上机实验报告实验一 JAVA 基本语法一、实验目的 了解 Java 的数据类型,掌握各种变量的声明方式,理解运算符的优先级,掌握 Java 基本数据类型、运算符与表达式,掌握顺序结构、选择结构和循环结构语法的程序设计方法。二、实验要求1、编写一个声明 Java 不同数据类型变量的程序。2、编写使用不同选择结构的程序。3、编写使用不同循环结构结构的程序。三、实验内容和步骤1、程序代码:public class SimpleTypespublic static void main(String args)byte b=0x55;short s=0x55ff;int i=1000000;long l=0xfffL;char c=c;float f=0.23F;double d=0.7E-3;boolean bool=true;System.out.println(b=+b);System.out.println(s=+s);System.out.println(i=+i);System.out.println(l=+l);System.out.println(c=+c);System.out.println(f=+f);System.out.println(d=+d);System.out.println(bool=+bool);运行结果:2、程序代码:public class TestIfpublic static void main(String args)boolean leap;int year=2005;if(year%4=0&year%100!=0)|(year%400=0) /方法1System.out.println(year+ 年是闰年);elseSystem.out.println(year+ 年不是闰年);year=2008; /方法2if(year%4!=0)leap=false;else if(year%100!=0)leap=true;else if(year%400!=0)leap=false;elseleap=true;if(leap=true)System.out.println(year+ 年是闰年);elseSystem.out.println(year+ 年不是闰年);year=2050; /方法3if(year%4=0)if(year%100=0)if(year%400=0)leap=true;elseleap=false;elseleap=true; /此行实验指导书给出的是leap=false,但我认为是trueelseleap=false;if(leap=true)System.out.println(year+ 年是闰年);elseSystem.out.println(year+ 年不是闰年);运行结果:3、程序代码:class SwitchTestpublic static void main(String args)throws java.io.IOExceptionchar a;System.out.println(Enter a number from 1-3:);a=(char)System.in.read();switch(a)case1:System.out.println(win a Car!);break;case2:System.out.println(picked the goat);break;case3:System.out.println(get to keep your 100);break;default:System.out.println(entry);运行结果:实验二 面向对象编程一、实验目的:通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。二、实验要求: 1、编写程序实现类的定义和使用。2、编写不同成员和不同成员方法修饰方法的程序。3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。4、编写接口的定义和使用的程序。5、编写包的定义和使用的程序。三、实验内容和步骤1、类的定义和使用1.1.1实验程序:import java.util.*;public class TestDateprivate int year;private int mouth;private int day;public void outDate()System.out.println(day+日+mouth+月+year+年);public void addDay() day = day+1;public void SetDate(int a,int b,int c)day = a;mouth = b;year = c;public static void main(String args)Calendar ca = Calendar.getInstance();int d = ca.get(Calendar.DATE);int m = ca.get(Calendar.MONTH);int y = ca.get(Calendar.YEAR);TestDate T = new TestDate();T.SetDate(d,m,y);T.outDate();1.1.2运行结果:1.2.1 实验程序:public class TestTable public static void main(String args) Table table=new Table(书桌,40,60,100,100); table.Area(); table.Display(); table.ChangeWeight(50); class TableString tablename;float weight;float width;float length;float height;public Table()public Table(String name,float weight,float width,float length,float height)this.tablename=name;this.weight=weight;this.width=width;this.length=length;this.height=height;public void Area()System.out.println(桌子的面积为:+(width*length)+cm2);public void Display()System.out.println(桌子的名字为:+tablename);System.out.println(桌子的重量为:+weight+kg);System.out.println(桌子的宽度为:+width+cm);System.out.println(桌子的长度为:+length+cm);System.out.println(桌子的高度为:+height+cm);public void ChangeWeight(int w)weight=(float)(w);System.out.println(改变后的桌子的重量为:+weight+kg);1.2 实验结果:1.3 结果分析:本程序中含有两个类,一个为TestTable类,含有main方法;另一个为Table类,定义了和桌子有关的一些属性和方法;在main方法中定义了一个Table类的对象,对该对象进行了相关的操作。2、修饰符的使用2.1 实验程序:class StaticDemo static int x; int y; public static int getX() return x; public static void setX(int newX) x= newX; public int getY() return y; public void setY(int newY) y= newY; public class TestDemo public static void main(String args) System.out.println(静态变量x=+StaticDemo.getX(); System.out.println(实例变量y=+StaticDemo.getY();/非法,编译时将出错 StaticDemo a=new StaticDemo(); StaticDemo b=new StaticDemo(); a.setX(1); a.setY(2); b.setX(3); b.setY(4); System.out.println(静态变量aX=+a.getX(); System.out.println(实例变量a.y=+a.getY(); System.out.println(静态变量b.x=+b.getX(); System.out.println(实例变量b.y=+b.getY(); 2.2 实验结果:2.3 结果分析:错误:方法getY()不是静态方法,不能有类直接调用;x为静态变量,每一个对象都会影响它的值;所以当对象b修改了x的值后,对象a调用x的值也会变;而y是实例变量,每个对象都有一个。故对象a,b的y值是不同的。3、继承和多态的作用3.1 实验程序:public class TestRodent public static void main(String args) Rodent rodent=new Rodent3; rodent0=new Mouse(mouse); rodent1=new Gerbil(gerbil); rodent2=new Hamster(hamster); rodent0.run(); rodent1.run(); rodent2.run(); abstract class Rodentpublic String name;public String getName()return name;public void setName(String name)=name;public abstract void run();class Mouse extends Rodentpublic Mouse(String name)=name;public void run()System.out.println(+ is running!);class Gerbil extends Mousepublic Gerbil(String name)super(name);class Hamster extends Mousepublic Hamster(String name)super(name);3.2 实验结果:3.4 结果分析:首先定义了一个抽象类Rodent,其中有一个run()抽象方法;Mouse类继承了Rodent类并且实现了该抽象方法;Gerbil类继承类Mouse类,所以具有了run()方法;Hamster类继承自Mouse类,也具有了run()方法。4、接口的定义和使用4.1 实验程序:interface cal_area double PI=3.14; double calculate_area();class circle implements cal_area double r; public void circle(double r) this.r=r; public double calculate_area() return PI*r*r; public class InterfaceTest public static void main(String args) double x; circle y=new circle();System.out.println(半径为:4); y.circle(4); x=y.calculate_area(); System.out.println(面积为:+x); 4.2 实验结果:4.3 结果分析:错误:circle类的构造函数有参数,而实例化circle的对象时错误;不能显示的调用构造函数。circle实现了接口cal_area的方法calculate_area;然后类InterfaceTest中的main方法生成了circle的对象,调用calculate_area方法输出圆的面积。5包的定义和使用 创建自定义包Mypackage 在存放源程序的文件夹中建立一个子文件夹Mypackage。例如,在“D:hpf”文件夹之中创建一个与包同名的子文件夹Mypackage(D:hpfMypackage),并将编译过的class文件放入该文件夹中。注意:包名与文件夹名大小写要一致。再添加环境变量classpath的路径,例如:E:j2sdkl.4.2_Ollib; 在包中创建类 YMD.java程序功能:在源程序中,首先声明使用的包名Mypackage,然后创建YMD类,该类具有计算今年的年份并输出一个带有年月日的字符串的功能。源代码如下 package Mypackage; /声明存放类的包 import java.util.*; /引用java.util包 public class Test_YMD private int year,month, day; public static void main(String arg3) public Test_YMD(int y,int m,int d) year=y; month=(m=1)&(m=1)&(d0) return true; else return false; boolean isBalance()throws InsufficientFoundsException /收支平衡 if(this.Balance() return true; else throw new InsufficientFoundsException(this,(income-payout); public static void main(String args) BankAccount bankAcc1=new BankAccount(1001,100,50);/创建第一个对象 BankAccount bankAcc2=new BankAccount(1002,100,300);/创建第二个对象 try if(bankAcc1.isBalance() /如果收支平衡 就显示ok! System.out.println(account:+bankAcc1.getAccountNumber()+ ok!); if(bankAcc2.isBalance() System.out.println(account:+bankAcc1.getAccountNumber()+ ok!); catch(InsufficientFoundsException i) /捕获 System.out.println(i.toString(); /自定义的异常处理类class InsufficientFoundsException extends Exception private BankAccount m_ba; private double getAmount; InsufficientFoundsException(BankAccount ba,double dAmount) super(Insufficient founds in account); m_ba=ba; getAmount=dAmount; public String toString() StringBuffer sb=new StringBuffer(); sb.append(Insufficient founds in account:); sb.append(m_ba.getAccountNumber(); sb.append(nBalance was:); sb.append(m_ba.Balance(); sb.append(ngetAmount was: ); sb.append(getAmount); return sb.toString(); 1.2 实验结果:2.1 试验程序:public class TestExcep1public static void main(String args)int i=0;String greeting=Hello,Only,Test;while(i4)trySystem.out.println(greetingi);catch(ArrayIndexOutOfBoundsException e)System.out.println(越界); i=-1; finallySystem.out.println(总会运行);i+;2.2 实验结果:2.3 结果分析:始终执行finallySystem.out.println(总会运行);语句。3.1 试验程序:public class TestExcep2public static void main(String args)System.out.println(这是一个异常处理的例子n);tryint i=10;i/=0;catch(ArithmeticException e)System.out.println(异常是:+e.getMessage();finallySystem.out.println(finally语句被执行);3.2 实验结果:实验五:系统I/O程序设计实验目的:理解数据流的概念、Java流的层次结构及文件的概念;熟悉图形用户界面基本组件的使用方法,熟悉如何使用布局管理器对组件进行管理及如何使用Java的事件处理机制。实验要求:1、掌握字节流和字符流的基本使用方法。2、能够创建、读写、更新文件。3、掌握在Applet容器中添加组件的方法,掌握使用布局管理器对组件进行管理的方法。4、理解 Java的事件处理机制,掌握为不同组件编写事件处理程序的方法。5、掌握编写独立运行的窗口界面的方法。6、了解对话框及Java Swing组件的使用方法。实验内容:3、创建图形用户界面图形用户界面(Graphic User Interface,简称GUI)是为方便用户使用设计的窗口界面,在图形用户界面中用户可以看到什么就操作什么,取代了在字符方式下知道是什么后才能操作什么的方式。组件(Component)是构成GUI的基本要素,通过对不同事件的响应来完成和用户的交互或组件之间的交互。组件一般作为一个对象放置在容器(Container)内,容器是能容纳和排列组件的对象,如Applet、Panel(面板)、Frame(窗口)等。通过容器的add方法把组件加入到容器中。在Applet中添加标签、按钮并使用网格布局程序功能:在Applet容器中添加组件标签、按钮,并使用网格布局管理器排列组件在容器中的位置。源代码如下:import java.awt.*; import java.applet.Applet;public class Test_Button extends AppletLabel l1;Button b1, b2, b3, b4, b5, b6;public void init() setLayout(new GridLayout(3,3);/ 设置网格布局(3行3列共9个网格)l1=new Label(标签1);b1 = new Button(按钮1);b2 = new Button(按钮2);b3 = new Button(按钮3);b4 = new Button(按钮4);add(l1);add(b1);add(b2);add(b3);add(new Label();add(b4);add(new Button(按钮5); add( new Button(按钮6); add(new Label(标签2);编译程序,编写显示Applet的html页面文件。在面板中添加组件程序功能:在Applet中添加面板容器,并分别在Applet、面板容器中添加组件并使用不同的布局管理方式。源代码如下:import java.awt.*;import java.awt.Color;import java.applet.Applet;public class Test_Component extends Applet public void init() /设置最底层的 Applet容器为顺序布局setFont(new Font(Arial,Font.PLAIN,20);Label l=new Label(这是最底层的 Applet容器中的标签,Label.CENTER);add(l);Panel panel1=new Panel();add( panel1);panel1.setBackground(Color.blue);panel1.setForeground(Color.red);panel1.setLayout(new BorderLayout();/设置边界布局panel1.add(North, new Button(北);panel1.add(South, new Button(南);panel1.add(East, new Button(东);panel1.add(West, new Button(西);panel1.add(Center, new Label(这是在 Panel1面板中部添加的标签); Panel panel2=new Panel();add( panel2);panel2.setLayout(new GridLayout(3,1); /设置网格布局Choice c=new Choice ();/创建下拉式列表c.addItem(北京);c.addItem(上海);c.addItem(天津);Label l1=new Label(这是在 Panel2面板中的标签);Button b1=new Button(Panel2中的按钮);panel2.setBackground(Color.green);panel2.add(l1);panel2.add(b1);panel2.add(c);编译程序,编写显示Applet的html页面文件。5、从键盘输入一个整型数,一个双精度型和一个字符串,用DataOutputStream将这些数据输出到文件中,然后用DataInputStream从文件中读出这些数据并打印到标准输出设备。源程序:import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.IOException;public class TestDataOutputStream public static void main(String args) throws IOException BufferedReader bf = new BufferedReader(new InputStreamReader(System.in); File newFile = new File( e:TestDataOutputStream.txt); try FileOutputStream fOut = new FileOutputStream(newFile); DataOutputStream dOut = new DataOutputStream(fOut); System.out.println(Please input Integer:); int i = Integer.parseInt(bf.readLine(); System.out.println(Please input Float:); float f = Float.parseFloat(bf.readLine(); System.out.println(Please input Double:); double d = Double.parseDouble(bf.readLine(); System.out.println(Please input Boolean:); boolean b = new Boolean(bf.readLine().booleanValue(); dOut.writeInt(i); dOut.writeFloat(f); dOut.writeDouble(d); dOut.writeBoolean(b); dOut.close(); catch (FileNotFoundException e) System.out.println(e); catch (IOException e) System.out.println(e); 结果:是乱码7、在窗口的一个区域进行鼠标操作:mouseEnter,mouseExit、mousePress,mouseDrage、mouseClick。在窗口的另一个区域以文字显示鼠标所进行的相应操作。另外当鼠标进行mousePress、mouseDrage、mouseClick操作时,将显示一个图片。当鼠标拖拉时,图片随鼠标移动。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class myclass /* param args*/private JFrame fm=new JFrame(鼠标移动示例); private JLabel lb=new JLabel(拖动测试对象);private Thread th=new Thread(new dothread();private boolean canmove=false;private Component com=null;private Point pt=new Point();myclass()fm.setLayout(null);fm.setDefaultCloseOperation(3);fm.setBounds(300, 200, 450, 300);fm.addMouseListener(new mouselisten();lb.setBounds(10, 10, 100, 25);/lb.hide();th.start();fm.getContentPane().add(lb);fm.show();class dothread implements Runnablepublic void run() / TODO 自动生成方法存根while (true)if (canmove)if (fm.getMousePosition()!=null)pt.x=fm.getMousePosition().x;pt.y=fm.getMousePosition().y;lb.setLocation(pt);class mouselisten implements MouseListenerpublic void mouseClicked(MouseEvent arg0) / TODO 自动生成方法存根if (arg0.getButton()=1)canmove=true;else canmove=false;public void mouseEntered(MouseEvent arg0) / TODO 自动生成方法存根public void mouseExited(MouseEvent arg0) / TODO 自动生成方法存根public void mousePressed(MouseEvent arg0) / TODO 自动生成方法存根canmove=false;public void mouseReleased(MouseEvent arg0) / TODO 自动生成方法存根canmove=false;public static void main(String args) / TODO 自动生成方法存根new myclass();8、实现一个计算器,界面类似WINDOWS中的界面,包括09十个数字、加、减、乘、除四种运算符号和一个用于输入及显示结果的文本框,单击每个按钮都能完成相应的功能。import java.awt.*; import java.awt.event.*; public class Calculator extends Frame implements ActionListener private double a=0, b=0, sum=0; int flag=0, count=0,p=1; char mode=?; /本次运算的符号 private TextField display = new TextField(); private Button one = new Button(1); private Button two = new Button(2); private Button three = new Button(3); private Button four = new Button(4); private Button five = new Button(5); private Button six = new Button(6); private Button seven = new Button(7); private Button eight = new Button(8); private Button nine = new Button(9); private Button zero = new Button(0); private Button point = new Button(.); private Button add = new Button(+); private Button sub = new Button(-); private Button mul = new Button(); private Button div = new Button(); private Button result = new Button(=); private Button clr = new Button(CLR); private Button quit = new Button(quit); private Label label = new Label(calculator); private class WindowCloser extends WindowAdapter public void windowClosing(WindowEvent we) System.exit(0); /布局 private void setup() Pane
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小班常规活动组织与实施
- 抢救车药品规范化管理培训
- 2026届吉林省延边州高二化学第一学期期中质量跟踪监视模拟试题含解析
- 神经康复病例讲解
- 情指一体化汇报
- 小鱼干技术分享
- 学院专业建设汇报
- 透射明暗场技术
- 2026届云南省峨山县大龙潭中学高三化学第一学期期末检测模拟试题含解析
- 双重曝光案例讲解
- 电机维护检修培训课件
- 入场安全教育培训
- 2025年广东省高考政治试卷真题(含答案)
- 艺术设计专业教学标准(高等职业教育专科)2025修订
- 保密检查培训课件
- 2026届贵州省六校联盟高三高考联考卷(一)化学及答案
- 2025年七一党课-作风建设永远在路上学习教育党课
- 黄山义警队管理制度
- 十五五畜牧兽医行业发展规划
- 2025-2030中国排毒养颜茶行业发展分析及发展趋势预测与投资风险研究报告
- 2025年全国高考数学真题全国2卷
评论
0/150
提交评论