Java程序设计实验报告.docx

上传人:b****3 文档编号:4466339 上传时间:2022-12-01 格式:DOCX 页数:44 大小:222.29KB
下载 相关 举报
Java程序设计实验报告.docx_第1页
第1页 / 共44页
Java程序设计实验报告.docx_第2页
第2页 / 共44页
Java程序设计实验报告.docx_第3页
第3页 / 共44页
Java程序设计实验报告.docx_第4页
第4页 / 共44页
Java程序设计实验报告.docx_第5页
第5页 / 共44页
点击查看更多>>
下载资源
资源描述

Java程序设计实验报告.docx

《Java程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《Java程序设计实验报告.docx(44页珍藏版)》请在冰豆网上搜索。

Java程序设计实验报告.docx

Java程序设计实验报告

 

学生实验报告册

(理工类)

 

课程名称:

JAVA程序设计实验专业班级:

M11计算机科学与技术II

学生学号:

1121117031学生姓名:

赵慧

所属院部:

龙蟠学院指导教师:

董军

2013——2014学年第二学期

 

金陵科技学院教务处制

实验报告书写要求

实验报告原则上要求学生手写,要求书写工整。

若因课程特点需打印的,标题采用四号黑体,正文采用小四号宋体,单倍行距。

纸张一律采用A4的纸张。

实验报告书写说明

实验报告中实验目的和要求、实验仪器和设备、实验内容与过程、实验结果与分析这四项内容为必需项。

教师可根据学科特点和实验具体要求增加项目。

填写注意事项

(1)细致观察,及时、准确、如实记录。

(2)准确说明,层次清晰。

(3)尽量采用专用术语来说明事物。

 

(4)外文、符号、公式要准确,应使用统一规定的名词和符号。

