《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx
- 文档编号:7577112
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:90
- 大小:41.28KB
《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx
《《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第二十八章练习题答案.docx(90页珍藏版)》请在冰豆网上搜索。
《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.List
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)); } } Graph graph.printEdges(); AbstractGraph 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}}; MyGraph AbstractGraph System.out.println(graph.getConnectedComponents()); } publicstaticclassMyGraph /**Constructanemptygraph*/ publicMyGraph(){ } /**Constructagraphfromedgesandverticesstoredinarrays*/ publicMyGraph(int[][]edges,V[]vertices){ super(vertices,edges); } /**ConstructagraphfromedgesandverticesstoredinList*/ publicMyGraph(List super(vertices,edges); } /**Constructagraphforintegervertices0,1,2andedgelist*/ publicMyGraph(List super(edges,numberOfVertices); } /**Constructagraphfromintegervertices0,1,andedgearray*/ publicMyGraph(int[][]edges,intnumberOfVertices){ super(edges,numberOfVertices); } publicList List List 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.List 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)); } } Graph graph.printEdges(); AbstractGraph java.util.List 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.List .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>getConnectedComponents(){
>list=newArrayList
>();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言程序设计基础篇 Java语言程序设计基础篇第10版 梁勇 著第二十八章练习题答案 Java 语言程序设计 基础 10 第二 十八 练习题 答案