public class test { static int m_data = 0; public int mb_method() { m_data++;
时间: 2024-05-11 21:17:56 浏览: 126
在你的代码中,mb_method() 方法没有返回值,而且 m_data 没有被赋值,只是被调用了。如果你想让 m_data 增加1,可以将代码修改为:
```
public class test {
static int m_data = 0;
public void mb_method() {
m_data++;
}
}
```
这样,每次调用 mb_method() 方法时,m_data 的值就会加1。
相关问题
根据以下要求: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<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(); } }
public class Model {
private ArrayList<Point> points;
private ArrayList<ModelListener> listeners;
private static final String DB_URL = "jdbc:mysql://localhost/java";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "";
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
// Read points from database
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM points";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int x = rs.getInt("x");
int y = rs.getInt("y");
Point p = new Point(x, y);
points.add(p);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException 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 database
}
public void clearAllPoints() {
points.clear();
notifyListeners(); // points changed so notify the listeners.
saveData(); // save empty list to database
}
public void deleteLastPoint() {
if (points.size() > 0) {
points.remove(points.size() - 1);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save updated list to database
}
}
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 {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
String sql = "DELETE FROM points";
stmt.executeUpdate(sql);
for (Point p : points) {
sql = "INSERT INTO points (x, y) VALUES (" + p.x + ", " + p.y + ")";
stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package project05demo1; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Test { public static void main(String[] args) throws FileNotFoundException, InvalidTriangleException{ start(); } public static void start() throws FileNotFoundException, InvalidTriangleException { //create an empty list of shapes and save the reference. ArrayList<Shape> ShapeList=new ArrayList<Shape>(); create(ShapeList); //Pass the reference to create method that fills the list. //create(ShapeList); } public static void create(ArrayList<Shape> ShapeList)throws FileNotFoundException,InvalidTriangleException{ //create shape objects using data from an input file. File file=new File("ShapeData.txt"); Scanner InputFile=new Scanner(file); double a,b,c; while(InputFile.hasNext()) { while(InputFile.nextLine()!="Triangle"){ String RectangleName; RectangleName=InputFile.nextLine(); a=InputFile.nextDouble(); b=InputFile.nextDouble(); ShapeList.add(new Rectangle(RectangleName,a,b)); } while(InputFile.nextLine()!="Circle") { String TriangleName; TriangleName=InputFile.nextLine(); a=InputFile.nextDouble(); b=InputFile.nextDouble(); c=InputFile.nextDouble(); ShapeList.add(new Triangle(TriangleName,a,b,c)); } while(InputFile.hasNextLine()) { String CircleName; CircleName=InputFile.nextLine(); a=InputFile.nextDouble(); ShapeList.add(new Circle(CircleName,a)); } } InputFile.close(); } public static void display(ArrayList<Shape> ShapeList) { for(int i=0;i<ShapeList.size();i++) { System.out.println(ShapeList.get(i)); } } }
这是一个Java程序,用于读取一个包含不同形状数据的文件,并创建相应的形状对象。程序包含三个类:Shape、Rectangle、Triangle和Circle。其中Shape是所有形状的基类,而Rectangle、Triangle和Circle是Shape的子类。程序使用ArrayList来存储所有创建的形状对象,并提供了一个display方法,用于打印所有形状对象的信息。
该程序还涉及到一个InvalidTriangleException异常类,用于在创建Triangle对象时检查是否为有效的三角形。程序的主方法是start方法,它调用create方法来读取文件并创建形状对象,然后调用display方法来打印形状对象的信息。
阅读全文