实验报告7面向对象程序设计Java15级计科0140何超健Word文档格式.docx
- 文档编号:263067
- 上传时间:2023-04-28
- 格式:DOCX
- 页数:17
- 大小:59.96KB
实验报告7面向对象程序设计Java15级计科0140何超健Word文档格式.docx
《实验报告7面向对象程序设计Java15级计科0140何超健Word文档格式.docx》由会员分享,可在线阅读,更多相关《实验报告7面向对象程序设计Java15级计科0140何超健Word文档格式.docx(17页珍藏版)》请在冰点文库上搜索。
3.编写矩形类Rectangle。
area方法求矩形的面积,perimeter求矩形的周长。
编写用户程序TestRectangle,创建一个矩形,求该矩形的面积和周长并打印。
4.如图所示,写一个成绩类Score,包含英语、数学、语文、综合四科。
getTotalScore求总分,average求平均分。
编写用户程序TestScore,创建一成绩对象,并对各科赋值。
然后求总分和平均分。
5.编写学生类Student,包含学号,姓名,是否特殊考生和成绩等属性,成绩利用上题编写的Score类。
特殊考生总成绩加10分。
编写用户程序TestStudent,创建学生,修改其成绩,求其总分并打印,调用speak方法来理解重载。
实验步骤与内容(备注截图)
第一题:
packagep1;
publicclassPoint{
privatedoublex;
privatedoubley;
publicPoint()
{
this.x=0;
this.y=0;
}
publicPoint(doublex,doubley)
this.x=x;
this.y=y;
publicdoublegetX()
returnthis.x;
publicvoidsetX(doublex)
publicdoublegetY()
returnthis.y;
publicvoidsetY(doubley)
publicvoidmovePoint(doubledx,doubledy)
this.x=this.x+dx;
this.y=this.y+dy;
publicdoublegetDistance(Pointpoint)
doubleL=Math.sqrt((this.x-point.x)*(this.x-point.x)+(this.y-point.y)*(this.y-point.y));
returnL;
importjava.util.Scanner;
publicclassTestPoint{
publicstaticvoidmain(String[]args)
{
Pointp1=newPoint(0,0);
Pointp2=newPoint(0,0);
System.out.println("
输入两个点的新坐标"
);
Scannerscanner=newScanner(System.in);
System.out.print("
输入P1点坐标增加:
"
doublex=scanner.nextDouble();
doubley=scanner.nextDouble();
p1.movePoint(x,y);
输入P2点坐标增加:
doublexx=scanner.nextDouble();
doubleyy=scanner.nextDouble();
p2.movePoint(xx,yy);
P1X:
+p1.getX()+"
"
P1Y:
+p1.getY());
P2X:
+p2.getX()+"
P2Y:
+p2.getY());
P1P2点距:
+p1.getDistance(p2));
}
第二题:
packagep2;
publicclassCircle{
privatedoubler;
publicCircle()
this.r=1;
publicdoublegetR()
returnthis.r;
publicvoidsetR(doubler)
this.r=r;
publicdoublearea()
doubleS=3.14*r*r;
returnS;
publicdoubleperometer()
doubleD=3.14*2*r;
returnD;
publicclassTestCircle{
Circlecl=newCircle();
创建一个圆。
输入圆的半径:
doubler=scanner.nextDouble();
cl.setR(r);
圆的周长:
+cl.perometer());
圆的面积:
+cl.area());
第三题:
packagep3;
publicclassRectangle{
privatedoublewidth;
privatedoublelength;
publicRectangle()
this.width=1;
this.length=1;
publicdoublegetWidth()
returnthis.width;
publicvoidsetWidth(doublewidth)
this.width=width;
publicdoublegetLength()
returnthis.length;
publicvoidsetLength(doublelength)
this.length=length;
doubleS=width*length;
doubleD=2*(width+length);
publicclassTestRectangle{
Rectanglert=newRectangle();
创建一个矩形。
输入矩形的长:
doublel=scanner.nextDouble();
输入矩形的宽:
doublew=scanner.nextDouble();
rt.setLength(l);
rt.setWidth(w);
矩形的周长:
+rt.perometer());
矩形的面积:
+rt.area());
第四题:
packagep4;
publicclassScore{
privatedoublechinese;
privatedoublemath;
privatedoubleenglish;
publicScore()
this.chinese=0;
this.math=0;
this.english=0;
this.x=0;
publicScore(doublec,doublem,doublee,doublex)
this.chinese=c;
this.math=m;
this.english=e;
this.x=x;
publicdoublegetChinese()
returnthis.chinese;
publicvoidsetChinese(doublechinese)
this.chinese=chinese;
publicdoublegetMath()
returnthis.math;
publicvoidsetMath(doublemath)
this.math=math;
publicdoublegetEnglish()
returnthis.english;
publicvoidsetEnglish(doubleenglish)
this.english=english;
publicdoublegetX()
returnthis.x;
publicvoidsetX(doublex)
publicdoublegetTotalScore()
doublets=this.chinese+this.english+this.math+this.x;
returnts;
publicdoubleaverage()
doubleaver=(this.chinese+this.english+this.math+this.x)/4;
returnaver;
publicclassTestScore{
Scoresc=newScore();
创建成绩对象"
输入语文成绩:
sc.setChinese(scanner.nextDouble());
输入数学成绩:
sc.setMath(scanner.nextDouble());
输入英语成绩:
sc.setEnglish(scanner.nextDouble());
输入综合成绩:
sc.setX(scanner.nextDouble());
总分:
+sc.getTotalScore());
平均分:
+sc.average());
第五题:
packagep5;
importp4.*;
publicclassStudent{
privateStringid;
privateStringname;
privatebooleanspecial;
privateScorescore;
publicStudent()
this.id="
000"
;
this.name="
张三"
this.special=false;
publicStudent(Stringid,Stringname,booleansp,Scorescore)
this.score=score;
publicStringgetId()
returnthis.id;
publicvoidsetId(Stringid)
this.id=id;
}publicStringgetName()
returnthis.name;
publicvoidsetName(Stringname)
this.name=name;
publicbooleanisSpecial()
returnthis.special;
publicvoidsetSpecial(booleansp)
this.special=sp;
publicScoregetScore()
returnthis.score;
publicvoidsetScore(Scorescore)
publicvoidsetScore(doublechinese,doublemath,doubleenglish,doublex)
{
score.setChinese(chinese);
score.setMath(math);
score.setEnglish(english);
score.setX(x);
{booleana=true;
booleanb=false;
if(special==a)
return(score.getChinese()+score.getEnglish()+score.getMath()+score.getX()+10);
elseif(special==b)
return(score.getChinese()+score.getEnglish()+score.getMath()+score.getX());
else
return1;
publicvoidspeak()
无参。
publicvoidspeak(Stringcontent)
一参。
publicvoidspeak(Stringcontent,Stringlanguage)
二参。
publicclassTestStudent{
{Scoresc=newScore();
Stringlanguage="
Stringcontent="
Studentst=newStudent();
输入学生信息:
输入学生学号:
st.setId(scanner.nextLine());
输入学生姓名:
st.setName(scanner.nextLine());
学生是否为特殊考生:
(1为是,0为否)"
Stringi=scanner.next();
st.setSpecial(i.equals("
1"
));
Scannerinput=newScanner(System.in);
输入学生成绩:
doublechinese=input.nextDouble();
doublemath=input.nextDouble();
doubleenglish=input.nextDouble();
doublex=input.nextDouble();
Scoresco=newScore(chinese,math,english,x);
st.setScore(sco);
+st.getTotalScore());
System.out.println();
st.speak();
st.speak(content);
st.speak(content,language);
实验过程记录
记录实验中遇到的困难及解决方法:
困难:
第五题的Student类有点不会
解决方法:
问同学
实验结果记录以及与预期结果比较以及分析
结果和预期的差不多
总结以及新的体会
不熟练,大部分代码都是经过大量的时间琢磨,才勉强能弄好。
填写内容时,可把表格扩大。
实验的源程序代码(要有注释)附在表后。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 报告 面向 对象 程序设计 Java15 级计科 0140