write java.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 13:05:42 浏览: 117
Here is the updated code with the requested changes:
Model.java
```
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Model {
private ArrayList<Point> points;
public Model() {
points = new ArrayList<Point>();
loadPointsFromFile();
}
public void addPoint(int x, int y) {
Point p = new Point(x, y);
points.add(p);
}
public ArrayList<Point> getPoints() {
return points;
}
public void saveData() {
try {
FileWriter writer = new FileWriter("points.txt");
for (Point p : points) {
writer.write(Integer.toString(p.getX()) + "," + Integer.toString(p.getY()) + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadPointsFromFile() {
File file = new File("points.txt");
if (file.exists()) {
try {
FileReader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] data = line.split(",");
int x = Integer.parseInt(data[0]);
int y = Integer.parseInt(data[1]);
addPoint(x, y);
}
bufferedReader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
Controller.java
```
public abstract class Controller {
protected Model model;
protected View view;
public Controller(Model model, View view) {
this.model = model;
this.view = view;
}
protected abstract void init();
protected void shutdown() {
model.saveData();
System.exit(0);
}
}
```
View.java
```
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public abstract class View {
protected JFrame frame;
public View() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
shutdown();
}
});
}
public abstract void init();
public void show() {
frame.setVisible(true);
}
protected abstract void shutdown();
}
```
Test.java and Start.java remain unchanged.
To test the updated software, run the Test class to make sure all tests still pass. Then run the Start class and add some points to the canvas. Close the application and check that the points are correctly saved to the "points.txt" file. Re-run the Start class and check that the saved points are correctly loaded and displayed on the canvas.
阅读全文
相关推荐













