




已阅读5页,还剩25页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
题目一填空题,填充Rational.pdf中划横线的部分,创建一个名为Rational(有理数)的类,用以执行分数运算,通过运行Test.java,测试Rational类的实现结果。 要求用整型变量表示类的private实力变量:numerator(分子)和denominator(分母)。提供构造函数方法,使声明类的对象时可以对他们进行初始化。此构造函数可按化简形式存储分数,例如,对于分数2/4,将1存储在numerator中,2存储在denominator中。同时为下列运算提供public方法: 1) 两个Rational数相加,结果以化简形式输出 2) 两个Rational数相减,结果以化简形式输出 3) 两个Rational数相乘,结果以化简形式输出 4) 两个Rational数相除,结果以化简形式输出 5) 以a/b的形式打印Rational数,其中a为numerator,b为denominator 6) 以浮点格式打印Rational数。 最终运行Test.java的结果为:(将Rational.java与Test.java放在同一目录下)题目二:实现一个复数类(ComplexNumber),其成员变量包含实部和虚部,类成员函数要求实现以下方法,设置和取得实部和虚部(set和get方法),打印该复数(toString方法)。 另外实现静态方法完成两个复数的相加,相乘运算。最后在main测试实现结果是否正确。复数的相加,相乘运算函数声明如下: public static CombNum AddComp(ComplexNumber a, ComplexNumber b) public static CombNum MulComp(ComplexNumber a, ComplexNumber b)题目三:创建一个简单的类,使用这个类的对象计算长方体的表面积和体积。 要求: 1 该类维护一个长方体的长,宽,高,这些变量外部不可使用。 2 有两个构造函数:一个无参数;一个接受三个参数,用之初始化长宽高。 3 提供函数接口设置长宽高属性 4 提供函数接口计算该长方体的表面积和体积 5 在main 函数里,创建这个类的两个对象,其中一个使用无参数构造函数,然后调用类的接口设置长宽高,一个使用三个参数的构造函数。然后分别调用类的接口计算出这两个长方体的表面积和体积。题目四完成一个数字操作类(BasicOperation),实现两个数的加减乘除操作。要求实现的函数声明如下: public int add(int n1, int n2) public double add(double n1, double n2) public int sub(int n1, int n2) public double sub(double n1, double n2) public int mul(int n1, int n2) public double mul(double n1, double n2) public int div(int n1, int n2) public double div(double n1, double n2) public int mod(int n1, int n2) public double mod(double n1, double n2) 另外提供了一个测试类(BPTest.java),在注释部分说明该对象调用的是对象中的哪个方法 例如:System.out.println( 1 + 3 = +bo.add(1,3); /调用的是public int add(int n1, int n2) 需要填写以上绿色注释部分内容题目五根据下面提供的UML类图,编写一个Rectangle类,并实现一个测试程序, (其中color变量为static类型,getColor()方法和setColor(String)方法也为Static类型) 要求分别以(长1,宽2),(长3,宽4)实例化两个Rectangle对象,通过调用对象的方法输出如图所示的结果要求的输出结果为:题目六创建一个类,类名Student。类有三个private 成员属性:学号,姓名,专业。另外要求在类里再使用一个静态变量,此静态变量用于生成实例对象的学号。要求在此类生成实例对象时能自动递增的分配学号。 具体要求: 类只能有一个构造函数,函数原型如下: public Student(String name, String major) 即不能在参数列表中传递学号,并且保证生成的对象其学号是唯一的,并按照生成的顺序递增。 Student 类要提供以下四个方法: public static int getIDSeq() /获得上述Student 类的静态变量的值 public int getID() /获得学生的学号 public String getName() /获得学生姓名 public String getMajor() /获得学生的专业 验证: 提供测试用的main 函数,直接放在Student 类里即可。题目七: 使用给定的MyDate类, 1.实现一个名为Person的类,其成员变量包括:姓,名,家庭住址,手机号,邮箱,出生日期(类型为MyDate). 2.在实现Person类的基础上,实现它的两个子类Student, Employee. Student包含学生的所在年级(freshman, sophomore, junior or senior). Employee类包含给员工的公司名称,工资,聘用日期。 3.在前两步完成的基础上,实现Employee的两个子类Faculty, Staff。Faculty包含工作时间和职位等级成员变量,Staff包含职务成员变量 4对以上各类实现toString 方法,依次打印出它们的信息。 5. 实现简单的测试 附: 1MyDate类 class MyDate public int year; public int month; public int day; public MyDate() public MyDate(int year,int month,int day) this.year = year; this.month = month; this.day = day; public String toString() return MyDate :+year+month+day; 2一个简单的输出例子 java Person : name:张三 address:上海 phoneNumberemail: birthday:MyDate :1987228 Student : name:张三 address:上海 phoneNumberemail: birthday:MyDate :1987228 status:1 Employee : name:张三 address:上海 phoneNumberemail: birthday:MyDate :1987228 salary:10000.0 office:fudanedu hiredDate:MyDate :2006228 Faculty : name:张三 address:上海 phoneNumberemail: birthday:MyDate :1987228 salary:10000.0office:fudanedu hiredDate:MyDate :2006228 officeHour:8 rank:1 Staff : name:张三 address:上海 phoneNumberemail: birthday:MyDate :1987228 salary:10000.0 office:fudanedu hiredDate:MyDate :2006228 title:CFO Output completed (0 sec consumed) Normal Termination题目8仿照Listing 7.13设计并实现一个队列QueueOfInteger类 int add(int i) 向队列头中加入一个值,并返回这个值 int poll(int i) 从队列尾取出一个值,并返回这个值 int peek() 得到队列尾的值,但不从队列中取出 empty(),int getSize()要求同StackOfInteger 题目九1. Design a base class Point that to represent the point in the two-dimensional plane, it contain protected field x and y to represent coordinate,two public method to get and set the x and y. 2.Design a class Circle that extend from Point to represent the circle in the two-dimensional plane,it contain the protected field r that represent radius,public method to get and set the r value and caculate the area of the circle. 3.Design a class Cylind that extend from Circle to reprensent cylind in the three-dimensional space,it contain the protected field h that represent height,public method to get and set the h value and caculate the volume. 4.Creat a circle and a cylind and display their coordinate the radius the height and the volume The output:题目十设计一个程序,输入你的生日,输出当月的月历,并标注出你生日的那一天。要求:l 生日的输入形式可以自己定义。l 输出的格式要求符合下面的例子l 在具体的生日那一天前,使用大于号()标注参照以下例子提示l 使用java.util.Calendar类,参照API相关函数说明,可能用到的方法如下:set(int year, int month, int date) Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.get(int field) Returns the value of the given calendar field.getActualMinimum(int field) Returns the minimum value that the specified calendar field could have, given the time value of this Calendar.l 以上提到方法中的参数field,可参见Calendar中的static常量:n DATEn DAY_OF_MONTHn DAY_OF_WEEK题目十一分数四则运算Rational用以表示一个分数,它有自己的分子和分母部分。RationalCalculate对两个分数进行四则运算完成Rational类中的方法,包括private int gcd(int m, int n)public Rational(int n, int d)public String getRationalInFraction()public String getRationalInFloat()完成RationalCalculate类中的方法public static Rational minus(Rational a, Rational b)public static Rational multiple(Rational a, Rational b)public static Rational divide(Rational a, Rational b)各方法的说明详见代码中的注释题目十二Implement a class named Person and two subclasses of Person named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date-hired. Define a class named MyDate that contains the field year, month, and day. A faculty member has office hours and a rank. A staff member has title. Override the toString method in each class to display the class name and the persons name. Test your classes.题目十二QueueOfInteger是一个整数的队列,与Stack不同(LIFO),焦躁的元素应当被较早地取出(FIFO)。它应该包括以下方法:可以参考书上7.17节StackOfInteger的实现,但是不要使用ArrayList等Java提供的类,使用数组实现QueueOfInteger实现QueueOfInteger之后,将QueueOfInteger.java和Lab8_1.java放置同一目录下。编译并运行Lab8_1.java,如果一切正确,显示如下:你可以在Lab8_1.java中随意增加测试或者print语句帮助你debug提示可先定义一个较小的数组,当数组存放不下时,重新初始化一个更大的数组,将原来的对象全部放到新的数组里。实现search方法时,使用Value() = Value()来比较整数,而不是integer1 = integer2。因为integer1 = integer2判断两个变量是否指向同一个Integer实例,而Value() = Value()判断这两个整数的数值是否相同题目131. 实现一个叫List的类。你可以认为每一个List对象是一个真正的动态Object类型数组,它可以无限制地添加Object类型的元素,同时它有以下的方法:1) Listlist=new List();是创建一个List数组对象。Listlist =new List(Object temp);以上创建一个List数组对象,该数组对象已经初始了一个Object数组temp.2) Objectat(Objecti);获得数组对象在i位置上的元素,如果i大于数组长度,返回null。3) intCount;获得数组当前的长度。4) boolean add(Objectobject);在list数组对象的末尾添加一个元素object,这个操作可以无限制地运行下去。5) boolean addRange(Object temp);在该数组对象的末尾自动添加temp数组。6) boolean addAt(Objectindex,Objectvalue);在数组对象的指定位置index上插入value,后面的元素自动后退一位,如果原始数组的Count小于index,则数组的指标从Count到count+index-1的位置上自动填充0。操作成功返回true,否则返回false。7) boolean removeAt(Objectindex);删除指标index的位置下的值,如果指标index或者index以上位置有元素的话,则index及以上的元素自动上升一位。操作成功返回true,否则返回false。8) boolean removeRange(Objectindex1,Objectindex2);程序删除指标index1和index2之间的所有元素(包括index1和index2),如果index2后面有元素的话,则index后面的元素自动上升(index2-index1+1)位。操作成功返回true,否则返回false。特别说明,boolean方法中,用户的输入是invalid应该直接返回false。2. 编写IntList类继承List类。该数组对象存储所有int类型的元素。3. 编写StringList类继承List类。该数组对象存储String类型的元素。但是,有新的方法:inthasSameCharacter(int index1,int index2);检查指标index1和index2的元素是否有相同的字符,有相同的字符返回1,否则返回-1。用户输入不合法返回0.4. 编写Test类测试以上IntList类和StringList的所有方法(包括构造方法)。Tips:(希望以下几行代码对大家有帮助,O(_)O)char s1=a,b,c; char s2=c,b,a; char s3=new chars1.length+s2.length; System.arraycopy(s1,0,s3,0,s1.length); System.arraycopy(s2,0,s3,s1.length,s2.length); for(int i=0;is3.length;i+)System.out.println(s3i);/测试一下s3数组里面都有哪些值题目14Explain why the underlined code is wrong. 1. (Syntax errors) public class Test private int x; public static void main(String args) new Test(); public Test(int x) this.x = x; 2. (Syntax errors) public class Test public static void main(String args) A a = new A(5.5); System.out.println(a.x); public class A private x; public void A(double x) this.x = x; 3. (Syntax errors) public class A String myStrings = new String2; myStrings0 = new String(A); public A( ) 4. (Runtime error) public class Test public static void main(String args) Object object = new Fruit(); Object object1 = (Apple)object; class Apple extends Fruit class Fruit 题目15Show the printout of the following code: 5. public class Test public static void main(String args) T t = new T(); swap(t); System.out.println(e1 = + t.e1 + e2 = + t.e2); public static void swap(T t) int temp = t.e1; t.e1 = t.e2; t.e2 = temp; class T int e1 = 1; int e2 = 2; 6. public class Test public static void main(String args) T t1 = new T(); T t2 = new T(); System.out.println(t1s i= + t1.i + and j= + t1.j); System.out.println(t2s i= + t2.i + and j= + t2.j); class T static int i = 0; / Please note that i is static int j = 0; T() i+; j+; 7. import java.util.*; public class Test public static void main(String args) Date date = new Date(); Object o = date; Date d = (Date)o; System.out.println(date = o); System.out.println(date = d); 8. class Test public static void main(String args) Count myCount = new Count(); int times = 0; for (int i = 0; i New,选择要加入战斗的机器人后双击或者单击Add 按钮即可,右边的列表包括了所有参加战斗的机器人。另外,BattleField 和Rules 选项卡中还能设置战场大小和坦克参数。 2) 单击 Start Battle 即可开始游戏,此时就可以看到刚才选择的坦克之间的战斗了。 4. Hello World:编写自己的第一个机器人 1) 在 Robocode 菜单中选择 Robot-Editor,在新建的 Robot Editor 窗口中选择File-New-Robot,如 MyFirstRobot,接着输入 package 名字,例如 zellux,以区别于其他名字为 MyFirstRobot 的机器人(还记得 package 的作用吗?这在多人 开发的项目中尤其重要)。 2) 程序会自动帮我们生成一段简单的机器人代码。让我们来分析下这个程序: package zellux; import robocode.*; public class MyFirstRobot extends Robot public void run() while(true) ahead(100); turnGunRight(360); back(100); turnGunRight(360); public void onScannedRobot(ScannedRobotEvent e) fire(1); public void onHitByBullet(HitByBulletEvent e) turnLeft(90 - e.getBearing(); MyFirstRobot 是一个继承了 Robot 的类,因此很多功能都已经封装在它的父类 Robot 中。这里我们可以感受到面向对象编程的优点,想想如果要不依赖于 Robot 库写一个类似的机器人,势必要实现许多像绘制机器人、判断是否中弹等方法。 同时,面向对象的封装性(encapsulation)把这些实现都隐藏了起来,我们不必知道这些细节到底是怎么实现的,只要知道 Robot 类中已经有相关的代码帮我们处理了这些问题就行。 但是后面我们会看到,Robot 类只是一个实现了如何移动、如何攻击、如何判断敌人位置等方法的类,具体该什么时候攻击、如何通过移动躲避敌人的子弹、看到敌人后怎么做出反应,这就需要我们自己写程序处理了。 MyFirstRobot 中最重要的一个方法就是 run()方法,这个方法在 Robot 类中是一个函数体为空的方法。而在 MyFirstRobot 中,这个方法的作用是不停地让机器人前后移动ahead(100) back(100),并不断调整炮口 turnGunRight(360),这是一个很简单的 AI,躲避子弹的能力很有限。 注:或许细心的同学会发现这四个方法是在一个 while (true)语句块中不断重复执行的,这似乎是一个死循环,永远不会结束。但事实上 Robot 是一个实现了 Runnable 接口的类,在运行机器人的时候虚拟机是通过新开一个线程来模拟机器人的运动的,因此这里的“死循环”不会影响其他机器人的运动。更多有关线程的知识推荐大家看 Core Java(Java 核心技术)第二卷的第一章。 接下来是 onScannedRobot 和 onHitByBullet 方法,这两个方法都是事件驱动(Event-Driven)的,很像 SWING 中的 mousePressed(MouseEvent e) actionPerformed(ActionEvent e)等方法,都是在某个事件发生后才会被调用。例如在一定范围内前方有敌人时系统会调用 onScannedRobot,在 MyFirstRobot 这个例子中发现敌人后的处理方法只是简单的向前发射,恐怕只对不会移动的敌人才有效 ;-) 。 5. 学会看 API 文档 想要自己的机器人变得更强,就需要为它添加更详细的战斗策略。那么如何知道Robocode 这个系统给了我们哪些有用的方法呢? API 文档是学习编程过程中很重要的资料,JDK 就为我们提供了详细的文档,里面包括了各种类及其方法的使用说明。Robocode 也一样。 robocodejavadocindex.html 就是这份文档的索引文件。打开后选择左边的Robot 类,我们可以看到这个类提供的所有方法。比如 getEnergy()方法返回当前的生命值,以便对当前战术做出不同的决策;此外 onBulletMissed(BulletMissedEvent event)会在子弹未击中对方时被调用。 另外我们还可以看到另一个功能更丰富的 AdvancedRobot 类,它继承了 Robot 类,也就是说包括了 Robot 类所有 public 和 protected 方法,同时还加入了数据文件操作(可以用于记忆学习)、自定义事件等高级功能。更多的东西就要大家自己去挖掘了 _。 6. 学习别人的代码 最后,想写出一个强大的机器人,还需要学习他人的代码,这也是编程学习过程中不可或缺的一个环节。 robocoderobotssample 下就有一些简单的机器人的源代码,从中我们可以看到一些方法和事件具体的应用。例如 Tracker 机器人会根据机器人的名字锁定一个目标,然后穷追猛打;而 Interactive 机器人则可以接受玩家的控制,当然这在比赛中是不允许的。 这些机器人以演示方法为主,AI 都比较简单。 /Categories.jsp 可以下载到许多更高级的机器人的源代码,其中包括更智能的目标追踪、躲避子弹等一些涉及算法、数学知识的功能。 What need to do write your own robot1、 Write a class extends robot class2、 Implement three functions: move, fight and scanMove is the way your robot move in battle field, which your robot is good to escape from other robots gun is depend on this.Fight is the main function how your robot fire other robotsScan is a function to use radar to discovery other robotsv 每人做一个自己认为最好的Robot,下次课程结束时,我们将根据Robot PK得分来评分(共两次课总分20分),并请部分优秀的同学分享自己的成功经验v 上传文件及地址(下周lab课程结束) 约定命名规范v package=fduss2007 v Robot Name=Robot + your student number work_uploadRobotcodefduss2007v Robot*.javav Robot*.class题目18Print a resume 1. Design a class named Resume to contain the following instance data fields: a) Some variables of basic information including: school(String), major(String), name(String), mobile phone number(long), mail address(String), degree (it should have only 3 status of UNDERGRADUATE, GRADUATE, or DOCTOR), and birth(String or Date). (you may also add something new) b) A inner-class called EducationExperience including: start_time(String or Date), end_time(String or Date), school(String). You can initialize your value through either Constructor or Method, but do not assign the variable directly. c) The EducationExperience class should have a variable or method to track how many times it has been invoked. (Hint: this variable should be shared by all instances) d) A string array of programming language skills like: Java, C, Perl, etc.(you can search on internet) e) A string called selfAssessment: an assessment of how this person is (like: optimistic, good at team-work, a work-alcohol, etc.) f) A method to print the resume. 2. Write a main class to invoke your Resume class. You should print the resume and the output should include the information, educational experience (and the time it has been invoked), programming language skills and personal assessment. The output should be like this: The resume could be either yourself or someone else. 题目19Fighting club 1. Design a class named Fighter to contain the following instance data fields: a) HP: the remaining health point of the fighter. HP is a random value from 150 to 300. b) Offense, defense, lucky: some parameters which will affect the result of each wave of attack. Offense, defense and lucky are all random values from 10 to 100. c) A method to get a random integer from MIN to MAX.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 新生幼儿半日活动方案
- 星光夜市活动方案
- 新兵参观活动方案
- 文化馆剪窗花活动方案
- 新城活动策划方案
- 教育活动幼儿活动方案
- 新店开业机器人活动方案
- 文化考核活动方案
- 春季少儿活动方案
- 春节村寨活动方案
- 2024北京朝阳区高二(下)期末英语试题和答案
- 破碎安全培训
- 初中数学新课程标准(2024版)
- 电信通信设备的应急维修
- 源头治超培训
- 出院病人终末消毒流程
- 2024年08月海南省财金集团有限公司招考实习见习生笔试历年参考题库附带答案详解
- star法则培训课件
- 【MOOC】逻辑学导论-西北大学 中国大学慕课MOOC答案
- 氯及其化合物(完整版)课件
- 【MOOC】3D工程图学-华中科技大学 中国大学慕课MOOC答案
评论
0/150
提交评论