java实验报告Word格式文档下载.docx

上传人:b****5 文档编号:20621767 上传时间:2023-01-24 格式:DOCX 页数:23 大小:255.43KB
下载 相关 举报
java实验报告Word格式文档下载.docx_第1页
第1页 / 共23页
java实验报告Word格式文档下载.docx_第2页
第2页 / 共23页
java实验报告Word格式文档下载.docx_第3页
第3页 / 共23页
java实验报告Word格式文档下载.docx_第4页
第4页 / 共23页
java实验报告Word格式文档下载.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

java实验报告Word格式文档下载.docx

《java实验报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java实验报告Word格式文档下载.docx(23页珍藏版)》请在冰豆网上搜索。

java实验报告Word格式文档下载.docx

利用文件字符流和缓冲字符流读入下列文件(文件名C:

\sales.txt):

2300

5890

7830

7850

并统计以上数值之和。

二、实验设计(包括实验方案设计,实验手段的确定,实验步骤,实验过程等)

源程序:

packagepkg;

importjava.io.*;

publicclassMyClass{

publicvoidmethod01()throwsIOException

{

FileOutputStreamfos=newFileOutputStream("

c:

\\myfile.data"

);

DataOutputStreamdos=newDataOutputStream(fos);

dos.writeUTF("

Zhangsan"

dos.writeInt(20);

Zhang_san@"

LiSi"

dos.writeInt(18);

Li_Si@"

WangErMazi"

dos.writeInt(21);

Wang_Er_Mazi@"

LiuBang"

Liu_Bang@"

fos.close();

dos.close();

}

publicvoidmethod02()throwsIOException

FileInputStreamfis=newFileInputStream("

DataInputStreamdis=newDataInputStream(fis);

Stringname,email;

intage,count;

while((count=dis.available())!

=0)

name=dis.readUTF();

age=dis.readInt();

email=dis.readUTF();

System.out.printf("

%-20s\t%-6d\t%-30s\n"

name,age,email);

//\t是水平制表符,相当于TAB空格

}

fis.close();

dis.close();

publicvoidmethod03()throwsIOException

FileReaderfr=newFileReader("

\\sales.txt"

BufferedReaderbr=newBufferedReader(fr);

Stringdata;

intsum=0;

while((data=br.readLine())!

=null)

{sum+=Integer.parseInt(data);

System.out.println("

sum="

+sum);

fr.close();

br.close();

}

publicstaticvoidmain(String[]args)throwsIOException{

//TODOAuto-generatedmethodstub

MyClassmc=newMyClass();

mc.method01();

//mc.method02();

//mc.method03();

//依次调用method01()、method02()、method03()

三、主要实验工具、仪器设备及耗材

个人计算机,Eclipse

第二部分:

实验结果分析(可加页)

一、实验结果描述

先执行方法一:

再执行方法二:

最后执行方法三:

二.实验小结、建议及体会

经过这次实验,我对java语言有了初步认识,对如何使用java编程有了初步了解,并且尝试编出一个简单的小程序,实现了先生成文件再将数据写入文件的目的,这对我有很大的帮助,为我以后的java学习打下了基础,以后我会多加练习,争取更大的进步。

Java类继承及接口

一.实验内容描述(问题域描述)

1、接口之一定义四则运算的加减法,接口之二定义四则运算的乘除运算。

类一有一个打印一个字符串和一个整数的成员方法,要求打印的数据通过形式参数导入。

类二继承类一并实现接口一和接口二,且含有用于计算20和30四则运算并打印计算结果的主函数。

2、利用标准输入流输入一元二次方程的三个系数,并解该方程。

第一题源代码:

接口一:

packagejavacourse;

publicinterfaceInterface01{

publicintadd(inta,intb);

publicintsub(inta,intb);

接口二:

publicinterfaceInterace02{

publicintprod(inta,intb);

publicintdiv(inta,intb);

父类:

publicclassFatherClass{

publicstaticvoidprint(Stringstr,inta)

{

System.out.println(str+"

:

"

+a);

子类:

publicclassAChildClass

extendsFatherClass

implementsInterface01,Interace02

publicAChildClass(){

//TODOAuto-generatedconstructorstub

@Override

publicintprod(inta,intb){

//TODOAuto-generatedmethodstub

return(a*b);

2

publicintdiv(inta,intb)

try{

return(a/b);

}catch(Exceptionex)

System.out.println("

a/bwithb=0"

return-999;

publicintadd(inta,intb){

return(a+b);

publicintsub(inta,intb){

return(a-b);

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

AChildClasscc=newAChildClass();

print("

30+20"

cc.add(20,30));

30-20"

cc.sub(30,20));

30*20"

cc.prod(30,20));

30/20"

cc.div(30,20));

第二题参考代码:

publicclassROOT{

privatedoublea,b,c,x1,x2;

privatebooleanhaRealRoots;

3

publicROOT(){

super();

publicROOT(doublea,doubleb,doublec){

this.a=a;

this.b=b;

this.c=c;

publicdoublegetX1(){

returnx1;

publicdoublegetX2(){

returnx2;

publicbooleanisHaRealRoots(){

returnhaRealRoots;

publicvoidpstr(Stringstr)

System.out.println(str);

publicstaticvoidprint(Stringstr,doublea)

System.out.println(str+a);

publicvoidcalRoots()

throwsIOException,NumberFormatException

byte[]arrA,arrB,arrC;

StringstrA,strB,strC;

doubledelta;

arrA=newbyte[80];

arrB=newbyte[80];

arrC=newbyte[80];

pstr("

Inputcoefficienta:

System.in.read(arrA);

4

Inputcoefficientb:

System.in.read(arrB);

Inputcoefficientc:

System.in.read(arrC);

strA=newString(arrA);

strB=newString(arrB);

strC=newString(arrC);

a=Double.parseDouble(strA);

b=Double.parseDouble(strB);

c=Double.parseDouble(strC);

delta=b*b-4*a*c;

if(delta<

0)

this.haRealRoots=false;

else

this.haRealRoots=true;

x1=(-b+Math.sqrt(delta))/(2*a);

x2=(-b-Math.sqrt(delta))/(2*a);

publicstaticvoidmain(String[]args)

throwsException

ROOTrt=newROOT();

rt.calRoots();

if(rt.isHaRealRoots())

x1="

rt.getX1());

x2="

rt.getX2());

rt.pstr("

Don'

texistrealroots"

个人计算机,Eclipse

输入方程无解:

输入方程有解:

二、实验小结、建议及体会

经过这次实验我对java的类继承及接口有了初步的认识,同时也进一步了解了java的各种类型和变量定义以及表达式的运算,并且亲自动手编了两个程序来巩固自己所学。

java语言跟c语言极有相似之处又有不同之处,这次实验让我对java语言的学习打下了很好的基础。

掌握可视化编程,数据结构,和文件输入、输出流

实验内容:

编写一个表示学生信息(姓名,电话号码,电子邮箱,性别)的JavaBean。

要求这个“结构体”实现java.io.Serializable,以便能够把若干这样的对象写到文件中。

使用java.util.Hashtable,即哈希表,来处理这样的JavaBean。

使用文件输入、输出流和对象输入、输出流来读、写这样一个哈希表对象。

图形用户界面要用到采单,列表,对话框等。

Java.awt.Menu,Dialog,List,等。

事件接口可以是java.awt.event.ActionListener或java.awt.event.MouseListener。

对话框用于输入一个学生的四项信息,而四个列表用于显示所有学生的四项信息。

软件具备四个功能:

输入一个学生的信息,显示所有学生的信息,从文件中加载数据,以及把当前所有的数据保存到文件中。

而这四项功能由一个采单来选择。

二.实验设计(包括实验方案设计,实验手段的确定,实验步骤,实验过程等)

实验步骤:

(1)把myfile.data数据文件拷到C盘(因为里面有一些初始数据)。

(2)在C盘下创建文件夹:

C:

\wk\javacourse。

(3)把源代码文件StudentManagmentSystem.java拷到该文件夹之下,并编译、解释执行。

(4)按照你的想法,修改程序,体会有关标准类的使用方法。

源程序:

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

publicclassStudentInfoManagment

extendsWindowAdapter

implementsActionListener

FramemainWindow;

MenuBarmb;

Menumenu;

MenuItem[]actions;

Dialogd1,d2;

Fontfont;

java.awt.List[]lists;

Label[]titles;

/***Controlsforinputingstudent'

sinfo***/

Label[]d1Labs;

TextField[]d1TFs;

Button[]d1Btns;

Checkbox[]d1CBs;

CheckboxGroupd1Grp;

Buttond1Quit;

/***Someconstantstrings***/

finalString[]messages=

Inputstudent'

info"

Displayallstudents'

infos"

Savedataintothefile"

Loadinfofromthefile"

};

/***Hashtabletohandlestudents'

infos***/

Hashtable<

String,STUDENT>

students=newHashtable<

();

finalString[]d1Str=

NAME"

"

PHONE"

EMAIL"

finalintNumberFunctionalities=4;

publicStudentInfoManagment(){

publicvoidguiSetup()

mainWindow=

newFrame("

AStudentInformationSystem"

mainWindow.setSize(600,400);

mainWindow.setLocation(100,100);

font=newFont("

TimesNewRoman"

Font.ITALIC,18);

mainWindow.setFont(font);

mb=newMenuBar();

menu=newMenu("

Functionalities"

actions=newMenuItem[NumberFunctionalities];

for(inti=0;

i<

NumberFunctionalities;

i++)

actions[i]=newMenuItem(messages[i]);

menu.add(actions[i]);

actions[i].addActionListener(this);

mb.add(menu);

mainWindow.setLayout(newGridLayout(1,4));

titles=newLabel[4];

lists=newjava.awt.List[4];

4;

titles[i]=newLabel();

lists[i]=newjava.awt.List();

titles[0].setText("

titles[1].setText("

titles[2].setText("

titles[3].setText("

SEX"

mainWindow.add(lists[i]);

mainWindow.setMenuBar(mb);

mainWindow.addWindowListener(this);

/***Dialog01setup***/

d1=newDialog(mainWindow,messages[0],false);

d1.setBounds(100,100,400,200);

d1Labs=newLabel[3];

d1TFs=newTextField[3];

3;

d1Labs[i]=newLabel(d1Str[i]);

d1TFs[i]=newTextField(20);

d1CBs=newCheckbox[2];

d1Grp=newCheckboxGroup();

d1CBs[0]=newCheckbox("

Male"

d1Grp,true);

d1CBs[1]=newCheckbox("

Female"

d1Grp,false);

d1Btns=newButton[2];

d1Btns[0]=newButton("

SUBMIT"

d1Btns[1]=newButton("

RESET"

d1Quit=newButton("

QUIT"

d1Btns[0].addActionListener(this);

d1Btns[1].addActionListener(this);

d1Quit.addActionListener(this);

d1.setLayout(newGridLayout(6,2));

d1.add(d1Labs[i]);

d1.add(d1TFs[i]);

d1.add(d1CBs[0]);

d1.add(d1CBs[1]);

d1.add(d1Btns[0]);

d1.add(d1Btns[1]);

d1.add(d1Quit);

mainWindow.setVisible(true);

publicvoidactionPerformed(ActionEvente)

Objecto=e.getSource();

if(o==actions[0])

d1.setVisible(true);

elseif(o==d1Btns[0])

Stringname,phone,email;

booleanisFemale;

name=d1TFs[0].getText();

phone=d1TFs[1].getText();

email=d1TFs[2].getText();

isFemale=d1CBs[1].getState();

STUDENTaStudent=new

STUDENT(name,phone,email,isFe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1