类与对象继承与接口.docx

上传人:b****3 文档编号:5436415 上传时间:2023-05-08 格式:DOCX 页数:20 大小:46.61KB
下载 相关 举报
类与对象继承与接口.docx_第1页
第1页 / 共20页
类与对象继承与接口.docx_第2页
第2页 / 共20页
类与对象继承与接口.docx_第3页
第3页 / 共20页
类与对象继承与接口.docx_第4页
第4页 / 共20页
类与对象继承与接口.docx_第5页
第5页 / 共20页
类与对象继承与接口.docx_第6页
第6页 / 共20页
类与对象继承与接口.docx_第7页
第7页 / 共20页
类与对象继承与接口.docx_第8页
第8页 / 共20页
类与对象继承与接口.docx_第9页
第9页 / 共20页
类与对象继承与接口.docx_第10页
第10页 / 共20页
类与对象继承与接口.docx_第11页
第11页 / 共20页
类与对象继承与接口.docx_第12页
第12页 / 共20页
类与对象继承与接口.docx_第13页
第13页 / 共20页
类与对象继承与接口.docx_第14页
第14页 / 共20页
类与对象继承与接口.docx_第15页
第15页 / 共20页
类与对象继承与接口.docx_第16页
第16页 / 共20页
类与对象继承与接口.docx_第17页
第17页 / 共20页
类与对象继承与接口.docx_第18页
第18页 / 共20页
类与对象继承与接口.docx_第19页
第19页 / 共20页
类与对象继承与接口.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

类与对象继承与接口.docx

《类与对象继承与接口.docx》由会员分享,可在线阅读,更多相关《类与对象继承与接口.docx(20页珍藏版)》请在冰点文库上搜索。

类与对象继承与接口.docx

类与对象继承与接口

实验题目

类与对象、继承与接口

小组合作

姓名

班级

学号

一、实验目的:

∙熟悉Java面向对象程序设计的基本思想。

∙掌握类与对象的定义及使用方法。

∙掌握package语句与import语句的用法。

二、实验要求:

∙复习理论教学中所学的内容。

∙认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。

∙认真总结实验并书写实验报告。

三、实验环境:

安装有JDK1.6,Eclipse的计算机。

四、实验内容与步骤:

(一)类与对象

1、三角形、梯形和圆形的类封装:

分别定义三角形、梯形和圆形类,要求每个类中包含构造方法、求周长及面积的方法。

最后在主方法中为三角形、梯形和圆形类创建对象并打印输出各种图形的周长及面积。

该程序的模板代码如下:

请将其补充完整并调试运行。

//AreaAndLength.java

classTrangle

{

doublesideA,sideB,sideC,area,length;

booleanboo;

publicTrangle(doublea,doubleb,doublec)

{

sideA=a;sideB=b;sideC=c;//参数a,b,c分别赋值给sideA,sideB,sideC

if(a+b>=c&&a+c>=b&&b+c>=a)//a,b,c构成三角形的条件表达式

{

boo=true;//给boo赋值。

}

else

{

boo=false;//给boo赋值。

}

}

doublegetLength()

{

length=sideA+sideB+sideC;//方法体,要求计算出length的值并返回

returnlength;

}

publicdoublegetArea()

{

if(boo)

{

doublep=(sideA+sideB+sideC)/2.0;

area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));

returnarea;

}

else

{

System.out.println("不是一个三角形,不能计算面积");

return0;

}

}

publicvoidsetABC(doublea,doubleb,doublec)

{

sideA=a;sideB=b;sideC=c;//参数a,b,c分别赋值给sideA,sideB,sideC

if(a+b>=c&&a+c>=b&&b+c>=a)//a,b,c构成三角形的条件表达式

{

boo=true;//给boo赋值。

}

else

{

boo=false;//给boo赋值。

}

}

}

classLader

{

doubleabove,bottom,height,area;

Lader(doublea,doubleb,doubleh)

{

above=a;bottom=b;height=h;//方法体,将参数a,b,h分别赋值给above,bottom,height

}

doublegetArea()

{

area=(above+bottom)*height/2;//方法体,要求计算出area返回

returnarea;

}

}

classCircle

{

doubleradius,area;

Circle(doubler)

{

radius=r;//方法体

}

doublegetArea()

{

returnarea=3.14*radius*radius;

}

doublegetLength()

{

doublelength=2*3.14*radius;//getArea方法体的代码,要求计算出length返回

returnlength;

}

voidsetRadius(doublenewRadius)

{

radius=newRadius;

}

doublegetRadius()

{

returnradius;

}

}

publicclassAreaAndLength

