实验4面向对象基础.docx

上传人:b****6 文档编号:7718658 上传时间:2023-05-12 格式:DOCX 页数:27 大小:189.80KB
下载 相关 举报
实验4面向对象基础.docx_第1页
第1页 / 共27页
实验4面向对象基础.docx_第2页
第2页 / 共27页
实验4面向对象基础.docx_第3页
第3页 / 共27页
实验4面向对象基础.docx_第4页
第4页 / 共27页
实验4面向对象基础.docx_第5页
第5页 / 共27页
实验4面向对象基础.docx_第6页
第6页 / 共27页
实验4面向对象基础.docx_第7页
第7页 / 共27页
实验4面向对象基础.docx_第8页
第8页 / 共27页
实验4面向对象基础.docx_第9页
第9页 / 共27页
实验4面向对象基础.docx_第10页
第10页 / 共27页
实验4面向对象基础.docx_第11页
第11页 / 共27页
实验4面向对象基础.docx_第12页
第12页 / 共27页
实验4面向对象基础.docx_第13页
第13页 / 共27页
实验4面向对象基础.docx_第14页
第14页 / 共27页
实验4面向对象基础.docx_第15页
第15页 / 共27页
实验4面向对象基础.docx_第16页
第16页 / 共27页
实验4面向对象基础.docx_第17页
第17页 / 共27页
实验4面向对象基础.docx_第18页
第18页 / 共27页
实验4面向对象基础.docx_第19页
第19页 / 共27页
实验4面向对象基础.docx_第20页
第20页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

实验4面向对象基础.docx

《实验4面向对象基础.docx》由会员分享,可在线阅读,更多相关《实验4面向对象基础.docx(27页珍藏版)》请在冰点文库上搜索。

实验4面向对象基础.docx

实验4面向对象基础

实验报告

姓名

学号

专业班级

课程名称

Java实验

实验日期

成绩

指导教师

批改日期

实验名称

实验4面向对象基础

一.实验目的:

(1)掌握类的定义、类的实例化方法;

(2)掌握类中的静态变量和静态方法的使用;

(3)掌握使用类的构造方法对对象的数据域进行初始化;

(4)掌握getter和setter的使用;

二.实验内容

(1)现在要求设计一个表示学生的类,里面有学生的姓名、学号、年龄,还要有三项成绩:

计算机成绩、数学成绩、英语成绩,要求可以求总分、平均分、最高、最低分,并且可以输出一个学生的完整信息,请设计此类。

提示:

程序开发步骤

根据需求定义出所要的类;

根据题目中的要求规划出类的属性;

所有的属性必须封装:

private;

所有的属性必须通过getter和setter访问;

如果需要增加构造方法,为属性赋初值,适当采用构造方法重载;

所有的信息不要类中直接输出,而是交给调用处输出,即在该类中尽量不出现System.out.println()语句。

例如在调用类中输出如下结果:

总分:

267.70000000000005

平均分:

89.23333333333335

最高分:

91.4

最低分:

87.3

学生信息:

|-学号:

100

|-姓名:

John

|-年龄:

20

|-英语:

89.0

|-计算机:

87.3

|-数学:

91.4

程序代码:

