You can use the Test class to run all the tests for the software and you can use the Start class to run the software itself. Run this code and check that it works correctly. 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). Add to the Controller superclass a protected shutdown method that: calls the saveData method of the model; manually terminates the program using System.exit(0). Then modify the View superclass to: hide the frame when the user clicks on the “close” button; add a “window closing” event handler (use an anonymous window adapter) that calls the controller’s shutdown method. 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 all the point coordinates in the file "points.txt" (you can find the file in the folder for your current project). Run the software again and check that all the points from the previous run are correctly displayed.
时间: 2023-07-02 15:05:55 浏览: 56
Okay, I understand the instructions. Here's what I'll do:
First, I'll add a new method called `saveData` to the `Model` class, which will save the integer coordinates x and y of each point in the arraylist of points to a text file called "points.txt". Here's the code for that:
```
public void saveData() {
try {
FileWriter fw = new FileWriter("points.txt");
for (Point p : points) {
fw.write(p.x + "," + p.y + "\n");
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
Next, I'll 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. Here's the updated constructor code:
```
public Model() {
points = new ArrayList<Point>();
try {
File file = new File("points.txt");
if (file.exists()) {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String[] coords = scanner.nextLine().split(",");
int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]);
points.add(new Point(x, y));
}
scanner.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
```
Then, I'll add a protected `shutdown` method to the `Controller` superclass, which will call the `saveData` method of the model and manually terminate the program using `System.exit(0)`. Here's the code for that:
```
protected void shutdown() {
model.saveData();
System.exit(0);
}
```
Finally, I'll modify the `View` superclass to hide the frame when the user clicks on the "close" button, and add a "window closing" event handler (using an anonymous window adapter) that calls the controller's `shutdown` method. Here's the updated `View` code:
```
public abstract class View extends JFrame {
protected Controller controller;
public View(Controller controller) {
this.controller = controller;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
controller.shutdown();
}
});
}
public void close() {
setVisible(false);
dispose();
}
}
```
I'll run the tests using the `Test` class to make sure everything still works, and then I'll run the software using the `Start` class to check that closing the software correctly saves all the point coordinates in the file "points.txt" and that all the points from the previous run are correctly displayed.