面向对象技术Java-期末复习试题(二).docx
精品文档,仅供学习与交流,如有侵权请联系网站删除期末复习试题(二)一、 程序题1、编写程序创建Point类,要求如下:(1)double类型的数据域x和y分别表示点的坐标;(2)x、y的get和set方法;(3)一个无参构造方法;(4)一个创建点对象同时指定x和y坐标的有参的构造方法;(5)一个名为distance(Point p)的方法,返回从该点到指定点之间的距离;(6)一个名为distance(double x, double y)的方法,返回从该点到指定x和y坐标的指定点之间的距离。解题要求:编写测试类,分别调用两个distance方法,计算:(1)点(2,3)到(10,30)之间的距离并显示;(2)点(4,5)到(20,50)之间的距离并显示。public class TestPoint public static void main(String a)System.out.println("前两点间的距离是" + new Point(2,3).distance(new Point(10,30);System.out.println("后两点间的距离是" + new Point(4,5).distance(20,50);class Pointprivate double x,y;public Point()public Point(double x, double y)this.x = x;this.y = y;public double getX() return x;public void setX(double x) this.x = x;public double getY() return y;public void setY(double y) this.y = y;public double distance(Point p)return Math.sqrt(x计算通过参数传递进来的p点与当前点的距离-p.getX()*(x-p.getX() + (y-p.getY()*(y-p.getY();public double distance(double x, double y)return Math.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(String student)。(5)一个查询选课学生数量的方法getNumberOfStduents()解题要求:编写测试类,创建课程对象,添加3个选课学生,按照如下提示输出课程信息。import java.util.ArrayList;public class TestCourse public static void main(String arg)Course c = new Course("面向对象技术");c.addStudents("张三");c.addStudents("李四");c.addStudents("王五");System.out.println(c.toString(); class Courseprivate String courseName;private ArrayList<String> students = new ArrayList<String>();public Course(String courseName)this.courseName = courseName;public void addStudents(String student)students.add(student);public ArrayList<String> getStudents()return students;public int getNumberOfStudents()return students.size();public String getCourseName()return courseName;public void dropStudent(String student)students.remove(student);public String toString()String s = ""局部变量在使用前需要初始化for(int i = 0; i < students.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),计算并输出线段的长度。public class UseLine public static void main(String arg) Line lFirst = new Line(10,20,30,40); System.out.println("线段的长度为" + lFirst.getLength(); Line lSecond = new Line(new Point(3,4),new Point(9,10); System.out.println("线段的长度为" + lSecond.getLength(); class Pointprivate int x; private int y; public Point(int x, int y)this.x = x;this.y = y;public int getX() return x;public void setX(int x) this.x = x;public int getY() return y;public void setY(int y) this.y = y;class Lineprivate Point point1; private Point point2; public Line() public Line(int x1, int y1, int x2, int y2)point1 = new Point(x1, y1);注意不要写成:point1.setX(x1);point1.setY(y1);在调用point1的方法的时候,piont1一定在某个位置通过new创建出来,否则出现空指针错误。如果Point类有参数为空的构造方法,则把上面的代码如下修改就可以了 :point1 = new Point();point1.setX(x1);point1.setY(y1);point2 = new Point(x2, y2);public Line(Point p1, Point p2)point1 = p1;point2 = p2;public double getLength()return Math.sqrt(point1.getX()-point2.getX()*(point1.getX()-point2.getX() + (point1.getY()-point2.getY()*(point1.getY()-point2.getY();public Point getPoint1() return point1;public void setPoint1(Point point1) this.point1 = point1;public Point getPoint2() return point2;public void setPoint2(Point point2) 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.按照如下顺序显示每个矩形的宽,高 ,周长和面积。public class TestRectangle public static void main(String args) Rectangle rectangle = new Rectangle(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(); Rectangle rectangle1 = new Rectangle(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(); class Rectangle double width; double height; public Rectangle() public Rectangle(double width, double height) this.width = width; this.height = height; public double getWidth() return width; public void setWidth(double width) this.width = width; public double getHeight() return height; public void setHeight(double height) this.height = height; public double getArea() return width * height; public double getPerimeter() return 2 * (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()方法,输出结果。public class TestMyInteger public static void main(String args) MyInteger n1 = new MyInteger(5); System.out.println("n1 is even? " + n1.isEven(); System.out.println("n1 is odd? " + n1.isOdd(); MyInteger n2 = new MyInteger(6); System.out.println("n2 is even? " + n2.isEven(); System.out.println("n2 is odd? " + n2.isOdd(); class MyInteger private int value; public MyInteger(int value) this.value = value; public void setValue(int value) this.value = value; public int getValue() return value; public boolean isEven() return value % 2 = 0; public boolean isOdd() return value % 2 != 0; 【精品文档】第 8 页