import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
/** Use the old Graph.java, AbstractGraph.java, UnweightedGraph.java. Need to replace it in the future.*/
public class Exercise28_18 extends Application {
private Model model = new Model();
private static final int SIZE = 8;
private int startX = 0;
private int startY = 0;
private ArrayList<Point2D> moveHistory = null;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
ChessBoard board = new ChessBoard();
pane.setCenter(board);
Button btPath = new Button("Path");
Button btCycle = new Button("Cycle");
HBox hBox = new HBox(5);
hBox.getChildren().addAll(btPath, btCycle);
hBox.setAlignment(Pos.CENTER);
pane.setBottom(hBox);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 250, 250);
primaryStage.setTitle("Exercise28_18"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
board.repaint();
btPath.setOnAction(e -> {
resetMoveHistory();
List<Integer> list = model.findHamiltonianPath(startX, startY);
for (int i = 0; i < list.size(); i++) {
addMoveHistory(list.get(i) / 8, list.get(i) % 8);
}
board.repaint();
});
btCycle.setOnAction(e -> {
resetMoveHistory();
List<Integer> list = model.findHamiltonianCycle(startX, startY);
for (int i = 0; i < list.size(); i++) {
addMoveHistory(list.get(i) / 8, list.get(i) % 8);
}
board.repaint();
});
scene.widthProperty().addListener(ov -> board.repaint());
scene.heightProperty().addListener(ov -> board.repaint());
}
public void resetMoveHistory() {
moveHistory = new ArrayList(63);
}
public void addMoveHistory(int x, int y) {
moveHistory.add(new Point2D(x, y));
}
public void removeLastMoveHistory() {
moveHistory.remove(moveHistory.size() - 1);
}
private class ChessBoard extends Pane {
ImageView knightImageView = new ImageView("image/knight.jpg");
ChessBoard() {
this.setOnMouseClicked(e -> {
startX = (int) (e.getX() / (getWidth() / SIZE));
startY = (int) (e.getY() / (getHeight() / SIZE));
resetMoveHistory();
repaint();
});
}
protected void repaint() {
// Clear previous drawing
this.getChildren().clear();
// Add the Knight image
this.getChildren().add(knightImageView);
knightImageView.setX(startX * getWidth() / SIZE);
knightImageView.setY(startY * getHeight() / SIZE);
knightImageView.setFitWidth(getWidth() / SIZE);
knightImageView.setFitHeight(getHeight() / SIZE);
// Draw the lines
for (int i = 1; i <= SIZE; i++) {
this.getChildren().add(
new Line(0, i * getHeight() / SIZE, getWidth(), i * getHeight() / SIZE));
this.getChildren().add(
new Line(i * getWidth() / SIZE, 0, i * getWidth() / SIZE, getHeight()));
}
// Draw the moves
if (moveHistory != null) {
for (int i = 1; i < moveHistory.size(); i++) {
Point2D p1 = moveHistory.get(i - 1);
Point2D p2 = moveHistory.get(i);
this.getChildren().add(
new Line(p1.getX() * (getWidth() / SIZE) + getWidth() / SIZE / 2,
p1.getY() * (getHeight() / SIZE) + (getHeight() / SIZE / 2),
p2.getX() * (getWidth() / SIZE) + getWidth() / SIZE / 2,
p2.getY() * (getHeight() / SIZE) + getHeight() / SIZE / 2));
}
}
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
/**
* Edge inner class inside the AbstractGraph class
*/
public static class Edge {
public int u; // Starting vertex of the edge
public int v; // Ending vertex of the edge
/**
* Construct an edge for (u, v)
*/
public Edge(int u, int v) {
this.u = u;
this.v = v;
}
}
public interface Graph {
/**
* Return the number of vertices in the graph
*/
public int getSize();
/**
* Return the vertices in the graph
*/
public Object[] getVertices();
/**
* Return the object for the specified vertex index
*/
public Object getVertex(int index);
/**
* Return the index for the specified vertex object
*/
public int getIndex(Object v);
/**
* Return the neighbors of vertex v
*/
public java.util.List getNeighbors(int v);
/**
* Return the degree for a specified vertex
*/
public int getDegree(int v);
/**
* Return the adjacency matrix
*/
public int[][] getAdjacencyMatrix();
/**
* Print the adjacency matrix
*/
public void printAdjacencyMatrix();
/**
* Print the edges
*/
public void printEdges();
/**
* Obtain a depth-first search tree
*/
public AbstractGraph.Tree dfs(int v);
/**
* Obtain a breadth-first search tree
*/
public AbstractGraph.Tree bfs(int v);
/**
* Obtain a depth-first search tree
*/
public List<Integer> getHamiltonianPath(int v);
/**
* Obtain a depth-first search tree
*/
public List<Integer> getHamiltonianCycle(int v);
/** Return a Hamiltonian cycle
* Return null if the graph does not contain a Hamiltonian cycle */
public List<Integer> getHamiltonianCycle();
}
public abstract class AbstractGraph implements Graph {
protected Object[] vertices; // Store vertices
protected LinkedList<Integer>[] neighbors; // Adjacency lists
/**
* Construct a graph from edges and vertices stored in arrays
*/
protected AbstractGraph(int[][] edges, Object[] vertices) {
this.vertices = vertices;
createAdjacencyLists(edges, vertices.length);
}
/**
* Construct a graph from edges and vertices stored in ArrayList
*/
protected AbstractGraph(List<Edge> edges, List<Object> vertices) {
this.vertices = vertices.toArray();
createAdjacencyLists(edges, vertices.size());
}
/**
* Construct a graph from edges and vertices in ArrayList
*/
protected AbstractGraph(List<Edge> edges, int numberOfVertices) {
vertices = new Integer[numberOfVertices]; // Create vertices
for (int i = 0; i < numberOfVertices; i++) {
vertices[i] = new Integer(i); // vertices is {0, 1, 2, ...}
}
createAdjacencyLists(edges, numberOfVertices);
}
/**
* Construct a graph from edges in array
*/
protected AbstractGraph(int[][] edges, int numberOfVertices) {
vertices = new Integer[numberOfVertices]; // Create vertices
for (int i = 0; i < numberOfVertices; i++) {
vertices[i] = new Integer(i); // vertices is {0, 1, 2, ...}
}
createAdjacencyLists(edges, numberOfVertices);
}
/**
* Create adjacency lists for each vertex
*/
@SuppressWarnings("unchecked")
private void createAdjacencyLists(
没有合适的资源?快使用搜索试试~ 我知道了~
Java语言程序设计第十版编程练习题答案
共2148个文件
java:1026个
gif:336个
png:183个
4星 · 超过85%的资源 需积分: 50 125 下载量 51 浏览量
2018-02-25
19:56:33
上传
评论 22
收藏 38.69MB RAR 举报
温馨提示
本书特点 基础篇介绍基础内容,进阶篇介绍高级内容,便于教师按需选择理想的教材。 全面整合了Java 8的特性,对全书的内容进行了修订和更新,以反映Java程序设计的最新技术发展。 ************学校用了这本书的基础篇做教材,后续自己学习买了高级篇,但是没有习题答案还是很苦恼,当时在网上找了很久,非常感谢当时有人分享了此资源,想到肯定有更多的初学者需要此资源便立刻上传了,希望能帮助到你们。************
资源推荐
资源详情
资源评论
收起资源包目录
Java语言程序设计第十版编程练习题答案 (2148个子文件)
minute22.au 76KB
minute8.au 74KB
minute42.au 12KB
minute37.au 12KB
hour11.au 12KB
minute20.au 12KB
am.au 12KB
hour5.au 12KB
minute52.au 12KB
minute5.au 12KB
minute54.au 12KB
minute43.au 12KB
minute11.au 12KB
minute40.au 12KB
minute26.au 12KB
minute31.au 12KB
minute30.au 12KB
hour4.au 12KB
hour3.au 12KB
hour9.au 12KB
minute27.au 12KB
minute21.au 12KB
minute35.au 12KB
minute23.au 12KB
minute0.au 12KB
minute3.au 12KB
minute36.au 12KB
minute9.au 12KB
minute58.au 12KB
pm.au 12KB
minute25.au 12KB
minute50.au 12KB
minute17.au 12KB
minute41.au 12KB
hour0.au 12KB
minute1.au 12KB
hour7.au 12KB
minute57.au 12KB
minute34.au 12KB
minute6.au 12KB
hour2.au 12KB
minute10.au 12KB
minute19.au 12KB
minute48.au 12KB
minute38.au 12KB
minute39.au 12KB
hour8.au 12KB
minute29.au 12KB
minute16.au 12KB
minute46.au 12KB
minute13.au 12KB
minute28.au 12KB
minute33.au 12KB
minute56.au 12KB
minute15.au 12KB
hour10.au 12KB
minute2.au 12KB
minute32.au 12KB
minute45.au 12KB
minute59.au 12KB
minute12.au 12KB
minute44.au 12KB
minute49.au 12KB
minute18.au 12KB
minute55.au 12KB
minute47.au 12KB
minute4.au 12KB
minute24.au 12KB
minute7.au 12KB
minute14.au 12KB
hour1.au 12KB
minute53.au 12KB
hour6.au 12KB
minute51.au 12KB
malaysia.bmp 92KB
malaysia.bmp 92KB
malaysia.bmp 92KB
canada.bmp 17KB
uk.bmp 17KB
canada.bmp 17KB
uk.bmp 17KB
uk.bmp 17KB
canada.bmp 17KB
usa.bmp 16KB
usa.bmp 16KB
usa.bmp 16KB
germany.bmp 14KB
germany.bmp 14KB
germany.bmp 14KB
india.bmp 13KB
france.bmp 13KB
france.bmp 13KB
india.bmp 13KB
india.bmp 13KB
france.bmp 13KB
norway.bmp 3KB
norway.bmp 3KB
norway.bmp 3KB
Registration.class 13KB
DisplayStudent.class 12KB
共 2148 条
- 1
- 2
- 3
- 4
- 5
- 6
- 22
资源评论
- z997924582022-01-02我的答案代码缺少了26章和33章,前面下载几个资源也是缺失这部分,在楼主这里找到了33章的答案代码,非常感谢,请问楼主还有26章的答案代码吗?
- alearner1262018-07-28非常好分享,真的有用的,我刚刚买了这本书的基础篇,第十版,再次感谢!!!I_AM_good_boy2019-04-05学校用了这本书的基础篇做教材,后续自己学习买了高级篇,但是没有习题答案还是很苦恼,当时在网上找了很久,非常感谢当时有人分享了此资源,想到肯定有更多的初学者需要此资源便立刻上传了,希望能帮助到你们。
- Bryceooo2018-06-12其实我现在还没看具体代码,但是挺全的。本想试完代码后再来评论的(我可能会忘了~~~~),但是其实我也是自学java需要代码来参考,总之谢谢你的分享咯。之所以文件这么大是因为博主放了很多无关的图片,代码还是有的。I_AM_good_boy2019-04-05学校用了这本书的基础篇做教材,后续自己学习买了高级篇,但是没有习题答案还是很苦恼,当时在网上找了很久,非常感谢当时有人分享了此资源,想到肯定有更多的初学者需要此资源便立刻上传了,希望能帮助到你们。
I_AM_good_boy
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功