帮我写出以下java代码:The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1. WARNING: when you use the remove method of Java’s ArrayList class to remove an element of an arraylist at index i, the arraylist immediately shifts down by one position all the elements with higher indexes to make the arraylist one element shorter. So, for example, when removing the element at index i, the element at index i+1 immediately moves to the position at index i, the element at index i+2 immediately moves to the position at index i+1, etc. This means that on the next iteration of the loop, when i has become i+1, the element that you will be testing at index i+1 is in fact the element that used to be at index i+2. Which means that the element that used to be at index i+1 (and which is now at index i) will never be tested! Therefore, when removing elements from an arraylist, if your loop starts at index 0 and goes up the indexes in the arraylist, then your loop will fail to test some elements! CONCLUSION: when removing elements from an arraylist, your loop must start from the END of the arraylist and go DOWN to index 0. The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bubbles. Make sure you test as many methods of the Model class as poss

时间: 2023-06-27 20:04:16 浏览: 82
public class Model { private ArrayList<Bubble> bubbles; private int score; public void clearInvisibles(int w, int h) { for (int i = bubbles.size() - 1; i >= 0; i--) { Bubble bubble = bubbles.get(i); if (!bubble.isVisible(w, h)) { bubbles.remove(i); score--; } } } public void deleteBubblesAtPoint(int x, int y) { Iterator<Bubble> iterator = bubbles.iterator(); while (iterator.hasNext()) { Bubble bubble = iterator.next(); if (bubble.containsPoint(x, y)) { iterator.remove(); score++; } } } public void drawAll() { for (Bubble bubble : bubbles) { bubble.draw(); } } } class Bubble { private int x; private int y; private int radius; public boolean isVisible(int w, int h) { return (x + radius >= 0 && x - radius <= w && y + radius >= 0 && y - radius <= h); } public boolean containsPoint(int x, int y) { return (Math.pow((this.x - x), 2) + Math.pow((this.y - y), 2) <= Math.pow(radius, 2)); } public void draw() { // code to draw bubble } }
阅读全文

相关推荐

Write a Model class with the following UML specification: +----------------------------------------------+ | Model | +----------------------------------------------+ | - score: int | | - bubbles: ArrayList<IShape> | +----------------------------------------------+ | + Model() | | + getScore(): int | | + addBubble(int w, int h): void | | + moveAll(int dx, int dy): void | | + clearInvisibles(int w, int h): void | | + deleteBubblesAtPoint(int x, int y): void | | + drawAll(Graphics g): void | | + testModel(): void | +----------------------------------------------+ When a new model object is created, the score must be zero and the arraylist must be empty. The getScore method returns as result the current score for the game. The addBubble method adds a new bubble to the arraylist of bubbles. The position of the center of the new bubble is random but must be inside a window of width w and height h (the arguments of the addBubble method), like this: new Bubble((int)(w * Math.random()), (int)(h * Math.random())) The moveAll method moves the positions of all the bubbles in the arraylist of bubbles by the amount dx in the x direction and by the amount dy in the y direction. The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1.The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bub

帮我写出以下java代码:Add a class Bubble that extends Shape. The Bubble class has an instance variable called radius of type double that represents the radius of the bubble. The constructor of the Bubble class takes an x and a y as arguments, which represent the position of the new bubble. The radius of a new bubble is always 10 and never changes after that. The isVisible method indicates whether the bubble is currently visible inside a window of width w and height h (position (0, 0) is in the upper-left corner of the window). The bubble is considered visible if at least one pixel of the bubble is visible. Therefore a bubble might be visible even when its center is outside the window, as long as the edge of the bubble is still visible inside the window. The code of the isVisible method is a little bit complex, mostly because of the case where the center of the circle is just outside one of the corners of the window. So here is the code of the isVisible method, which you can directly copy-paste into your assignment: // Find the point (wx, wy) inside the window which is closest to the // center (x, y) of the circle. In other words, find the wx in the // interval [0, w - 1] which is closest to x, and find the wy in the // interval [0, h - 1] which is closest to y. // If the distance between (wx, wy) and (x, y) is less than the radius // of the circle (using Pythagoras's theorem) then at least part of // the circle is visible in the window. // Note: if the center of the circle is inside the window, then (wx, wy) // is the same as (x, y), and the distance is 0. public boolean isVisible(int w, int h) { double x = getX(); double y = getY(); double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x)); double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y)); double dx = wx - x; double dy = wy - y; return dx * dx + dy * dy <= radius * radius; } The isIn method indicates whether the point at coordinates (x, y) (which are the arguments of the method) is currently inside the bubble or not. The edge of the bubble counts as being inside of the bubble. HINT: use Pythagoras's theorem to compute the distance from the center of the bubble to the point (x, y). The draw method uses the graphics object g to draw the bubble. HINT: remember that the color of the graphics object g is changed in the draw method of the superclass of Bubble. Also add a testBubble method to test all your methods (including inherited methods, but excluding the isVisible method, which I provide, and excluding the draw method since it requires as argument a graphics object g that you

Add a Library class with the following UML specification: +-------------------------------------------+ | Library | +-------------------------------------------+ | - name: String | | - users: ArrayList | +-------------------------------------------+ | + Library(String name) | | + addUser(IUser user): void | | + totalBorrowedBooks(): int | | + getBook(String name): int | | + moreBook(String name, int number): void | | + testLibrary(): void | +-------------------------------------------+ When a library is created, it has an arraylist of users (IUser) but the arraylist is empty (the arraylist does not contain any user). The addUser method takes a user (IUser) as argument and adds the user to the arraylist of users for the library. The totalBorrowedBooks method returns as result the total number of books borrowed by all users of the library (the result can be either positive or negative). The getBook method takes as argument the name of a user and returns as result the number of books currently borrowed by the user. If the library does not have a user with the given name, then the getBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. You can assume all user names are unique in the arraylist. The moreBook method takes as argument the name of a user and a number of books and changes the number of books currently borrowed by that user. If the library does not have a user with the given name, then the moreBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. Note: the moreBook method does not catch any exception, it only throws exceptions. Hint: use the equals method to compare strings, not the == operator which only works with constant strings. 写java文件

continue to use Java language, Add a class Borrower that extends User. The constructor of the Borrower class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor must throw a NotALenderException with the message “A new borrower cannot lend books.”. The borrower class does not have any instance variable. The moreBook method of the Borrower class increases the number of books borrowed by the borrower by the number of books given as argument to the method (so the books borrowed by the borrower becomes more positive!) For example, if a borrower currently borrows 10 books and moreBook(2) is called then the borrower borrows 12 books. It is fine for the moreBook method to be given a negative value as argument, which means the borrower then just returned some books. For example, if a borrower currently borrows 10 books and moreBook(-2) is called then the borrower borrows 8 books. However, a borrower cannot lend books, so the number of books borrowed by the borrower must always be positive or zero, never negative. If the argument given to the moreBook method is too negative and would change the book variable into a negative value, then the number of books borrowed by the borrower must not change and the moreBook method must throw a NotALenderException with the message “A borrower cannot lend XXX book(s).”, where XXX is replaced with the result of -(book + number). For example, if a borrower currently borrows 10 books and moreBook(-12) is called then the borrower still borrows 10 books and the method throws a NotALenderException with the message “A borrower cannot lend 2 book(s).”. Note: to simplify the project, do not worry about the setBook method. Change other classes and interfaces as necessary

最新推荐

recommend-type

170: Warning: (1681, ‘Integer display width is deprecated and will be removed in a future release.’)

在Python的数据库操作中,尤其是当你使用pymysql库连接MySQL数据库时,可能会遇到一个警告,提示"Integer display width is deprecated and will be removed in a future release." 这个警告源于MySQL数据库的一个...
recommend-type

基于C语言课程设计学生成绩管理系统、详细文档+全部资料+高分项目.zip

【资源说明】 基于C语言课程设计学生成绩管理系统、详细文档+全部资料+高分项目.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于springboot的简历系统源码(java毕业设计完整源码+LW).zip

项目均经过测试,可正常运行! 环境说明: 开发语言:java JDK版本:jdk1.8 框架:springboot 数据库:mysql 5.7/8 数据库工具:navicat 开发软件:eclipse/idea
recommend-type

WildFly 8.x中Apache Camel结合REST和Swagger的演示

资源摘要信息:"CamelEE7RestSwagger:Camel on EE 7 with REST and Swagger Demo" 在深入分析这个资源之前,我们需要先了解几个关键的技术组件,它们是Apache Camel、WildFly、Java DSL、REST服务和Swagger。下面是这些知识点的详细解析: 1. Apache Camel框架: Apache Camel是一个开源的集成框架,它允许开发者采用企业集成模式(Enterprise Integration Patterns,EIP)来实现不同的系统、应用程序和语言之间的无缝集成。Camel基于路由和转换机制,提供了各种组件以支持不同类型的传输和协议,包括HTTP、JMS、TCP/IP等。 2. WildFly应用服务器: WildFly(以前称为JBoss AS)是一款开源的Java应用服务器,由Red Hat开发。它支持最新的Java EE(企业版Java)规范,是Java企业应用开发中的关键组件之一。WildFly提供了一个全面的Java EE平台,用于部署和管理企业级应用程序。 3. Java DSL(领域特定语言): Java DSL是一种专门针对特定领域设计的语言,它是用Java编写的小型语言,可以在Camel中用来定义路由规则。DSL可以提供更简单、更直观的语法来表达复杂的集成逻辑,它使开发者能够以一种更接近业务逻辑的方式来编写集成代码。 4. REST服务: REST(Representational State Transfer)是一种软件架构风格,用于网络上客户端和服务器之间的通信。在RESTful架构中,网络上的每个资源都被唯一标识,并且可以使用标准的HTTP方法(如GET、POST、PUT、DELETE等)进行操作。RESTful服务因其轻量级、易于理解和使用的特性,已经成为Web服务设计的主流风格。 5. Swagger: Swagger是一个开源的框架,它提供了一种标准的方式来设计、构建、记录和使用RESTful Web服务。Swagger允许开发者描述API的结构,这样就可以自动生成文档、客户端库和服务器存根。通过Swagger,可以清晰地了解API提供的功能和如何使用这些API,从而提高API的可用性和开发效率。 结合以上知识点,CamelEE7RestSwagger这个资源演示了如何在WildFly应用服务器上使用Apache Camel创建RESTful服务,并通过Swagger来记录和展示API信息。整个过程涉及以下几个技术步骤: - 首先,需要在WildFly上设置和配置Camel环境,确保Camel能够运行并且可以作为路由引擎来使用。 - 其次,通过Java DSL编写Camel路由,定义如何处理来自客户端的HTTP请求,并根据请求的不同执行相应的业务逻辑。 - 接下来,使用Swagger来记录和描述创建的REST API。这包括定义API的路径、支持的操作、请求参数和响应格式等。 - 最后,通过Swagger提供的工具生成API文档和客户端代码,以及服务器端的存根代码,从而使得开发者可以更加便捷地理解和使用这些RESTful服务。 这个资源的实践演示对于想要学习如何在Java EE平台上使用Camel集成框架,并且希望提供和记录REST服务的开发者来说是非常有价值的。通过这种方式,开发者可以更加快速和简单地创建和管理Web服务,同时也增强了API的可访问性和可维护性。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【声子晶体模拟全能指南】:20年经验技术大佬带你从入门到精通

![【声子晶体模拟全能指南】:20年经验技术大佬带你从入门到精通](https://docs.lammps.org/_images/lammps-gui-main.png) # 摘要 声子晶体作为一种具有周期性结构的材料,在声学隐身、微波和红外领域具有广泛的应用潜力。本文从基础理论出发,深入探讨了声子晶体的概念、物理模型和声子带结构的理论解析,同时介绍了声子晶体的数值模拟方法,包括有限元方法(FEM)、离散元方法(DEM)和分子动力学(MD)。本文还提供了一套完整的声子晶体模拟实践指南,涵盖了模拟前的准备工作、详细的模拟步骤以及结果验证和案例分析。此外,文章探讨了声子晶体模拟的高级技巧和拓展
recommend-type

2024-07-27怎么用python转换成农历日期

在Python中,可以使用`lunarcalendar`库来将公历日期转换为农历日期。首先,你需要安装这个库,可以通过pip命令进行安装: ```bash pip install lunarcalendar ``` 安装完成后,你可以使用以下代码将公历日期转换为农历日期: ```python from lunarcalendar import Converter, Solar, Lunar, DateNotExist # 创建一个公历日期对象 solar_date = Solar(2024, 7, 27) # 将公历日期转换为农历日期 try: lunar_date = Co
recommend-type

FDFS客户端Python库1.2.6版本发布

资源摘要信息:"FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括文件存储、文件同步、文件访问等,适用于大规模文件存储和高并发访问场景。FastDFS为互联网应用量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,保证系统的高可用性和扩展性。 FastDFS 架构包含两个主要的角色:Tracker Server 和 Storage Server。Tracker Server 作用是负载均衡和调度,它接受客户端的请求,为客户端提供文件访问的路径。Storage Server 作用是文件存储,一个 Storage Server 中可以有多个存储路径,文件可以存储在不同的路径上。FastDFS 通过 Tracker Server 和 Storage Server 的配合,可以完成文件上传、下载、删除等操作。 Python 客户端库 fdfs-client-py 是为了解决 FastDFS 文件系统在 Python 环境下的使用。fdfs-client-py 使用了 Thrift 协议,提供了文件上传、下载、删除、查询等接口,使得开发者可以更容易地利用 FastDFS 文件系统进行开发。fdfs-client-py 通常作为 Python 应用程序的一个依赖包进行安装。 针对提供的压缩包文件名 fdfs-client-py-master,这很可能是一个开源项目库的名称。根据文件名和标签“fdfs”,我们可以推测该压缩包包含的是 FastDFS 的 Python 客户端库的源代码文件。这些文件可以用于构建、修改以及扩展 fdfs-client-py 功能以满足特定需求。 由于“标题”和“描述”均与“fdfs-client-py-master1.2.6.zip”有关,没有提供其它具体的信息,因此无法从标题和描述中提取更多的知识点。而压缩包文件名称列表中只有一个文件“fdfs-client-py-master”,这表明我们目前讨论的资源摘要信息是基于对 FastDFS 的 Python 客户端库的一般性了解,而非基于具体文件内容的分析。 根据标签“fdfs”,我们可以深入探讨 FastDFS 相关的概念和技术细节,例如: - FastDFS 的分布式架构设计 - 文件上传下载机制 - 文件同步机制 - 元数据管理 - Tracker Server 的工作原理 - Storage Server 的工作原理 - 容错和数据恢复机制 - 系统的扩展性和弹性伸缩 在实际使用中,开发者可以通过 fdfs-client-py 库来与 FastDFS 文件系统进行交互,利用其提供的 API 接口实现文件的存储、管理等功能,从而开发出高效、可靠的文件处理应用。开发者可以根据项目的实际需求,选择合适的 FastDFS 版本,并根据官方文档进行安装、配置及优化,确保系统稳定运行。 总的来说,fdfs-client-py 是 FastDFS 文件系统与 Python 应用之间的一座桥梁,它使得开发者能够更加方便地将 FastDFS 集成到基于 Python 开发的应用中,发挥出 FastDFS 在文件管理方面的优势。"
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

传感器集成全攻略:ICM-42688-P运动设备应用详解

![传感器集成全攻略:ICM-42688-P运动设备应用详解](https://static.mianbaoban-assets.eet-china.com/xinyu-images/MBXY-CR-ba33fcfbde1d1207d7b8fe45b6ea58d0.png) # 摘要 ICM-42688-P传感器作为一种先进的惯性测量单元,广泛应用于多种运动设备中。本文首先介绍了ICM-42688-P传感器的基本概述和技术规格,然后深入探讨了其编程基础,包括软件接口、数据读取处理及校准测试。接着,本文详细分析了该传感器在嵌入式系统、运动控制和人机交互设备中的实践应用,并且探讨了高级功能开发,