《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案.docx
- 文档编号:882027
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:77
- 大小:32.87KB
《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案.docx
《《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案.docx(77页珍藏版)》请在冰点文库上搜索。
《Java语言程序设计基础篇》第10版梁勇著第三十章练习题答案
《Java语言程序设计(基础篇)》(第10版梁勇著)
第三十章练习题答案
30.1
importjavafx.application.Application;
importjavafx.stage.Stage;
importjavafx.scene.Scene;
importjavafx.scene.control.*;
publicclassExercise30_01extendsApplication{
privateTextAreata=newTextArea();
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
ta.setWrapText(true);
ThreadprintA=newThread(newPrintChar('a',100));
ThreadprintB=newThread(newPrintChar('b',100));
Threadprint100=newThread(newPrintNum(100));
//Startthreads
print100.start();
printA.start();
printB.start();
//Createasceneandplacethepaneinthestage
Scenescene=newScene(ta,250,150);
primaryStage.setTitle("ConcurrentOutput");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
//Thethreadclassforprintingaspecifiedcharacterinspecifiedtimes
classPrintCharimplementsRunnable{
privatecharcharToPrint;//thecharactertoprint
privateinttimes;//thetimestorepeat
//Thethreadclassconstructor
publicPrintChar(charc,intt){
charToPrint=c;
times=t;
}
//overridetherun()methodtotellthesystemwhatthethreadwilldo
publicvoidrun(){
for(inti=1;i ta.appendText(charToPrint+""); } } //Thethreadclassforprintingnumberfrom1tonforagivenn. classPrintNumimplementsRunnable{ privateintlastNum; publicPrintNum(inti){ lastNum=i; } publicvoidrun(){ for(inti=1;i<=lastNum;i++) ta.appendText(""+i); } } /** *ThemainmethodisonlyneededfortheIDEwithlimited *JavaFXsupport.Notneededforrunningfromthecommandline. */ publicstaticvoidmain(String[]args){ launch(args); } } 30.1附加 importjavafx.application.Application; importjavafx.application.Platform; importjavafx.geometry.Pos; importjavafx.stage.Stage; importjavafx.scene.Scene; importjavafx.scene.control.Label; importjavafx.scene.image.Image; importjavafx.scene.image.ImageView; importjavafx.scene.layout.BorderPane; importjavafx.scene.layout.Pane; importjavafx.scene.paint.Color; importjavafx.scene.shape.Line; importjavafx.scene.shape.Rectangle; publicclassExercise30_01ExtraextendsApplication{ publicstaticfinalintSIZE=8;//Thesizeofthechessboard //queensareplacedat(i,queens[i]) //-1indicatesthatnoqueeniscurrentlyplacedintheithrow //Initially,placeaqueenat(0,0)inthe0throw privateint[]queens={-1,-1,-1,-1,-1,-1,-1,-1}; privateLabellblStatus=newLabel(); //Displaychessboard ChessBoardchessBoard=newChessBoard(); intk=0; @Override//OverridethestartmethodintheApplicationclass publicvoidstart(StageprimaryStage){ BorderPanepane=newBorderPane(); pane.setTop(lblStatus); BorderPane.setAlignment(lblStatus,Pos.CENTER); pane.setCenter(chessBoard); //Createasceneandplaceitinthestage Scenescene=newScene(pane,50*SIZE,55*SIZE); primaryStage.setTitle("ExtraExercise30_01");//Setthestagetitle primaryStage.setScene(scene);//Placethesceneinthestage primaryStage.show();//Displaythestage newThread(()->{ while(true){ search(); Platform.runLater(()->chessBoard.repaint()); if(k==-1){ Platform.runLater(()->lblStatus.setText("Nosolutionfound")); break; } elseif(k==8){ Platform.runLater(()->lblStatus.setText("Asolutionfound")); break; } try{ Thread.sleep(1000); } catch(Exceptionex){ } } }).start(); chessBoard.repaint(); } /**Searchforasolution*/ privatebooleansearch(){ //k-1indicatesthenumberofqueensplacedsofar //Wearelookingforapositioninthekthrowtoplaceaqueen if(k>=0&&k //Findapositiontoplaceaqueeninthekthrow intj=findPosition(k); if(j<0){ queens[k]=-1; k--;//backtracktothepreviousrow }else{ queens[k]=j; k++; } } if(k==-1) returnfalse;//Nosolution else returntrue;//Asolutionisfound } publicintfindPosition(intk){ intstart=queens[k]+1;//Searchforanewplacement for(intj=start;j if(isValid(k,j)) returnj;//(k,j)istheplacetoputthequeennow } return-1; } /**Returntrueifaqueencanbeplacedat(row,column)*/ publicbooleanisValid(introw,intcolumn){ for(inti=1;i<=row;i++) if(queens[row-i]==column//Checkcolumn ||queens[row-i]==column-i//Checkupleftdiagonal ||queens[row-i]==column+i)//Checkuprightdiagonal returnfalse;//Thereisaconflict returntrue;//Noconflict } classChessBoardextendsPane{ privateImagequeenImage=newImage("image/queen.jpg"); ChessBoard(){ setStyle("-fx-border-color: black"); } doublew=400; doubleh=400; publicvoidrepaint(){ getChildren().clear(); //Highlightthecurrentrow Rectanglerectangle=newRectangle(0,k*h/SIZE, w,h/SIZE); rectangle.setFill(Color.YELLOW); getChildren().add(rectangle); //Paintthequeens for(inti=0;i intj=queens[i];//Thepositionofthequeeninrowi ImageViewimageView=newImageView(queenImage); imageView.setX(j*w/SIZE); imageView.setY(i*h/SIZE); imageView.setFitWidth(w/SIZE); imageView.setFitHeight(h/SIZE); getChildren().add(imageView); } //Drawthehorizontalandverticallines for(inti=1;i getChildren().addAll(newLine(0,i*h/SIZE, w,i*h/SIZE),newLine(i*w/SIZE,0, i*w/SIZE,h)); } } } /** *ThemainmethodisonlyneededfortheIDEwithlimited *JavaFXsupport.Notneededforrunningfromthecommandline. */ publicstaticvoidmain(String[]args){ launch(args); } } 30.2 importjavafx.animation.KeyFrame; importjavafx.animation.Timeline; importjavafx.application.Application; importjavafx.scene.Scene; importjavafx.scene.input.KeyCode; importjavafx.scene.layout.Pane; importjavafx.scene.paint.Color; importjavafx.scene.shape.Circle; importjavafx.scene.shape.Polygon; importjavafx.scene.shape.Rectangle; importjavafx.stage.Stage; importjavafx.util.Duration; importjavafx.application.Platform; publicclassExercise30_02extendsApplication{ @Override//OverridethestartmethodintheApplicationclass publicvoidstart(StageprimaryStage){ CarPanecar=newCarPane(); //Createasceneandplaceitinthestage Scenescene=newScene(car,200,200); primaryStage.setTitle("Exercise30_02");//Setthestagetitle primaryStage.setScene(scene);//Placethesceneinthestage primaryStage.show();//Displaythestage scene.widthProperty().addListener(e->car.setW(car.getWidth())); scene.heightProperty().addListener(e->car.setH(car.getHeight())); car.setOnMousePressed(e->car.suspend()); car.setOnMouseReleased(e->car.resume()); car.requestFocus(); car.setOnKeyPressed(e->{ if(e.getCode()==KeyCode.UP){ car.faster(); } elseif(e.getCode()==KeyCode.DOWN){ car.slower(); } }); } /** *ThemainmethodisonlyneededfortheIDEwithlimited *JavaFXsupport.Notneededforrunningfromthecommandline. */ publicstaticvoidmain(String[]args){ launch(args); } } classCarPaneextendsPane{ privatedoublew=200; privatedoubleh=200; privatedoublebaseX=0; privatedoublebaseY=h; privateCirclec1=newCircle(baseX+10+5,baseY-10+5,5); privateCirclec2=newCircle(baseX+30+5,baseY-10+5,5); privateRectanglecarBody=newRectangle(baseX,baseY-20,50,10); privatePolygoncarTop=newPolygon(baseX+10,baseY-20, baseX+20,baseY-30,baseX+30,baseY-30, baseX+40,baseY-20); privateintsleepTime=50; privateThreadthread=newThread(()->{ try{ while(true){ Platform.runLater(()->move()); Thread.sleep(sleepTime); } } catch(InterruptedExceptionex){ } }); publicCarPane(){ carBody.setFill(Color.GREEN); carTop.setFill(Color.RED); this.getChildren().addAll(c1,c2,carBody,carTop); thread.start(); } publicvoidsuspend(){ thread.suspend(); } publicvoidresume(){ thread.resume(); } publicvoidfaster(){ if(sleepTime>1) sleepTime--; } publicvoidslower(){ sleepTime++; } publicvoidmove(){ if(baseX>w) baseX=-20; else baseX+=1; setValues(); } publicvoidsetValues(){ c1.setCenterX(baseX+10+5); c1.setCenterY(baseY-10+5); c2.setCenterX(baseX+30+5); c2.setCenterY(baseY-10+5); carBody.setX(baseX); carBody.setY(baseY-20); carTop.getPoints().clear(); carTop.getPoints().addAll(baseX+10,baseY-20, baseX+20,baseY-30,baseX+30,baseY-30, baseX+40,baseY-20); } publicvoidsetW(doublew){ this.w=w; setValues(); } publicvoidsetH(doubleh){ this.h=h; baseY=h; setValues(); } } 30.3 importjavafx.animation.PathTransition; importjavafx.application.Application; importjavafx.scene.Scene; importjavafx.scene.image.ImageView; importjavafx.scene.layout.Pane; importjavafx.scene.shape.Line; importjavafx.stage.Stage; importjavafx.util.Duration; importjavafx.application.Platform; publicclassExercise30_03extendsApplication{ @Override//OverridethestartmethodintheApplicationclass publicvoidstart(StageprimaryStage){ //Createapane Panepane=newPane(); //Addanimageviewandaddittopane ImageViewimageView=newImageView("image/us.gif"); pane.getChildren().ad
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言程序设计基础篇 Java语言程序设计基础篇第10版 梁勇 著第三十章练习题答案 Java 语言程序设计 基础 10 第三十 练习题 答案
文档标签
- java第十答案doc
- 第10章排序答案10排序
- Java语言程序设计讲义第十
- 第三章MATLAB的程序设计基础第三
- 第三章MATLAB程序设计基础第三MATLAB
- java语言程序设计基础第十
- Java语言程序设计基础篇《Java语言程序设计基础篇》第10版梁勇著第二十五章练习题答案
- 语言第七习题答案
- 第七章MATLAB的GUI程序设计第七
- Java语言程序设计基础篇《Java语言程序设计基础篇》第10版梁勇著第二十七章练习题答案
- Java语言程序设计基础篇《Java语言程序设计基础篇》第10版梁勇著第二十一章练习题答案
- 第二第七章SAS程序及习题第二第七
- Java语言程序设计基础篇《Java语言程序设计基础篇》第10版梁勇著第七章练习题答案