期末复习试题(二)一、程序题1、编写程序创建Point类,要求如下:(1)double类型的数据域x和y分别表示点的坐标;(2)x、y的get和set方法;(3)一个无参构造方法;(4)一个创建点对象同时指定x和y坐标的有参的构造方法;(5)一个名为distance(Pointp)的方法,返回从该点到指定点之间的距离;(6)一个名为distance(doublex,doubley)的方法,返回从该点到指定x和y坐标的指定点之间的距离。解题要求:编写测试类,分别调用两个distance方法,计算:(1)点(2,3)到(10,30)之间的距离并显示;(2)点(4,5)到(20,50)之间的距离并显示。publicclassTestPoint{publicstaticvoidmain(String[]a){System.out.println(前两点间的距离是+newPoint(2,3).distance(newPoint(10,30)));System.out.println(后两点间的距离是+newPoint(4,5).distance(20,50));}}classPoint{privatedoublex,y;publicPoint(){}publicPoint(doublex,doubley){this.x=x;this.y=y;}publicdoublegetX(){returnx;}publicvoidsetX(doublex){this.x=x;}publicdoublegetY(){returny;}publicvoidsetY(doubley){this.y=y;}publicdoubledistance(Pointp){returnMath.sqrt((x-p.getX())*(x-p.getX())+(y-p.getY())*(y-p.getY()));}publicdoubledistance(doublex,doubley){returnMath.sqrt((x-this.x)*(x-this.x)+(y-this.y)*(y-this.y));}}2、下图是课程类Course的UML类图,说明如下:(1)成员变量包括课程名称(courseName)和选课学生(students),选课学生存放在ArrayList链表中。(2)包括成员变量的set和get方法。(3)一个输出课程信息的方法toString(),可以输出课程名称、选课学生名单和选课人数。(4)一个添加学生的方法addStudent(Stringstudent)。(5)一个查询选课学生数量的方法getNumberOfStduents()解题要求:编写测试类,创建课程对象,添加3个选课学生,按照如下提示输出课程信息。importjava.util.ArrayList;publicclassTestCourse{publicstaticvoidmain(String[]arg){Coursec=newCourse(面向对象技术);c.addStudents(张三);c.addStudents(李四);c.addStudents(王五);System.out.println(c.toString());}}classCourse{privateStringcourseName;privateArrayListStringstudents=newArrayListString();publicCourse(StringcourseName){this.courseName=courseName;}publicvoidaddStudents(Stringstudent){students.add(student);}publicArrayListStringgetStudents(){returnstudents;}publicintgetNumberOfStudents(){returnstudents.size();}publicStringgetCourseName(){returncourseName;}publicvoiddropStudent(Stringstudent){students.remove(student);}publicStringtoString(){Strings=;for(inti=0;istudents.size();i++){s+=+students.get(i);}return课程名称+courseName+\n+选课人数+getNumberOfStudents()+\n+学生名单+s;}}3、下图描述了两个类:Line(线段)和Point(点),以及两个类之间的关联关系,一条线段对象由对应的两个点对象组成。解题要求:要求如下:(1)编写Line(线段)和Point(点)两个类的代码,注意满足封装的需求。将数据隐藏,通过方法访问数据。(2)使用Line类的Line(x1:int,y1:int,x2:int,y2:int)方法,创建Line对象,端点是(10,20)、(30,40),计算并输出线段的长度。(3)使用Line类的Line(p1:Point,p2:Point)方法,创建Line对象,端点是(3,4)、(9,,10),计算并输出线段的长度。publicclassUseLine{publicstaticvoidmain(String[]arg){LinelFirst=newLine(10,20,30,40);System.out.println(线段的长度为+lFirst.getLength());LinelSecond=newLine(newPoint(3,4),newPoint(9,10));System.out.println(线段的长度为+lSecond.getLength());}}classPoint{privateintx;privateinty;publicPoint(intx,inty){this.x=x;this.y=y;}publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}}classLine{privatePointpoint1;privatePointpoint2;publicLine(){}publicLine(intx1,inty1,intx2,inty2){point1=newPoint(x1,y1);point2=newPoint(x2,y2);}publicLine(Pointp1,Pointp2){point1=p1;point2=p2;}publicdoublegetLength(){returnMath.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));}publicPointgetPoint1(){returnpoint1;}publicvoidsetPoint1(Pointpoint1){this.point1=point1;}publicPointgetPoint2(){returnpoint2;}publicvoidsetPoint2(Pointpoint2){this.point2=point2;}}4、创建矩形类Rectangle,包括(1)两个名为width和height的double型数据域,它们分别表示矩形的宽和高.width和height的默认值都为1.(2)创建默认矩形的无参构造方法。(3)一个创建width和height为指定值的矩形的构造方法。(4)一个名为getArea()的方法返回这个矩形的面积(5)一个名为getPerimeter()的方法返回周长。解题要求:编写测试程序,创建两个Rectangle对象,其中一个宽为4而高为40,另一个矩形的宽为3.5而高为35.9.按照如下顺序显示每个矩形的宽,高,周长和面积。publicclassTestRectangle{publicstaticvoidmain(String[]args){Rectanglerectangle=newRectangle(4,40);System.out.println(\n宽度+rectangle.width);System.out.println(\n高度+rectangle.height);System.out.println(\n周长+rectangle.getPerimeter());System.out.println(\n面积+rectangle.getArea());Rectanglerectangle1=newRectangle(3.5,35.9);System.out.println(\n宽度+rectangle1.width);System.out.println(\n高度+rectangle1.height);System.out.println(\n周长+rectangle1.getPerimeter());System.out.println(\n面积+rectangle1.getArea());}}classRectangle{doublewidth;doubleheight;publicRectangle(){}publicRectangle(doublewidth,doubleheight){this.width=width;this.height=height;}publicdoublegetWidth(){returnwidth;}publicvoidsetWidth(doublewidth){this.width=width;}publicdoublegetHeight(){returnheight;}publicvoidsetHeight(doubleheight){this.height=height;}publicdoublegetArea(){returnwidth*height;}publicdoublegetPerimeter(){return2*(width+height);}}5、模拟Integer编写int类型的包装类MyInteger,要求如下:(1)一个名为value的int型私有数据域,存储这个对象表示的int值;(2)一个为指定的int值创建MyInteger对象的构造方法;(3)一个返回/设置value值的get和set方法。(4)如果值分别为偶数、奇数,那么isEven()、isOdd()的方法都会返回true。解题要求:编写测试类,创建对象n1,设置其value属性值为5,分别调用isEven()和isOdd()方法,输出结果;创建对象n2,设置其value属性值为6,分别调用isEven()和isOdd()方法,输出结果。publicclassTestMyInteger{publicstaticvoidmain(String[]args){MyIntegern1=newMyInteger(5);System.out.println(n1iseven?+n1.isEven());System.out.println(n1isodd?+n1.isOdd());MyIntegern2=newMyInteger(6);System.out.println(n2iseven?+n2.isEven());System.out.println(n2isodd?+n2.isOdd());}}classMyInteger{privateintvalue;publicMyInteger(intvalue){this.