classStudent{

privateintsNo;//学号

privateStringsName;//姓名

privateintsAge;//年龄

privatedoublecomputerGrade;//计算机成绩

privatedoublemathGrade;//数学成绩

privatedoubleenglishGrade;//英语成绩

privatedoubletotalScore;//总分

privatedoubleaverageScore;//平均分

privatedoublemaxScore;//最高分

privatedoubleminScore;//最低分

privateStringmessage;//学生信息

//构造方法

publicStudent(){

}

publicStudent(intsNo,StringsName,intsAge,doublecomputerGrade,doublemathGrade,doubleenglishGrade){

this.sNo=sNo;

this.sName=sName;

this.sAge=sAge;

puterGrade=computerGrade;

this.mathGrade=mathGrade;

this.englishGrade=englishGrade;

totalScore=computerGrade+mathGrade+englishGrade;

averageScore=totalScore/3;

maxScore=(computerGrade>=mathGrade)

?

((computerGrade>=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade>=englishGrade)?

mathGrade:

englishGrade);

minScore=(computerGrade<=mathGrade)

?

((computerGrade<=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade<=englishGrade)?

mathGrade:

englishGrade);

}

//设置器与访问器

publicvoidsetSNo(intsNo){

this.sNo=sNo;

}

publicintgetSNo(){

returnsNo;

}

publicvoidsetSName(StringsName){

this.sName=sName;

}

publicStringgetSName(){

returnsName;

}

publicvoidsetSAge(intsAge){

this.sAge=sAge;

}

publicintgetSAge(){

returnsAge;

}

publicvoidsetComputerGrade(doublecomputerGrade){

puterGrade=computerGrade;

totalScore=computerGrade+mathGrade+englishGrade;

averageScore=totalScore/3;

maxScore=(computerGrade>=mathGrade)

?

((computerGrade>=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade>=englishGrade)?

mathGrade:

englishGrade);

minScore=(computerGrade<=mathGrade)

?

((computerGrade<=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade<=englishGrade)?

mathGrade:

englishGrade);

}

publicdoublegetComputerGrade(){

returncomputerGrade;

}

publicvoidsetMathGrade(doublemathGrade){

this.mathGrade=mathGrade;

totalScore=computerGrade+mathGrade+englishGrade;

averageScore=totalScore/3;

maxScore=(computerGrade>=mathGrade)

?

((computerGrade>=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade>=englishGrade)?

mathGrade:

englishGrade);

minScore=(computerGrade<=mathGrade)

?

((computerGrade<=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade<=englishGrade)?

mathGrade:

englishGrade);

}

publicdoublegetMathGrade(){

returnmathGrade;

}

publicvoidsetEnglishGrade(doubleenglishGrade){

this.englishGrade=englishGrade;

totalScore=computerGrade+mathGrade+englishGrade;

averageScore=totalScore/3;

maxScore=(computerGrade>=mathGrade)

?

((computerGrade>=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade>=englishGrade)?

mathGrade:

englishGrade);

minScore=(computerGrade<=mathGrade)

?

((computerGrade<=englishGrade)?

computerGrade:

englishGrade)

:

((mathGrade<=englishGrade)?

mathGrade:

englishGrade);

}

publicdoublegetEnglishGrade(){

returnenglishGrade;

}

publicdoublegetTotalScore(){

returntotalScore;

}

publicdoublegetAverageScore(){

returnaverageScore;

}

publicdoublegetMaxScore(){

returnmaxScore;

}

publicdoublegetMinScore(){

returnminScore;

}

publicStringgetMessage(){

message="总分:

\t"+totalScore+"\n平均分:

\t"+averageScore+"\n最高分:

\t"

+maxScore+"\n最低分:

\t"+minScore+"\n学生信息:

"+"\n\t|-学号:

"

+sNo+"\n\t|-姓名:

"+sName+"\n\t|-年龄:

"+sAge+"\n\t|-英语:

"+englishGrade

+"\n\t|-计算机:

"+computerGrade+"\n\t|-数学:

"+mathGrade;

returnmessage;

}

}

//测试类

