帮我根据以下要求: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).修改下述代码:public class Controller { protected Model m; public Controller(Model m) { this.m = m; } } import java.awt.Point; public class ControllerClicks extends Controller { public ControllerClicks(Model m) { super(m); } public void mouseClicked(Point p) { m.addPoint(p); } public void resetClicked() { m.clearAllPoints(); } public void undoClicked() { m.deleteLastPoint(); } }
时间: 2024-01-06 21:04:02 浏览: 34
修改后的代码如下:
```
public class Controller {
protected Model m;
public Controller(Model m) {
this.m = m;
}
protected void shutdown() {
m.saveData();
System.exit(0);
}
}
import java.awt.Point;
public class ControllerClicks extends Controller {
public ControllerClicks(Model m) {
super(m);
}
public void mouseClicked(Point p) {
m.addPoint(p);
}
public void resetClicked() {
m.clearAllPoints();
}
public void undoClicked() {
m.deleteLastPoint();
}
}
```
在 Controller 类中添加了一个名为 shutdown 的 protected 方法,该方法会调用 Model 类的 saveData 方法,然后手动终止程序。在 ControllerClicks 类中没有进行任何修改。
相关问题
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.
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.
ava.lang.Error: Unresolved compilation problem: The method select(UserDto) of type UserServiceImpl must override a superclass method
这是一个编译错误,出现这个错误是因为你的代码中的方法没有正确地覆盖父类或接口中的方法。在Java中,当你尝试覆盖一个父类或接口中的方法时,你必须确保方法的签名和返回类型与父类或接口中的方法完全一致。如果方法签名或返回类型不一致,编译器会报出类似于你遇到的错误。
要解决这个错误,你应该检查你的代码,确保你的方法签名和返回类型与父类或接口中的方法完全一致。如果你是在实现一个接口,你可以尝试使用`@Override`注释来帮助你检查是否正确地覆盖了接口中的方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)