Instead of using a text file to save all the point coordinates one by one, change the savaData method and the constructor of the Model class to use object serialization and a binary file called "points.bin" to write / read the whole arraylist of points to / from the file in one operation. Use the Test class to run all the tests for the software and check that all the tests still work. Use the Start class to run the software and check that closing the software correctly saves the arraylist of points in the file "points.bin" (you can find the file in the folder for your current project but it is a binary file so you will not be able to read its content). Run the software again and check that all the points from the previous run are correctly displayed.

时间: 2023-07-02 22:05:37 浏览: 105
To implement this change, we can modify the `Model` class to use object serialization. Here's an updated implementation of the `Model` class: ```java import java.io.*; import java.util.ArrayList; public class Model { private ArrayList<Point> points; public Model() { points = new ArrayList<>(); loadData(); } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point point) { points.add(point); } public void saveData() { try { FileOutputStream fileOut = new FileOutputStream("points.bin"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(points); out.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public void loadData() { try { FileInputStream fileIn = new FileInputStream("points.bin"); ObjectInputStream in = new ObjectInputStream(fileIn); points = (ArrayList<Point>) in.readObject(); in.close(); fileIn.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ``` In the `saveData` method, we use an `ObjectOutputStream` to write the entire `ArrayList` of points to a binary file called "points.bin". In the `loadData` method, we use an `ObjectInputStream` to read the entire `ArrayList` from the same binary file. We also need to update the `Test` class to test the new serialization functionality: ```java import org.junit.jupiter.api.Test; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; public class Test { @Test public void testAddPoint() { Model model = new Model(); Point point = new Point(1, 2); model.addPoint(point); ArrayList<Point> points = model.getPoints(); assertEquals(1, points.size()); assertEquals(point, points.get(0)); } @Test public void testSaveAndLoadData() { Model model1 = new Model(); model1.addPoint(new Point(1, 2)); model1.addPoint(new Point(3, 4)); model1.saveData(); Model model2 = new Model(); ArrayList<Point> points = model2.getPoints(); assertEquals(2, points.size()); assertEquals(new Point(1, 2), points.get(0)); assertEquals(new Point(3, 4), points.get(1)); } } ``` In the `testSaveAndLoadData` method, we create a new `Model` instance, add some points, and save the data to a binary file. Then we create another `Model` instance and check that it can correctly load the data from the binary file. Finally, we need to update the `Start` class to use the new serialization functionality: ```java import javafx.application.Application; import javafx.stage.Stage; public class Start extends Application { @Override public void start(Stage primaryStage) throws Exception { Model model = new Model(); View view = new View(primaryStage, model); Controller controller = new Controller(model, view); view.start(); primaryStage.setOnCloseRequest(event -> { model.saveData(); }); } public static void main(String[] args) { launch(args); } } ``` In the `start` method, we create a new `Model` instance and pass it to the `View` and `Controller`. We also add a `setOnCloseRequest` handler to the primary stage to save the data when the user closes the application. With these changes, the software should now use object serialization to save and load the entire `ArrayList` of points in one operation. You can run the `Test` class to verify that all the tests still work, and you can run the `Start` class to verify that the software correctly loads and saves the data to the binary file.
阅读全文

相关推荐

代码We now want to always redraw all the points that have ever been drawn in the panel, not just the last point. To do this, we must save the coordinates of all these points so that we can redraw them all one by one in the paintComponent method every time this method is called. To save the coordinates of the various mouse positions we click, replace the x and y instance variables of the MyPanel class with a single private instance variable called points of type ArrayList. The Point class is provided to you by Swing. In the constructor of MyPanel, initialize the points instance variable with a new arraylist object of the same type. In the mouseClicked method of the mouse listener, use the getPoint method of the mouse event object to get a Point object representing the position of the mouse click (that Point object internally stores both the x and y coordinates of the mouse click event). Then add this Point object to the arraylist using the arraylist’s add method. Then, in the paintComponent method, add a loop to draw in the panel all the points of the arraylist. You can get the number of elements in the arraylist by using the size method of the arraylist; you can access a specific element of the arraylist at index i by using the get(i) method of the arraylist (element indexes start at zero in an arraylist). The Point class has getX and getY methods to get the coordinates of the point (these two methods return values of type double so you need to cast the returned values into the int type before you can use them to draw a point).