publicclassTestStudent{

publicstaticvoidmain(String[]args){

Students1=newStudent(100,"John",20,87.3,91.4,89);

Students2=newStudent();

System.out.println(s1.getMessage());

System.out.println("s2的初始信息:

\n"+s2.getMessage());

//重设s2信息

s2.setSName("hello");

s2.setComputerGrade(80);

s2.setMathGrade(90);

s2.setEnglishGrade(70);

System.out.println("重设后s2的信息为:

\n"+s2.getMessage());

}

}

运行结果贴图:

图1

(二)

1、设计类来描述真实客观世界中的事物,使用类的成员变量来表示事物的属性和状态,使用类的成员方法来提供对成员变量的访问或修改

程序功能:

设计一个用来描述汽车的类,使用类的非静态成员变量来表示汽车的车主姓名、当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的速率和停车两个操作。

程序源代码如下,补全横线上的程序代码。

publicclassCar{

privateStringownerName;//车主姓名

privatefloatcurSpeed;//当前车速

privatefloatcurDirInDegree;//当前方向盘转向角度

publicCar(StringownerName){

this.ownerName=ownerName;

}

publicCar(StringownerName,floatspeed,floatdirInDegree){

Car(ownerName);//调用上面那个构造函数为ownerName赋值

curSpeed=speed;

curDirInDegree=dirInDegree;

}

publicStringgetOwnerName(){//提供对车主姓名的访问

returnownerName;

}

publicfloatgetCurDirInDegree(){//提供对当前方向盘转向角度的访问

returncurDirInDegree;

}

publicfloatgetCurSpeed(){//提供对当前车速的访问

returncurSpeed;

}

publicvoidchangeSpeed(floatcurSpeed){//提供改变当前的车速

this.curSpeed=curSpeed;

}

publicvoidstop(){//提供停车

curSpeed=0;

}

}

编写一个主方法,编译并测试源程序。

主方法程序代码:

publicclassTestCar{

publicstaticvoidmain(String[]args){

Carc1=newCar("Car_One");

Carc2=newCar("Car_Two",100,60);

System.out.println("当前车车主为:

\t"+c1.getOwnerName()+"\n车速为:

\t"

+c1.getCurSpeed()+"\n方向转度为:

\t"+c1.getCurDirInDegree());

c1.changeSpeed(200);

System.out.println("\n车主为:

\t"+c1.getOwnerName()+"的车变速后,车速为:

"

+c1.getCurSpeed()+"\n方向转度为:

"+c1.getCurDirInDegree());

System.out.println("当前车车主为:

\t"+c2.getOwnerName()+"\n车速为:

\t"

+c2.getCurSpeed()+"\n方向转度为:

\t"+c2.getCurDirInDegree());

c2.stop();

System.out.println("\n车主为:

"+c2.getOwnerName()+"的车停车后,车速为:

"+c2.getCurSpeed());

}

}

运行结果贴图:

 

图2

2、创建类的对象,使用对象的方法(类的非静态方法)来访问或修改对象的变量(类的非静态变量)

创建类Car的对象,在调用类的构造函数时指定对象的变量的初始值,以后再使用对象的方法来访问或修改对象的变量的值。

程序代码如下(需要添加代码,要求运行结果如下图所示。

主方法程序代码:

publicclassCar1{

publicstaticvoidmain(String[]args){

Carcar=newCar("成龙",200,25);

System.out.println("车主姓名:

\t"+car.getOwnerName()+"\n当前车速:

\t"

+car.getCurSpeed()+"\n当前转向角度:

\t"+car.getCurDirInDegree());

car.changeSpeed(80);

System.out.println("在调用changeSpeed(80)后,车速变为:

:

\t"+car.getCurSpeed());

car.stop();

System.out.println("在调用stop()后,车速变为:

:

\t"+car.getCurSpeed());

}

}

运行结果贴图:

图3

(三)(账户类Account)设计一个名为Account的类,它包括:

●一个名为id的int类型私有账户数据域(默认值为0)。

●一个名为balance的double类型私有账户数据域(默认值为0)。

●一个名为annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。

假设所有的账户都有相同的利率。

●一个名为dateCreated的Date类型私有数据域存储账户的开户日期。

●一个能创建默认账户的无参构造方法。

●一个能创建带特定id和初始余额的账户的构造方法。

●id、balance和annualInterestRate的访问器和修改器。

●dateCreated的访问器。

●一个名为getMonthlyInterestRate()的方法返回月利率。

●一个名为withDraw的方法从账户提取特定数额。

●一个名为deposit的方法向账户存储特定数额。

实现这个类。

编写一个测试程序,创建一个账户ID为1122、余额为20000美元、年利率为4.5%的Account对象。

使用withdraw方法取款2500美元,使用deposit方法存款3000美元,然后打印余额、月利息以及这个账户的开户日期。

程序代码:

//Account类

importjava.util.Date;

classAccount{

privateintid;//帐号

privatedoublebalance;//余额

privatestaticdoubleannualIntereatRate;//年利率

privateDatedateCreated=newDate();//开户日期

//构造方法

publicAccount(){

}

publicAccount(intid,doublebalance){

this.id=id;

this.balance=balance;

}

//id、balance和annualInterestRate的访问器和修改器,dateCreated的访问器。

publicvoidsetId(intid){

this.id=id;

}

publicintgetId(){

returnid;

}

publicvoidsetBalance(doublebalance){

this.balance=balance;

}

publicdoublegetBalance(){

returnbalance;

}

publicstaticvoidsetAnnualIntereatRate(doublerate){

annualIntereatRate=rate;

}

publicstaticdoublegetAnnualIntereatRate(){

returnannualIntereatRate;

}

publicDategetDateCreated(){

returndateCreated;

}

//其他方法

publicstaticdoublegetMonthlyInterestRate(){

return(annualIntereatRate/12);

}

publicvoidwithDraw(doublenum){

balance=balance-num;

}

publicvoiddeposit(doublenum){

balance=balance+num;

}

}

//测试类

publicclassTestAccount{

publicstaticvoidmain(String[]args){

Account.setAnnualIntereatRate(4.5);

Accounta1=newAccount(1122,20000);

System.out.println("\n帐号:

\t"+a1.getId()+"\n余额:

\t"+a1.getBalance()+

"\n月利息:

\t"+Account.getMonthlyInterestRate()+"%\n开户日期:

\t"

+a1.getDateCreated());

a1.withDraw(2500);

a1.deposit(3000);

System.out.println("————————存取款后——————\n帐号:

\t"+a1.getId()+"\n余额:

\t"

+a1.getBalance()+"\n月利息:

\t"+Account.getMonthlyInterestRate()

+"%\n开户日期:

\t"+a1.getDateCreated());

}

}

运行结果贴图:

图4

(三)(风扇类Fan)设计一个名为Fan的类来表示一个风扇。

这个类包括:

●三个名为SLOW、MEDIUM、FAST而值是1、2和3的常量表示风扇的速度。

●一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。

●一个名为on的boolean类型的私有数据域表示风扇是否打开(默认值为false)。

●一个名为radius的double类型的私有数据域表示风扇的半径(默认值为5)。

●一个名为color的String类型的数据域表示风扇的颜色(默认值为blue)。

●这四个数据域的访问器和修改器。

●一个创建默认风扇的无参构造方法。

●一个名为toString()的方法返回描述风扇的字符串。

如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。

如果风扇没有打开,该方法就返回一个由“fanisoff”和风扇颜色及半径组合成的字符串。

实现这个类。

编写一个测试程序,创建两个fan对象。

将第一个对象设置为最大速度、半径为10、颜色为yellow、状态为打开。

将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。

通过调用它们的toString方法显示这些对象。

程序代码:

classFan{

publicstaticfinalintSLOW=1;

publicstaticfinalintMEDIUM=2;

publicstaticfinalintFAST=3;

//状态属性

privateintspeed;

privatebooleanon;

privatedoubleradius;

privateStringcolor;

//构造方法

publicFan(){

speed=SLOW;

radius=5;

color="blue";

}

//访问器与修改器

publicvoidsetSpeed(intspeed){

if(speed>=1||speed<=3)

this.speed=speed;

}

publicintgetSpeed(){

returnspeed;

}

publicvoidsetOn(booleanon){

this

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

当前位置:首页 > 农林牧渔 > 林学

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

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