实验4面向对象基础Word格式.docx

上传人:b****6 文档编号:19969218 上传时间:2023-01-13 格式:DOCX 页数:23 大小:142.46KB
下载 相关 举报
实验4面向对象基础Word格式.docx_第1页
第1页 / 共23页
实验4面向对象基础Word格式.docx_第2页
第2页 / 共23页
实验4面向对象基础Word格式.docx_第3页
第3页 / 共23页
实验4面向对象基础Word格式.docx_第4页
第4页 / 共23页
实验4面向对象基础Word格式.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

实验4面向对象基础Word格式.docx

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

实验4面向对象基础Word格式.docx

267.70000000000005

平均分:

89.23333333333335

最高分:

91.4

最低分:

87.3

学生信息:

|-学号:

100

|-姓名:

John

|-年龄:

20

|-英语:

89.0

|-计算机:

|-数学:

程序代码:

classStudentMessage{

privateintnum;

//学号

privateStringname;

//姓名

privateintage;

//年龄

privatedoublecomputerGrade;

//计算机成绩

privatedoublemathGrade;

//数学成绩

privatedoubleenglishGrade;

//英语成绩

privatedoubletotalScore;

//总分

privatedoubleaverageScore;

//平均分

privatedoublemaxScore;

//最高分

privatedoubleminScore;

//最低分

privateStringmessage;

//学生信息

publicStudentMessage(){

}

publicStudentMessage(intnum,Stringname,intage,doublecomputerGrade,doublemathGrade,doubleenglishGrade){

this.num=num;

this.name=name;

this.age=age;

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<

((computerGrade<

((mathGrade<

}

//设置器与访问器

publicvoidsetnum(intnum){

}

publicintgetnum(){

returnnum;

publicvoidsetname(Stringname){

publicStringgetname(){

returnname;

publicvoidsetage(intage){

publicintgetage(){

returnage;

publicvoidsetcomputerGrade(doublecomputerGrade){

totalScore=computerGrade+mathGrade+englishGrade;

averageScore=totalScore/3;

maxScore=(computerGrade>

?

minScore=(computerGrade<

publicdoublegetComputerGrade(){

returncomputerGrade;

publicvoidsetMathGrade(doublemathGrade){

=mathGrade)

?

englishGrade)

:

}

publicdoublegetMathGrade(){

returnmathGrade;

publicvoidsetEnglishGrade(doubleenglishGrade){

publicdoublegetEnglishGrade(){

returnenglishGrade;

publicdoublegetTotalScore(){

returntotalScore;

publicdoublegetAverageScore(){

returnaverageScore;

publicdoublegetMaxScore(){

returnmaxScore;

publicdoublegetMinScore(){

returnminScore;

publicStringgetMessage(){

message="

\t"

+totalScore+"

\n平均分:

+averageScore+"

\n最高分:

+maxScore+"

\n最低分:

+minScore+"

\n学生信息:

"

+"

\n\t|-学号:

+num+"

\n\t|-姓名:

+name+"

\n\t|-年龄:

+age+"

\n\t|-英语:

+englishGrade

+"

\n\t|-计算机:

+computerGrade+"

\n\t|-数学:

+mathGrade;

returnmessage;

//测试类

publicclassTestStudentMessage{

publicstaticvoidmain(String[]args){

StudentMessages1=newStudentMessage(100,"

John"

20,87.3,91.4,89);

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

}

}

运行结果贴图:

(二)

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

程序功能:

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

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

publicclassCar{

privateStringownerName;

//车主姓名

privatefloatcurSpeed;

//当前车速

privatefloatcurDirInDegree;

//当前方向盘转向角度

publicCar(StringownerName){

super();

publicCar(StringownerName,floatspeed,floatdirInDegree){

this(ownerName);

//调用上面那个构造函数为ownerName赋值

this.ownerName=ownerName;

this.curSpeed=speed;

this.curDirInDegree=dirInDegree;

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

returnownerName;

publicvoidsetOwnerName(StringownerName){

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

returncurDirInDegree;

publicvoidsetCurDirInDegree(floatCurDirInDegree){

this.curDirInDegree=curDirInDegree;

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

returncurSpeed;

publicvoidsetChangeSpeed(floatchangeSpeed){//提供改变当前的车速

this.curSpeed=changeSpeed;

publicvoidstop(){//提供停车

curSpeed=0;

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

主方法程序代码:

publicclassTestCar{

publicstaticvoidmain(String[]args){

Carc1=newCar("

Car_One"

);

Carc2=newCar("

Car_Two"

100,60);

System.out.println("

当前车车主为:

+c1.getOwnerName()+"

\n车速为:

+c1.getCurSpeed()+"

\n方向转度为:

+c1.getCurDirInDegree());

c1.changeSpeed(200);

\n车主为:

的车变速后,车速为:

+c2.getOwnerName()+"

+c2.getCurSpeed()+"

+c2.getCurDirInDegree());

c2.stop();

的车停车后,车速为:

+c2.getCurSpeed());

}

运行结果贴图:

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

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

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

publicstaticvoidmain(String[]args){

Carcar=newCar("

成龙"

200f,25f);

System.out.println("

车主姓名:

+car.getOwnerName());

现在车速:

+car.getCurSpeed());

当前转向角度:

+car.getCurDirInDegree());

car.setChangeSpeed(80);

当前车速:

car.stop();

现在停车:

编译并运行,结果如图所示。

(三)(账户类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美元,然后打印余额、月利息以及这个账户的开户日期。

importjava.util.Date;

publicclassAccount{

privateintid=0;

privatedoublebalance=0;

privatedoubleannualInterestRate=0;

privateDatedateCreated;

publicAccount(){

publicAccount(intid,doublebalance,doubleannualInterestRate){

this.id=id;

this.balance=balance;

this.annualInterestRate=annualInterestRate;

publicintgetId(){

returnid;

publicvoidsetId(intid){

publicvoidchangeId(intcid){

this.id=cid;

publicdoublegetBalance(){

returnbalance;

publicvoidsetBalance(doublebalance){

publicvoidwithDraw(doublecbalance){

this.balance=balance-cbalance;

publicvoiddeposit(doublecbalance){

this.balance=balance+cbalance;

publicdoublegetannualInterestRat(){

returnannualInterestRate;

publicvoidsetannualInterestRate(doubleannualInterestRate){

publicdoublegetMonthlyInterestRate(){

publicDategetDate(){

dateCreated=newDate();

returndateCreated;

Accounts=newAccount(1122,20000,0.045);

你的id是:

+s.id);

s.deposit(3000);

你的存款余额:

+s.balance);

s.withDraw(2500);

s.getMonthlyInterestRate();

当前利率为"

+s.annualInterestRate);

你的建户日期为:

+s.getDate());

(三)(风扇类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方法显示这些对象。

publicclassFan{

finalstaticintSLOW=1;

finalstaticintMEDIUM=2;

finalstaticintFAST=3;

privateintspeed=SLOW;

privatebooleanon=false;

privatedoubleradius=5;

privateStringcolor="

blue"

;

publicFan(){

publicFan(intspeed,booleanon,doubleradius,Stringco

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

当前位置:首页 > 工作范文 > 其它

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

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