根据以下要求: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.修改下述代码:import java.awt.Point; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Model { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; public Model() { points = new ArrayList<Point>(); listeners = new ArrayList<ModelListener>(); // Read points from file if it exists File file = new File("points.txt"); if (file.exists()) { try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String[] coordinates = scanner.nextLine().split(" "); int x = (int) Double.parseDouble(coordinates[0]); int y = (int) Double.parseDouble(coordinates[1]); points.add(new Point(x, y)); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> 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 { FileWriter writer = new FileWriter("points.txt"); for (Point p : points) { writer.write(p.x + " " + p.y + "\n"); } writer.close(); } catch (IOException e) { e.printStackTrace(); } }

时间: 2024-01-05 09:05:16 浏览: 108
ZIP

PSO.zip_PSO best solution_The Best Of All_best-so-far_pso global

以下是修改后的代码: import java.awt.Point; import java.io.*; public class Model implements Serializable { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; private static final String FILE_NAME = "points.bin"; public Model() { points = new ArrayList<Point>(); 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<Point>) in.readObject(); in.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> 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的序列化机制来保存整个ArrayList对象,同时将数据保存到了一个二进制文件中(名为"points.bin")。在构造函数中,我们首先检查是否存在该文件,如果存在,则通过ObjectInputStream读取该文件并将ArrayList对象恢复到程序中。在saveData()方法中,我们使用ObjectOutputStream将整个ArrayList对象写入文件。注意:我们还必须将Model类实现Serializable接口来支持序列化。
阅读全文

相关推荐

根据以下要求: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(); } }

代码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).

