




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上精选优质文档-倾情为你奉上专心-专注-专业专心-专注-专业精选优质文档-倾情为你奉上专心-专注-专业期末复习试题(二)程序题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坐标的指定点之间的距离。解题要求:编写测试类,分别调用两
2、个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()p
3、ublic 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.g
4、etY();计算通过参数传递进来的p点与当前点的距离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)一个添加学生的方法addSt
5、udent(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()
6、; class Courseprivate String courseName;private ArrayList students = new ArrayList();public Course(String courseName)this.courseName = courseName;public void addStudents(String student)students.add(student);public ArrayList getStudents()return students;public int getNumberOfStudents()return students
7、.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()
8、 + 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),
9、计算并输出线段的长度。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; p
10、ublic 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
11、(x1, y1);注意不要写成:point1.setX(x1);point1.setY(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;point
12、2 = 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
13、;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而
14、高为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面积 +
15、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;
16、 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
17、; 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。解题要求:编写测试类,创建对
18、象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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025室内装修施工合同文本
- 建筑信息模型技术员练习题库(附参考答案)
- 发电机采购协议
- 土地流转使用权转让与种植计划合同
- 浙江国企招聘2025衢州市属国企春季招聘23人笔试参考题库附带答案详解
- 2025重庆西南证券股份有限公司招聘45人笔试参考题库附带答案详解
- 2025年第一季度广西兴工投资集团有限公司招聘21人笔试参考题库附带答案详解
- 2025年安徽九华山旅游发展股份有限公司招聘66人笔试参考题库附带答案详解
- 2025北京大兴区司法局招聘临时辅助用工1人笔试参考题库附带答案详解
- 青职综合评价试题及答案
- 工程开票申请表
- 船舶岸基应急预案
- 6人小品《没有学习的人不伤心》台词完整版
- 企业零代码应用开发白皮书-2023.03
- 巴蜀武术天下奇
- 装在套子里的人公开课
- 教科版四年级下册科学《植物的生长变化》单元解读
- 英文电影鉴赏知到章节答案智慧树2023年北华大学
- (完整版)一年级必诵童谣、儿歌
- 2022年03月四川成都市公园城市建设管理局事业单位公开招聘54名工作人员笔试题库含答案解析
- 年产吲哚美辛的生产设计设计说明书
评论
0/150
提交评论