实验报告7面向对象程序设计Java15级计科0140何超健.docx
《实验报告7面向对象程序设计Java15级计科0140何超健.docx》由会员分享,可在线阅读,更多相关《实验报告7面向对象程序设计Java15级计科0140何超健.docx(17页珍藏版)》请在冰点文库上搜索。
实验报告7面向对象程序设计Java15级计科0140何超健
福建工程学院软件学院
实验报告
2016–2017学年度第一学期任课教师:
陈丽晖
课程名称
面向对象程序设计(Java)
班级
计科(本)
1501
座号
40
姓名
何超健
实验题目
Java语言概述及环境配置
实验时间
实验开始日期:
2016.9.5
实验提交日期:
2016.9.16
实验目的、要求
1.如图所示,编写类Point。
Point包含两个成员变量x、y分别表示x和y坐标。
movePoint方法实现点的位置的移动(dx,dy表示相对位移)。
getDistance求该点与另外一个点的距离。
编写用户程序TestPoint,创建两个Point对象p1,p2,分别调用movePoint()方法后,打印p1和p2的坐标,然后求p1和p2之间的距离。
2.如图所示,编写圆类Circle。
area方法求圆的面积,perimeter求圆的周长。
编写用户程序TestCircle,创建一个圆,求圆的面积和周长并打印出来。
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)
{
this.x=x;
}
publicdoublegetY()
{
returnthis.y;
}
publicvoidsetY(doubley)
{
this.y=y;
}
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;
}
}
packagep1;
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);
System.out.print("输入P2点坐标增加:
");
doublexx=scanner.nextDouble();
doubleyy=scanner.nextDouble();
p2.movePoint(xx,yy);
System.out.print("P1X:
"+p1.getX()+"");
System.out.println("P1Y:
"+p1.getY());
System.out.print("P2X:
"+p2.getX()+"");
System.out.println("P2Y:
"+p2.getY());
System.out.print("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;
}
}
packagep2;
importjava.util.Scanner;
publicclassTestCircle{
publicstaticvoidmain(String[]args)
{
Circlecl=newCircle();
System.out.println("创建一个圆。
");
System.out.print("输入圆的半径:
");
Scannerscanner=newScanner(System.in);
doubler=scanner.nextDouble();
cl.setR(r);
System.out.println("圆的周长:
"+cl.perometer());
System.out.println("圆的面积:
"+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;
}
publicdoublearea()
{
doubleS=width*length;
returnS;
}
publicdoubleperometer()
{
doubleD=2*(width+length);
returnD;
}
}
packagep3;
importjava.util.Scanner;
publicclassTestRectangle{
publicstaticvoidmain(String[]args)
{
Rectanglert=newRectangle();
System.out.println("创建一个矩形。
");
Scannerscanner=newScanner(System.in);
System.out.print("输入矩形的长:
");
doublel=scanner.nextDouble();
System.out.print("输入矩形的宽:
");
doublew=scanner.nextDouble();
rt.setLength(l);
rt.setWidth(w);
System.out.println("矩形的周长:
"+rt.perometer());
System.out.println("矩形的面积:
"+rt.area());
}
}
第四题:
packagep4;
publicclassScore{
privatedoublechinese;
privatedoublemath;
privatedoubleenglish;
privatedoublex;
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)
{
this.x=x;
}
publicdoublegetTotalScore()
{
doublets=this.chinese+this.english+this.math+this.x;
returnts;
}
publicdoubleaverage()
{
doubleaver=(this.chinese+this.english+this.math+this.x)/4;
returnaver;
}
}
packagep4;
importjava.util.Scanner;
publicclassTestScore{
publicstaticvoidmain(String[]args)
{
Scoresc=newScore();
Scannerscanner=newScanner(System.in);
System.out.println("创建成绩对象");
System.out.print("输入语文成绩:
");
sc.setChinese(scanner.nextDouble());
System.out.print("输入数学成绩:
");
sc.setMath(scanner.nextDouble());
System.out.print("输入英语成绩:
");
sc.setEnglish(scanner.nextDouble());
System.out.print("输入综合成绩:
");
sc.setX(scanner.nextDouble());
System.out.println("总分:
"+sc.getTotalScore());
System.out.println("平均分:
"+sc.average());
}
}
第五题:
packagep5;
importp4.*;
publicclassStudent{
privateStringid;
privateStringname;
privatebooleanspecial;
privateScorescore;
publicStudent()
{
this.id="000";
this.name="张三";
this.special=false;
}
publicStudent(Stringid,Stringname,booleansp,Scorescore)
{
this.id="000";
this.name="张三";
this.special=false;
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)
{
this.score=score;
}
publicvoidsetScore(doublechinese,doublemath,doubleenglish,doublex)
{
score.setChinese(chinese);
score.setMath(math);
score.setEnglish(english);
score.setX(x);
}
publicdoublegetTotalScore()
{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()
{
System.out.print("无参。
");
}
publicvoidspeak(Stringcontent)
{
System.out.print("一参。
");
}
publicvoidspeak(Stringcontent,Stringlanguage)
{
System.out.print("二参。
");
}
}
packagep5;
importjava.util.Scanner;
importp4.*;
publicclassTestStudent{
publicstaticvoidmain(String[]args)
{Scoresc=newScore();
Stringlanguage="000";
Stringcontent="000";
Studentst=newStudent();
Scannerscanner=newScanner(System.in);
System.out.println("输入学生信息:
");
System.out.print("输入学生学号:
");
st.setId(scanner.nextLine());
System.out.print("输入学生姓名:
");
st.setName(scanner.nextLine());
System.out.print("学生是否为特殊考生:
(1为是,0为否)");
Stringi=scanner.next();
st.setSpecial(i.equals("1"));
Scannerinput=newScanner(System.in);
System.out.println("输入学生成绩:
");
System.out.print("输入语文成绩:
");
doublechinese=input.nextDouble();
System.out.print("输入数学成绩:
");
doublemath=input.nextDouble();
System.out.print("输入英语成绩:
");
doubleenglish=input.nextDouble();
System.out.print("输入综合成绩:
");
doublex=input.nextDouble();
Scoresco=newScore(chinese,math,english,x);
st.setScore(sco);
System.out.print("总分:
"+st.getTotalScore());
System.out.println();
st.speak();
st.speak(content);
st.speak(content,language);
}
}
实验过程记录
记录实验中遇到的困难及解决方法:
困难:
第五题的Student类有点不会
解决方法:
问同学
实验结果记录以及与预期结果比较以及分析
结果和预期的差不多
总结以及新的体会
不熟练,大部分代码都是经过大量的时间琢磨,才勉强能弄好。
填写内容时,可把表格扩大。
实验的源程序代码(要有注释)附在表后。