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

下载本文档

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

文档简介

java实验报告java上机实验报告姓名: 郭星 班级: 网络121班 学号: 122547 实验一 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); /布局 pri

温馨提示

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

评论

0/150

提交评论