(5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。

实验报告批改说明

实验报告的批改要及时、认真、仔细,一律用红色笔批改。

实验报告的批改成绩采用五级记分制或百分制,按《金陵科技学院课堂教学实施细则》中作业批阅成绩评定要求执行。

实验报告装订要求

实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。

实验项目名称:

面向对象编程实验实验学时:

8

同组学生姓名:

实验地点:

1414

实验日期:

3.284.4实验成绩:

批改教师:

批改时间:

 

实验1面向对象编程实验

一、实验目的和要求

(1)理解Java概念、掌握JDK环境配置

(2)熟悉Java开发过程

(3)掌握Java面向对象编程基础:

封装、继承、多态

(4)掌握Java接口编程,理解开发模式

二、实验仪器和设备

奔腾以上个人计算机,windows操作系统。

配置好JDK环境,安装集成开发环境(Eclipse)

三、实验内容与过程

1、JDK环境配置

2、面向对象的封装性

范例:

设计一个表示学生的类,里面有学生的三项成绩:

计算机成绩、数学成绩、英语成绩。

要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息。

代码如下:

classStudent{

privateStringname;

privateintage;

privatefloatenglish;

privatefloatcomputer;

privatefloatmath;

publicStudent(){}

publicStudent(Stringn,inta,floate,floatc,floatm){

this.setName(n);

this.setAge(a);

this.setEnglish(e);

this.setComputer(c);

this.setMath(m);

}

publicfloatsum(){

returnenglish+computer+math;

}

publicfloatavg(){

returnthis.sum()/3;

}

publicfloatmax(){

floatmax=computer>math?

computer:

math;

max=max>english?

max:

english;

returnmax;

}

publicfloatmin(){

floatmin=computer

computer:

math;

min=min

min:

english;

returnmin;

}

publicStringgetInfo(){

return"学生信息:

\n"+

"\t|-姓名:

"+this.getName()+"\n"+

"\t|-年龄:

"+this.getAge()+"\n"+

"\t|-数学成绩:

"+this.getMath()+"\n"+

"\t|-英语成绩:

"+this.getEnglish()+"\n"+

"\t|-计算机成绩:

"+this.getComputer();

}

publicvoidsetName(Stringn){

name=n;

}

publicvoidsetAge(inta){

age=a;

}

publicvoidsetEnglish(floate){

english=e;

}

publicvoidsetComputer(floatc){

computer=c;

}

publicvoidsetMath(floatm){

math=m;

}

publicStringgetName(){

returnname;

}

publicintgetAge(){

returnage;

}

publicfloatgetEnglish(){

returnenglish;

}

publicfloatgetComputer(){

returncomputer;

}

publicfloatgetMath(){

returnmath;

}

}

publicclassExecDemo{

publicstaticvoidmain(Stringargs[]){

Studentstu=newStudent("张三",30,89.0f,91.0f,87.0f);

System.out.println("总分:

"+stu.sum());

System.out.println("平均分:

"+stu.avg());

System.out.println("最高分:

"+stu.max());

System.out.println("最低分:

"+stu.min());

System.out.println(stu.getInfo());

}

}

对照范例写出如下题目:

(1)编写并测试一个代表地址的Address类,包括以下的属性:

国家、省份、城市、街道、邮编。

可以从此地址类中得到一个完整的地址信息。

(2)定义并测试一个代表员工的Employee类。

它的属性包括“员工姓名”、“员工号码”、“员工基本薪水”、“员工薪水增长额”;它的方法包括“构造方法”、“获取员工姓名”、“获取员工号码”、“获取员工基本薪水”、“计算薪水增长额”及“计算增长后的工资总额”。

(3)、定义一个圆形类,可以返回圆的面积与周长。

(4)、思考设计:

一个人有一本书,一本书属于一个人。

3、面向对象的继承性

范例:

要求定义一个数组类Array,里面定义了一个整型数组,但是此整型数组属于动态分配大小,即:

所有的大小由程序指定,并在此基础上实现以下的两个子类:

反转类:

可以将数组的内容反转排列

排序类:

可以对数组进行排序的操作

classArray{

privateinttemp[]=null;//只是声明数组,但是大小未知

privateintfoot=0;//用于保存下一个的记录点

publicArray(intlen){

if(len>0){

this.temp=newint[len];//此时大小由外部决定

}else{

this.temp=newint[1];//至少开辟一个空间

}

}

publicbooleanadd(inti){//加入数据操作

if(this.foot

this.temp[this.foot]=i;//加入内容

this.foot++;//改变长度

returntrue;//加入成功返回true

}else{

returnfalse;//加入失败

}

}

publicint[]getArray(){//返回全部的数组

returnthis.temp;

}

}

classSortArrayextendsArray{

publicSortArray(intlen){

super(len);

}

publicint[]getArray(){

java.util.Arrays.sort(super.getArray());//排序操作

returnsuper.getArray();//返回的是排序后的内容

}

}

classReverseArrayextendsArray{

publicReverseArray(intlen){

super(len);

}

publicint[]getArray(){

intrt[]=newint[super.getArray().length];//根据大小开辟新数组

intcount=rt.length-1;

for(intx=0;x

rt[count]=super.getArray()[x];

count--;

}

returnrt;

}

}

publicclassArrayDemo{

publicstaticvoidmain(Stringargs[]){

ReverseArrayarr=newReverseArray(6);

System.out.println(arr.add(3));

System.out.println(arr.add(23));

System.out.println(arr.add

(1));

System.out.println(arr.add(5));

System.out.println(arr.add(6));

System.out.println(arr.add(8));

System.out.println(arr.add(11));

System.out.println(arr.add(16));

print(arr.getArray());

}

publicstaticvoidprint(inti[]){

for(intx=0;x

System.out.print(i[x]+"、");

}

}

}

对照范例写出如下题目:

(1).创建GrandFather类,其中包括

a)属性:

姓名(name),年龄(age)

b)方法getGrandFather():

显示爷爷的信息

c)构造方法:

给爷爷的姓名,年龄赋值

(2).创建Father类,继承Grandfather类

a)属性:

除了继承爷爷的属性以外,还要增加自己的属性:

“职业”(occupation)

b)构造方法:

显式调用父类的构造方法,为Father类的姓名和年龄赋初始值。

再为职业输入初始值。

c)方法getFather():

显示父亲的相关信息

(3).创建ClassMain()类,定义main()方法,构造GrandFather类的对象和Father类的对象,并分别显示详细信息。

4、面向对象多态性

范例:

计算柱体的体积。

柱体体积计算公式是:

底部面积乘以高度

柱体底部分为圆形和矩形

要求:

通过抽象类和多态实现

packagecn.jit.demo;

abstractclassBottom{//父类抽象类底部

publicabstractdoublecalculatorArea();

}

classCircleBottomextendsBottom{//圆形底

/**

*半径

*/

privatedoubleradius;

@Override

publicdoublecalculatorArea(){

returnMath.PI*radius*radius;

}

publicdoublegetRadius(){

returnradius;

}

publicvoidsetRadius(doubleradius){

this.radius=radius;

}

publicCircleBottom(doubleradius){

super();

this.radius=radius;

}

}

classSquareBottomextendsBottom{//矩形底

privatedoublesideA;

privatedoublesideB;

publicdoublegetSideA(){

returnsideA;

}

publicvoidsetSideA(doublesideA){

this.sideA=sideA;

}

publicdoublegetSideB(){

returnsideB;

}

publicvoidsetSideB(doublesideB){

this.sideB=sideB;

}

@Override

publicdoublecalculatorArea(){

returnsideA*sideB;

}

publicSquareBottom(doublesideA,doublesideB){

super();

this.sideA=sideA;

this.sideB=sideB;

}

}