帮我写出以下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 to the Model class a new method called savaData that saves into a text file called "points.txt" the integer coordinates x and y of each point in the arraylist of points. Also modify the constructor of the Model class to read the integer coordinates of all the points from the same text file, if it exists, and put them into the arraylist of points (if the file does not exist then the arraylist of points should remain empty).public class Model { private ArrayList points; private ArrayList<ModelListener> listeners; public Model() { points = new ArrayList(); listeners = new ArrayList<ModelListener>(); } 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. } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. } public void deleteLastPoint() { if(points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. } } private void notifyListeners() { for(ModelListener l: listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public static void testModel() { Model m = new Model(); m.addListener(new ModelListener() { @Override public void update() { System.out.println(true + " (listener)"); } }); System.out.println(m.getPoints() == m.points); Point p1 = new Point(1, 2); Point p2 = new Point(3, 4); m.addPoint(p1); // Listener called. m.addPoint(p2); // Listener called. System.out.println(m.numberOfPoints() == 2); System.out.println(m.points.get(0) == p1); System.out.println(m.points.get(1) == p2); m.deleteLastPoint(); // Listener called. System.out.println(m.numberOfPoints() == 1); System.out.println(m.points.get(0) == p1); m.clearAllPoints(); // Listener called. System.out.println(m.numberOfPoints() == 0); m.notifyListeners(); // Listener called. } }修改下述代码:

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

华普微四通道数字隔离器

华普微四通道数字隔离器,替换纳芯微,川土微
recommend-type

基于区块链的分级诊疗数据共享系统全部资料+详细文档.zip

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

正整数数组验证库:确保值符合正整数规则

资源摘要信息:"validate.io-positive-integer-array是一个JavaScript库,用于验证一个值是否为正整数数组。该库可以通过npm包管理器进行安装,并且提供了在浏览器中使用的方案。" 该知识点主要涉及到以下几个方面: 1. JavaScript库的使用:validate.io-positive-integer-array是一个专门用于验证数据的JavaScript库,这是JavaScript编程中常见的应用场景。在JavaScript中,库是一个封装好的功能集合,可以很方便地在项目中使用。通过使用这些库,开发者可以节省大量的时间,不必从头开始编写相同的代码。 2. npm包管理器:npm是Node.js的包管理器,用于安装和管理项目依赖。validate.io-positive-integer-array可以通过npm命令"npm install validate.io-positive-integer-array"进行安装,非常方便快捷。这是现代JavaScript开发的重要工具,可以帮助开发者管理和维护项目中的依赖。 3. 浏览器端的使用:validate.io-positive-integer-array提供了在浏览器端使用的方案,这意味着开发者可以在前端项目中直接使用这个库。这使得在浏览器端进行数据验证变得更加方便。 4. 验证正整数数组:validate.io-positive-integer-array的主要功能是验证一个值是否为正整数数组。这是一个在数据处理中常见的需求,特别是在表单验证和数据清洗过程中。通过这个库,开发者可以轻松地进行这类验证,提高数据处理的效率和准确性。 5. 使用方法:validate.io-positive-integer-array提供了简单的使用方法。开发者只需要引入库,然后调用isValid函数并传入需要验证的值即可。返回的结果是一个布尔值,表示输入的值是否为正整数数组。这种简单的API设计使得库的使用变得非常容易上手。 6. 特殊情况处理:validate.io-positive-integer-array还考虑了特殊情况的处理,例如空数组。对于空数组,库会返回false,这帮助开发者避免在数据处理过程中出现错误。 总结来说,validate.io-positive-integer-array是一个功能实用、使用方便的JavaScript库,可以大大简化在JavaScript项目中进行正整数数组验证的工作。通过学习和使用这个库,开发者可以更加高效和准确地处理数据验证问题。
recommend-type

管理建模和仿真的文件

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

【损失函数与随机梯度下降】:探索学习率对损失函数的影响,实现高效模型训练

![【损失函数与随机梯度下降】:探索学习率对损失函数的影响,实现高效模型训练](https://img-blog.csdnimg.cn/20210619170251934.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNjc4MDA1,size_16,color_FFFFFF,t_70) # 1. 损失函数与随机梯度下降基础 在机器学习中,损失函数和随机梯度下降(SGD)是核心概念,它们共同决定着模型的训练过程和效果。本
recommend-type

在ADS软件中,如何选择并优化低噪声放大器的直流工作点以实现最佳性能?

在使用ADS软件进行低噪声放大器设计时,选择和优化直流工作点是至关重要的步骤,它直接关系到放大器的稳定性和性能指标。为了帮助你更有效地进行这一过程,推荐参考《ADS软件设计低噪声放大器:直流工作点选择与仿真技巧》,这将为你提供实用的设计技巧和优化方法。 参考资源链接:[ADS软件设计低噪声放大器:直流工作点选择与仿真技巧](https://wenku.csdn.net/doc/9867xzg0gw?spm=1055.2569.3001.10343) 直流工作点的选择应基于晶体管的直流特性,如I-V曲线,确保工作点处于晶体管的最佳线性区域内。在ADS中,你首先需要建立一个包含晶体管和偏置网络
recommend-type

系统移植工具集:镜像、工具链及其他必备软件包

资源摘要信息:"系统移植文件包通常包含了操作系统的核心映像、编译和开发所需的工具链以及其他辅助工具,这些组件共同作用,使得开发者能够在新的硬件平台上部署和运行操作系统。" 系统移植文件包是软件开发和嵌入式系统设计中的一个重要概念。在进行系统移植时,开发者需要将操作系统从一个硬件平台转移到另一个硬件平台。这个过程不仅需要操作系统的系统镜像,还需要一系列工具来辅助整个移植过程。下面将详细说明标题和描述中提到的知识点。 **系统镜像** 系统镜像是操作系统的核心部分,它包含了操作系统启动、运行所需的所有必要文件和配置。在系统移植的语境中,系统镜像通常是指操作系统安装在特定硬件平台上的完整副本。例如,Linux系统镜像通常包含了内核(kernel)、系统库、应用程序、配置文件等。当进行系统移植时,开发者需要获取到适合目标硬件平台的系统镜像。 **工具链** 工具链是系统移植中的关键部分,它包括了一系列用于编译、链接和构建代码的工具。通常,工具链包括编译器(如GCC)、链接器、库文件和调试器等。在移植过程中,开发者使用工具链将源代码编译成适合新硬件平台的机器代码。例如,如果原平台使用ARM架构,而目标平台使用x86架构,则需要重新编译源代码,生成可以在x86平台上运行的二进制文件。 **其他工具** 除了系统镜像和工具链,系统移植文件包还可能包括其他辅助工具。这些工具可能包括: - 启动加载程序(Bootloader):负责初始化硬件设备,加载操作系统。 - 驱动程序:使得操作系统能够识别和管理硬件资源,如硬盘、显卡、网络适配器等。 - 配置工具:用于配置操作系统在新硬件上的运行参数。 - 系统测试工具:用于检测和验证移植后的操作系统是否能够正常运行。 **文件包** 文件包通常是指所有这些组件打包在一起的集合。这些文件可能以压缩包的形式存在,方便下载、存储和传输。文件包的名称列表中可能包含如下内容: - 操作系统特定版本的镜像文件。 - 工具链相关的可执行程序、库文件和配置文件。 - 启动加载程序的二进制代码。 - 驱动程序包。 - 配置和部署脚本。 - 文档说明,包括移植指南、版本说明和API文档等。 在进行系统移植时,开发者首先需要下载对应的文件包,解压后按照文档中的指导进行操作。在整个过程中,开发者需要具备一定的硬件知识和软件开发经验,以确保操作系统能够在新的硬件上正确安装和运行。 总结来说,系统移植文件包是将操作系统和相关工具打包在一起,以便于开发者能够在新硬件平台上进行系统部署。了解和掌握这些组件的使用方法和作用是进行系统移植工作的重要基础。
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

【损失函数与批量梯度下降】:分析批量大小对损失函数影响,优化模型学习路径

![损失函数(Loss Function)](https://img-blog.csdnimg.cn/20190921134848621.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc3MjUzMw==,size_16,color_FFFFFF,t_70) # 1. 损失函数与批量梯度下降基础 在机器学习和深度学习领域,损失函数和批量梯度下降是核心概念,它们是模型训练过程中的基石。理解它们的基础概念对于构建
recommend-type

在设计高性能模拟电路时,如何根据应用需求选择合适的运算放大器,并评估供电对电路性能的影响?

在选择运算放大器以及考虑供电对模拟电路性能的影响时,您需要掌握一系列的关键参数和设计准则。这包括运算放大器的增益带宽积(GBWP)、输入偏置电流、输入偏置电压、输入失调电压、供电范围、共模抑制比(CMRR)、电源抑制比(PSRR)等。合理的选择运算放大器需考虑电路的输入和输出范围、负载大小、信号频率、温度系数、噪声水平等因素。而供电对性能的影响则体现在供电电压的稳定性、供电噪声、电源电流消耗、电源抑制比等方面。为了深入理解这些概念及其在设计中的应用,请参考《模拟电路设计:艺术、科学与个性》一书,该书由模拟电路设计领域的大师Jim Williams所著。您将通过书中的丰富案例学习如何针对不同应用