《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx

上传人:b****6 文档编号:7577112 上传时间:2023-01-25 格式:DOCX 页数:90 大小:41.28KB
下载 相关 举报
《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx_第1页
第1页 / 共90页
《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx_第2页
第2页 / 共90页
《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx_第3页
第3页 / 共90页
《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx_第4页
第4页 / 共90页
《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx_第5页
第5页 / 共90页
点击查看更多>>
下载资源
资源描述

《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx

《《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx(90页珍藏版)》请在冰豆网上搜索。

《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx

《Java语言程序设计基础篇》第10版梁勇著第二十八章练习题答案

《Java语言程序设计(基础篇)》(第10版梁勇著)

第二十八章练习题答案

28.1

publicclassExercise28_01{

publicstaticvoidmain(String[]args)throwsException{

java.util.Scannerinput=newjava.util.Scanner(System.in);

System.out.print("Enterafilename:

");

java.io.Filefile=newjava.io.File(input.nextLine());

if(!

file.exists()){

System.out.println("Filedoesnotexist");

System.exit

(1);

}

java.util.ScannerinFile=newjava.util.Scanner(file);

//Readthenumberofvertices

Strings=inFile.nextLine();

intnumberOfVertices=Integer.parseInt(s);

System.out.println("Thenumberofverticesis"

+numberOfVertices);

java.util.Listlist=newjava.util.ArrayList<>();

while(inFile.hasNext()){

s=inFile.nextLine();

String[]tokens=s.split("[\\s+]");

intstartingVertex=Integer.parseInt(tokens[0].trim());

for(inti=1;i

intadjacentVertex=Integer.parseInt(tokens[i].trim());

list.add(newAbstractGraph.Edge(startingVertex,adjacentVertex));

}

}

Graphgraph=newUnweightedGraph<>(list,numberOfVertices);

graph.printEdges();

AbstractGraph.Treetree=graph.dfs(0);

if(tree.getNumberOfVerticesFound()==numberOfVertices)

System.out.println("Thegraphisconnected");

else

System.out.println("Thegraphisnotconnected");

}

}

 

28.2

publicclassExercise28_02{

publicstaticvoidmain(String[]args)throwsjava.io.FileNotFoundException{

String[]vertices={"Seattle","SanFrancisco","LosAngeles",

"Denver","KansasCity","Chicago","Boston","NewYork",

"Atlanta","Miami","Dallas","Houston"};

int[][]edges={

{0,1},{0,3},{0,5},

{1,0},{1,2},{1,3},

{2,1},{2,3},{2,4},{2,10},

{3,0},{3,1},{3,2},{3,4},{3,5},

{4,2},{4,3},{4,5},{4,7},{4,8},{4,10},

{5,0},{5,3},{5,4},{5,6},{5,7},

{6,5},{6,7},

{7,4},{7,5},{7,6},{7,8},

{8,4},{8,7},{8,9},{8,10},{8,11},

{9,8},{9,11},

{10,2},{10,4},{10,8},{10,11},

{11,8},{11,9},{11,10}

};

java.io.PrintWriteroutput=newjava.io.PrintWriter("Exercise28_02.txt");

intnumberOfVertices=vertices.length;

output.println(numberOfVertices);

for(intstartingVertex=0;startingVertex

output.print(startingVertex+"");

intcount=0;

for(inti=0;i

if(edges[i][0]==startingVertex){

count++;

if(count==1)

output.print(edges[i][1]);

else

output.print(""+edges[i][1]);

}

}

if(count>0)output.println();

}

System.out.println("Done!

");

output.close();

}

}

 

28.4

importjava.util.*;

publicclassExercise28_04{

publicstaticvoidmain(String[]args){

String[]vertices={"Seattle","SanFrancisco","LosAngeles","Denver",

"KansasCity","Chicago","Boston","NewYork","Atlanta","Miami",

"Dallas","Houston","Savannah","Charlston"};

int[][]edges={{0,1},{0,3},{0,5},{1,0},{1,2},

{1,3},{2,1},{2,3},{2,4},{2,10},{3,0},{3,1},

{3,2},{3,4},{3,5},{4,2},{4,3},{4,5},{4,7},

{4,8},{4,10},{5,0},{5,3},{5,4},{5,6},{5,7},

{6,5},{6,7},{7,4},{7,5},{7,6},{7,8},{8,4},

{8,7},{8,9},{8,10},{8,11},{9,8},{9,11},

{10,2},{10,4},{10,8},{10,11},{11,8},{11,9},

{11,10},{12,13},{13,12}};

MyGraphgraph=newMyGraph(edges,vertices);

AbstractGraph.Treedfs=graph.dfs(graph.getIndex("Chicago"));

System.out.println(graph.getConnectedComponents());

}

publicstaticclassMyGraphextendsUnweightedGraph{

/**Constructanemptygraph*/

publicMyGraph(){

}

/**Constructagraphfromedgesandverticesstoredinarrays*/

publicMyGraph(int[][]edges,V[]vertices){

super(vertices,edges);

}

/**ConstructagraphfromedgesandverticesstoredinList*/

publicMyGraph(Listvertices,Listedges){

super(vertices,edges);

}

/**Constructagraphforintegervertices0,1,2andedgelist*/

publicMyGraph(Listedges,intnumberOfVertices){

super(edges,numberOfVertices);

}

/**Constructagraphfromintegervertices0,1,andedgearray*/

publicMyGraph(int[][]edges,intnumberOfVertices){

super(edges,numberOfVertices);

}

publicList>getConnectedComponents(){

List>list=newArrayList>();

ListvertexIndices=newArrayList();

for(inti=0;i

vertexIndices.add(i);

while(vertexIndices.size()>0){

Treetree=dfs(vertexIndices.get(0));

list.add(tree.getSearchOrder());

vertexIndices.removeAll(tree.getSearchOrder());

}

returnlist;

}

}

}

 

28.10

publicclassExercise28_10{

publicstaticvoidmain(String[]args)throwsException{

java.util.Scannerinput=newjava.util.Scanner(System.in);

System.out.print("Enterafilename:

");

java.io.Filefile=newjava.io.File(input.nextLine());

if(!

file.exists()){

System.out.println("Filedoesnotexist");

System.exit

(1);

}

System.out.print("Entertwovertices(integerindexes):

");

intv1=input.nextInt();

intv2=input.nextInt();

java.util.ScannerinFile=newjava.util.Scanner(file);

//Readthenumberofvertices

Strings=inFile.nextLine();

intnumberOfVertices=Integer.parseInt(s);

System.out.println("Thenumberofverticesis"+numberOfVertices);

java.util.Listlist=

newjava.util.ArrayList();

while(inFile.hasNext()){

s=inFile.nextLine();

String[]tokens=s.split("[\\s+]");

intv=Integer.parseInt(tokens[0].trim());

for(inti=1;i

intadjacentVertex=Integer.parseInt(tokens[i].trim());

list.add(newAbstractGraph.Edge(v,adjacentVertex));

}

}

Graphgraph=newUnweightedGraph<>(list,numberOfVertices);

graph.printEdges();

AbstractGraph.Treetree=graph.bfs(v1);

java.util.Listpath=tree.getPath(v2);

System.out.print("Thepathis");

for(inti=0;i

System.out.print(path.get(i)+"");

}

}

 

28.11

importjavafx.application.Application;

importjavafx.stage.Stage;

importjavafx.scene.Scene;

importjavafx.scene.control.Button;

importjavafx.scene.control.Label;

importjavafx.scene.layout.BorderPane;

importjavafx.scene.layout.HBox;

importjavafx.scene.layout.GridPane;

importjavafx.scene.paint.Color;

importjavafx.geometry.Pos;

importjavafx.scene.control.ScrollPane;

importjavafx.scene.text.Font;

publicclassExercise28_11extendsApplication{

//Createtheinitialboard

privateInitialNodePaneinitialNodePane=newInitialNodePane();

privateButtonbtSolve=newButton("Solve");

privateButtonbtStartOver=newButton("StartOver");

privateNineTailModelmodel=newNineTailModel();

@Override//OverridethestartmethodintheApplicationclass

publicvoidstart(StageprimaryStage){

BorderPanepane=newBorderPane();

//solutionPanelholdsasequenceofpanelsfordisplayingnodes

HBoxhBox=newHBox(5);

hBox.getChildren().addAll(btSolve,btStartOver);

hBox.setAlignment(Pos.CENTER);

HBoxsolutionPane=newHBox(5);

solutionPane.getChildren().add(initialNodePane);

pane.setCenter(newScrollPane(solutionPane));

pane.setBottom(hBox);

//Createasceneandplacethepaneinthestage

Scenescene=newScene(pane,650,250);

primaryStage.setTitle("Exercise28_11:

NineTailProblem");//Setthestagetitle

primaryStage.setScene(scene);//Placethesceneinthestage

primaryStage.show();//Displaythestage

//ListenerfortheSolvebutton

btSolve.setOnAction(e->{

solutionPane.getChildren().clear();

//Getashortestpath

java.util.Listlist=model

.getShortestPath(NineTailModel

.getIndex(initialNodePane.getNode()));

//Displaynodesintheshortestpath

for(inti=0;i

if(i!

=0){

solutionPane.getChildren().add(newNodePane(NineTailModel

.getNode(list.get(i)),NineTailModel

.getNode(list.get(i-1))));

}else{

solutionPane.getChildren().add(newNodePane(NineTailModel

.getNode(list.get(i))));

}

}

});

//ListenerfortheStartOverbutton

btStartOver.setOnAction(e->{

solutionPane.getChildren().clear();

solutionPane.getChildren().add(initialNodePane);//Displayinitialnode

});

}

/**

*ThemainmethodisonlyneededfortheIDEwithlimited

*JavaFXsupport.Notneededforrunningfromthecommandline.

*/

publicstaticvoidmain(String[]args){

launch(args);

}

/**

*Aninnerclassfordisplayinganodeonagridpane

*/

staticclassNodePaneextendsGridPane{

publicNodePane(char[]node){

this.setStyle("-fx-border-color:

black");

for(inti=0;i<9;i++){

add(newCell(node[i]+""),i%3,i/3);

}

}

publicNodePane(char[]newNode,char[]oldNode){

this.setStyle("-fx-border-color:

black");

for(inti=0;i<9;i++){

if(newNode[i]==oldNode[i]){

add(newCell(newNode[i]+"",Color.BLACK),i%3,i/3);

}else{

add(newCell(newNode[i]+"",Color.RED),i%3,i/3);

}

}

}

}

/**

*Aninnerclassfordisplayingacell

*/

staticclassCellextendsLabel{

publicCell(Strings){

this.setFont(newFont("Times",40));

setText(s);

}

publicCell(Strings,Colorcolor){

this.setFont(newFont("Times",40));

this.setTextFill(color);

setText(s);

}

}

/**

*Aninnerclassfordisplayingtheinitialnode

*/

staticclassInitialNodePaneextendsGridPane{

//Eachcellrepresentsacoin,whichcanbeflipped

ClickableCell[][]clickableCells=newClickableCell[3][3];

publi

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 实习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1