classZhuTi{//柱体类,完成形状的拼装

/**

*底

*/

privateBottombottom;

/**

*高

*/

privatedoubleheight;

/**

*计算体积

*@return

*/

publicdoublecalculatorVolumn(){

returnbottom.calculatorArea()*height;

}

publicZhuTi(Bottombottom,doubleheight){

super();

this.bottom=bottom;

this.height=height;

}

publicBottomgetBottom(){

returnbottom;

}

publicvoidsetBottom(Bottombottom){

this.bottom=bottom;

}

publicdoublegetHeight(){

returnheight;

}

publicvoidsetHeight(doubleheight){

this.height=height;

}

publicvoidchangeBottom(Bottombottom){

this.bottom=bottom;

}

}

publicclassVolumnTest{//测试类

publicstaticvoidmain(String[]args){

Bottombottom=newCircleBottom(1.0);

doubleheight=1.0;

ZhuTizhuTi=newZhuTi(bottom,height);

doubleresult=zhuTi.calculatorVolumn();

System.out.println("圆柱体的体积是:

"+result);

bottom=newSquareBottom(1.0,1.0);

zhuTi.changeBottom(bottom);

result=zhuTi.calculatorVolumn();

System.out.println("立方体的体积是:

"+result);

}

}

范例:

接口和多态的应用,例如:

电脑上实现了USB接口,U盘,打印机等等也都实现了此标准。

interfaceUSB{

publicvoidstart();//开始工作

publicvoidstop();//结束工作

}

classComputer{

publicstaticvoidplugin(USBusb){

usb.start();

usb.stop();

}

};

classFlashimplementsUSB{

publicvoidstart(){

System.out.println("U盘开始工作。

");

}

publicvoidstop(){

System.out.println("U盘停止工作。

");

}

};

classPrintimplementsUSB{

publicvoidstart(){

System.out.println("打印机开始工作。

");

}

publicvoidstop(){

System.out.println("打印机停止工作。

");

}

};

publicclassInterPolDemo02{

publicstaticvoidmain(Stringargs[]){

Computer.plugin(newFlash());

Computer.plugin(newPrint());

}

};

对照范例,写出以下程序:

(1)乐器(Instrument)的标准为弹奏(play),而乐器类型分为:

钢琴(Piano)和小提琴(Violin),各种乐器的弹奏方法各不同。

编写代码实现不同乐器的弹奏。

(2)计算机模拟

四、实验结果与分析(程序运行结果及其分析)

2.(3)

classCircle{

privateintradius;

publicCircle(intr){

this.setRadius(r);

}

publicvoidsetRadius(intr){

radius=r;

}

publicintgetRadius(){

returnradius;

}

publicdoubleprintArea(){

return3.14*radius*radius;

}

publicdoubleprintChang(){

return2*3.14*radius;

}

}

publicclassTestCircle{

publicstaticvoidmain(Stringargs[])

{

Circlec1=newCircle(6);

System.out.println("面积为"+c1.printArea());

System.out.println("周长为"+c1.printChang());

}

}

3.

classGrandFather{

privateStringname;

privateintage;

publicGrandFather(Stringname,intage){

this.setName(name);

this.setAge(age);

}

publicvoidsetName(Stringname){

this.name=name;

}

publicvoidsetAge(intage){

this.age=age;

}

publicStringgetName(){

returnname;

}

publicintgetAge(){

returnage;

}

publicStringgetGrandFather(){

return"爷爷的姓名:

"+getName()+""+"年龄:

"+getAge();

}

}

classFatherextendsGrandFather{

privateStringoccupation;

publicFather(Stringname,intage,Stringoccupation){

super(name,age);

this.setOccupation(occupation);

}

publicvoidsetOccupation(Stringoccupation){

this.occupation=occupation;

}

publicStringgetOccupation(){

returnoccupation;

}

publicStringgetFather(){

return"爸爸的姓名:

"+getName()+""+"年龄:

"+getAge()+""+"职业:

"+getOccupation();

}

}

publicclassClassMain{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

GrandFathergf=newGrandFather("wuzongyao",78);

System.out.println(gf.getGrandFather());

Fatherf=newFather("wushengguang",48,"工人");

System.out.println(f.getFather());

}

}

4.

(1)

abstractclassInstrument{

abstractvoidplay();

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

当前位置:首页 > 高中教育 > 语文

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

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