根据以下要求:Instead of using a binary file to save the arraylist of points, change the savaData method and the constructor of the Model class to use a database to write / read the coordinates of all the points. Use XAMPP and phpMyAdmin to create a database called "java" with a table called "points" that has two integer columns x and y (in addition to the ID primary key). Hint: make sure you delete all the old point coordinates from the database before inserting new ones. Hint: use phpMyAdmin to check what is stored in the database. Use the Test class to run all the tests for the software and check that all the tests still work. Use the Start class to run the software and check that closing the software correctly saves the point coordinates in the database (use phpMyAdmin to check the content of the database). Run the software again and check that all the points from the previous run are correctly displayed,修改下述代码:public class Model implements Serializable { private ArrayList points; private ArrayList<ModelListener> listeners; private static final String FILE_NAME = "points.bin"; public Model() { points = new ArrayList(); listeners = new ArrayList<ModelListener>(); // Read points from file if it exists File file = new File(FILE_NAME); if (file.exists()) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); points = (ArrayList) in.readObject(); in.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList getPoints() { return points; } public void addPoint(Point p) { points.add(p); notifyListeners(); // points changed so notify the listeners. saveData(); // save point to file } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. saveData(); // save empty list to file } public void deleteLastPoint() { if (points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. saveData(); // save updated list to file } } private void notifyListeners() { for (ModelListener l : listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public void saveData() { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME)); out.writeObject(points); out.close(); } catch (IOException e) { e.printStackTrace(); } }

帮我写出以下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

帮我写出以下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

大家在看

recommend-type

RK eMMC Support List

RK eMMC Support List
recommend-type

UD18415B_海康威视信息发布终端_快速入门指南_V1.1_20200302.pdf

仅供学习方便使用,海康威视信息发布盒配置教程
recommend-type

qt mpi程序设计

qt中使用mpi进行程序设计,以pi的计算来讲解如何使用mpi进行并行程序开发
recommend-type

考研计算机408历年真题及答案pdf汇总来了 计算机考研 计算机408考研 计算机历年真题+解析09-23年

408计算机学科专业基础综合考研历年真题试卷与参考答案 真的很全!2009-2023计算机408历年真题及答案解析汇总(pdf 2009-2023计算机考研408历年真题pdf电子版及解析 2023考研408计算机真题全解 专业408历年算题大全(2009~2023年) 考研计算机408历年真题及答案pdf汇总来了 计算机考研 计算机408考研 计算机历年真题+解析09-23年 408计算机学科专业基础综合考研历年真题试卷与参考答案 真的很全!2009-2023计算机408历年真题及答案解析汇总(pdf 2009-2023计算机考研408历年真题pdf电子版及解析 2023考研408计算机真题全解 专业408历年算题大全(2009~2023年) 考研计算机408历年真题及答案pdf汇总来了 计算机考研 计算机408考研 计算机历年真题+解析09-23年 408计算机学科专业基础综合考研历年真题试卷与参考答案 真的很全!2009-2023计算机408历年真题及答案解析汇总(pdf 2009-2023计算机考研408历年真题pdf电子版及解析 2023考研408计算机真题全解 专业4
recommend-type

应用手册 - SoftMove.pdf

ABB机器人的SoftMove手册,本手册是中文版,中文版,中文版,重要的事情说三遍,ABB原版手册是英文的,而这个手册是中文的。

最新推荐

recommend-type

基于STM32单片机的激光雕刻机控制系统设计-含详细步骤和代码

内容概要:本文详细介绍了基于STM32单片机的激光雕刻机控制系统的设计。系统包括硬件设计、软件设计和机械结构设计,主要功能有可调节激光功率大小、改变雕刻速率、手动定位、精确雕刻及切割。硬件部分包括STM32最小系统、步进电机驱动模块、激光发生器控制电路、人机交互电路和串口通信电路。软件部分涉及STM32CubeMX配置、G代码解析、步进电机控制、激光功率调节和手动定位功能的实现。 适合人群:对嵌入式系统和激光雕刻机感兴趣的工程师和技术人员。 使用场景及目标:① 适用于需要高精度激光雕刻的应用场合;② 为开发类似的激光雕刻控制系统提供设计参考。 阅读建议:本文提供了详细的硬件和软件设计方案,读者应结合实际应用场景进行理解,重点关注电路设计和代码实现。
recommend-type

白色简洁风格的前端网站模板下载.zip

白色简洁风格的前端网站模板下载.zip
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传感器的基本概述和技术规格,然后深入探讨了其编程基础,包括软件接口、数据读取处理及校准测试。接着,本文详细分析了该传感器在嵌入式系统、运动控制和人机交互设备中的实践应用,并且探讨了高级功能开发,
recommend-type

matlab 中实现 astar

在MATLAB中,A*算法是一种用于求解最短路径问题的启发式搜索算法。它适用于带权重的图或网格,特别是当有大量潜在解决方案时,比如迷宫寻路问题。以下是使用MATLAB基本步骤来实现A*算法: 1. **数据结构准备**: - 创建一个二维数组表示地图,其中0代表可以通行的节点,其他值代表障碍物或边界。 - 定义一个队列(通常使用`prioritiesqueue`)来存储待探索的节点及其信息。 2. **初始化**: - 设定起始节点(start),目标节点(goal),以及每个节点的初始g值(从起点到该点的实际代价)和f值(g值加上估计的h值,即启发函数)。 3.