{

publicstaticvoidmain(Stringargs[])

{

doublelength,area;

Circlecircle=null;

Trangletrangle;

Laderlader;

circle=newCircle(10);//创建对象circle

trangle=newTrangle(3,4,5);//创建对象trangle。

lader=newLader(3,4,10);//创建对象lader

length=circle.getLength();//circle调用方法返回周长并赋值给length

System.out.println("圆的周长:

"+length);

area=circle.getArea();//circle调用方法返回面积并赋值给area

System.out.println("圆的面积:

"+area);

length=trangle.getLength();//trangle调用方法返回周长并赋值给length

System.out.println("三角形的周长:

"+length);

area=trangle.getArea();//trangle调用方法返回面积并赋值给area

System.out.println("三角形的面积:

"+area);

area=lader.getArea();//lader调用方法返回面积并赋值给area

System.out.println("梯形的面积:

"+area);

trangle.setABC(12,34,1);//trangle调用方法设置三个边,要求将三个边修改为12,34,1。

area=trangle.getArea();//trangle调用方法返回面积并赋值给area

System.out.println("三角形的面积:

"+area);

length=trangle.getLength();//trangle调用方法返回周长并赋值给length

System.out.println("三角形的周长:

"+length);

}

}

运行结果:

2、学习实例成员与类成员的区别:

按照下面模板代码中的注释信息将该程序补充完整并调试运行。

//Example.java

packagetom.jiafei;

classA

{

floata;//声明一个float型实例变量a

staticfloatb;//声明一个float型类变量b,即static变量b

voidsetA(floata)

{

this.a=a;//将参数a的值赋值给成员变量a

}

voidsetB(floatb)

{

this.b=b;//将参数b的值赋值给成员变量b

}

floatgetA()

{

returna;

}

floatgetB()

{

returnb;

}

voidinputA()

{

System.out.println(a);

}

staticvoidinputB()

{

System.out.println(b);

}

}

publicclassExample

{

publicstaticvoidmain(Stringargs[])

{

A.b=100;//通过类名操作类变量b,并赋值100

A.inputB();//通过类名调用方法inputB()

Acat=newA();

Adog=newA();

cat.setA(200);//cat象调用方法setA(inta)将cat的成员a的值设置为200

cat.setB(400);//cat调用方法setB(intb)将cat的成员b的值设置为400

dog.setA(150);//dog象调用方法setA(inta)将dog的成员a的值设置为150

dog.setB(300);//dog调用方法setB(intb)将dog的成员b的值设置为300

cat.inputA();//cat调用inputA()。

cat.inputB();//cat调用inputB()。

dog.inputA();//dog调用inputA()。

dog.inputB();//dog调用inputB()。

}

}

运行结果:

3、使用package语句与import语句:

上机调试下面程序,学会package与import语句的用法。

//SquareEquation.java

packagetom.jiafei;

publicclassSquareEquation

{

doublea,b,c;

doubleroot1,root2;

booleanboo;

publicSquareEquation(doublea,doubleb,doublec)

{

this.a=a;

this.b=b;

this.c=c;

if(a!

=0)

{

boo=true;

}

else

{

boo=false;

}

}

publicvoidgetRoots()

{

if(boo)

{

System.out.println("是一元2次方程");

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

if(disk>=0)

{

root1=(-b+Math.sqrt(disk))/(2*a);

root2=(-b-Math.sqrt(disk))/(2*a);

System.out.printf("方程的根:

%f,%f\n",root1,root2);

}

else

{

System.out.printf("方程没有实根\n");

}

}

else

{

System.out.println("不是一元2次方程");

}

}

publicvoidsetCoefficient(doublea,doubleb,doublec)

{

this.a=a;

this.b=b;

this.c=c;

if(a!

=0)

{

boo=true;

}

else

{

boo=false;

}

}

}

//SunRise.java

packagetom.jiafei;

classSunRise

{

publicstaticvoidmain(Stringargs[])

{

SquareEquationequation=newSquareEquation(4,5,1);

equation.getRoots();

equation.setCoefficient(-3,4,5);

equation.getRoots();

}

}

运行结果:

(二)继承与接口

1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。

最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。

该程序的模板代码如下:

请将其补充完整并调试运行。

package第二部分;

classPeople

{

protecteddoubleweight,height;

publicvoidspeakHello()

{

System.out.println("yayawawa");

}

publicvoidaverageHeight()

{

height=173;

System.out.println("averageheight:

"+height);

}

publicvoidaverageWeight()

{

weight=70;

System.out.println("averageweight:

"+weight);

}

}

package第二部分;

classChinaPeopleextendsPeople

{

publicvoidspeakHello()

{

System.out.println("你好,吃饭了吗?

");

}//重写publicvoidspeakHello()方法,

//要求输出类似“你好,吃了吗”这样的汉语信息

publicvoidaverageHeight()

{

height=173;

System.out.println("中国人的平均身高:

"+height+"厘米");

}//重写publicvoidaverageHeight()方法,要求

//输出类似“中国人的平均身高:

168.78厘米”这样的汉语信息

publicvoidaverageWeight()

{

weight=67.34;

System.out.println("中国人的平均体重:

"+weight+"公斤");

}//重写publicvoidaverageWeight()方法,

//要求输出类似“中国人的平均体重:

65公斤”这样的汉语信息

publicvoidchinaGongfu()

{

System.out.println("坐如钟,站如松,睡如弓");

}//输出中国武术的信息,例如:

"坐如钟,站如松,睡如弓"等

}

package第二部分;

classAmericanPeopleextendsPeople

{

publicvoidspeakHello()

{

System.out.println("HowdoYoudo");

}//重写publicvoidspeakHello()方法,要求

//输出类似“Howdoyoudo”这样的英语信息。

publicvoidaverageHeight()

{

height=188;

System.out.println("AmerianAverageheight:

"+height+"cm");

}//重写publicvoidaverageHeight()方法

publicvoidaverageWeight()

{

weight=80.23;

System.out.println("AmerianAverageweight:

"+weight+"kg");

}//重写publicvoidaverageWeight()方法

publicvoidamericanBoxing()

{

System.out.println("直拳、钩拳");//输出拳击的信息,例如,“直拳”、“钩拳”等

}

}

package第二部分;

classBeijingPeopleextendsChinaPeople

{

publicvoidspeakHello()

{

System.out.println("您好");

}//重写publicvoidspeakHello()方法,

//要求输出类似“您好”这样的汉语信息

publicvoidaverageHeight()

{

height=16;

System.out.println("北京人的平均身高:

"+height+"厘米");

}//重写publicvoidaverageHeight()方法

publicvoidaverageWeight()

{

weight=6;

System.out.println("北京人的平均体重:

"+weight+"公斤");

}//重写publicvoidaverageWeight()方法

publicvoidbeijingOpera()

{

System.out.println("京剧术语");//输出京剧的信息

}

}

package第二部分;

publicclassExample

{

publicstaticvoidmain(Stringargs[])

{

ChinaPeoplechinaPeople=newChinaPeople();

AmericanPeopleamericanPeople=newAmericanPeople();

BeijingPeoplebeijingPeople=newBeijingPeople();

chinaPeople.speakHello();

americanPeople.speakHello();

beijingPeople.speakHello();

chinaPeople.averageHeight();

americanPeople.averageHeight();

beijingPeople.averageHeight();

chinaPeople.averageWeight();

americanPeople.averageWeight();

beijingPeople.averageWeight();

chinaPeople.chinaGongfu();

americanPeople.americanBoxing();

beijingPeople.beijingOpera();

beijingPeople.chinaGongfu();

}

}

运行结果:

2、读懂下面模板代码,按要求补充程序并调试运行。

掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。

package第三部分;

abstractclassEmployee

{

publicabstractdoubleearnings();

}

package第三部分;

classYearWorkerextendsEmployee

{

publicdoubleearnings()

{

return50000.456;

}//重写earnings()方法

}

package第三部分;

classMonthWorkerextendsEmployee

{

publicdoubleearnings()

{

return12*2300;

}//重写earnings()方法。

}

package第三部分;

classWeekWorkerextendsEmployee

{

publicdoubleearnings()

{

return52*500;

}//重写earnings()方法。

}

package第三部分;

classCompany

{

Employee[]employee;

doublesalaries=0;

Company(Employee[]employee)

{

this.employee=employee;

}

publicdoublesalariesPay()

{

salaries=0;

for(inti=0;i

{

salaries=salaries+employee[i].earnings();

}//计算salaries。

returnsalaries;

}

}

package第三部分;

publicclassHardWork

{

publicstaticvoidmain(Stringargs[])

{

Employee[]employee=newEmployee[20];

for(inti=0;i

{

if(i%3==0)

employee[i]=newWeekWorker();

elseif(i%3==1)

employee[i]=newMonthWorker();

elseif(i%3==2)

employee[i]=newYearWorker();

}

Companycompany=newCompany(employee);

System.out.println("公司年工资总额:

"+company.salariesPay());

}

}

运行结果:

3、读懂下面模板代码,按要求补充程序并调试运行。

掌握接口的定义及其实现方法,学习接口回调的运用方法。

package第四部分;

interfaceComputerWeight

{

publicdoublecomputeWeight();

}

classTelevisionimplementsComputerWeight

{

publicdoublecomputeWeight()

{

return45.5;

}//实现computeWeight()方法。

}

package第四部分;

classComputerimplementsComputerWeight

{

publicdoublecomputeWeight()

{

return65.5;

}//实现computeWeight()方法。

}

package第四部分;

classWashMachineimplementsComputerWeight

{

publicdoublecomputeWeight()

{

return145;

}//实现computeWeight()方法。

}

package第四部分;

classCar

{ComputerWeight[]goods;

doubletotalWeights=0;

Car(ComputerWeight[]goods)

{

this.goods=goods;

}

publicdoublegetTotalWeights()

{

totalWeights=0;

for(intk=0;k

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

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2