《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案Word格式.docx
- 文档编号:1076394
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:77
- 大小:32.87KB
《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案Word格式.docx
《《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案Word格式.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第三十章练习题答案Word格式.docx(77页珍藏版)》请在冰点文库上搜索。
);
//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<
times;
i++)
ta.appendText(charToPrint+"
"
//Thethreadclassforprintingnumberfrom1tonforagivenn.
classPrintNumimplementsRunnable{
privateintlastNum;
publicPrintNum(inti){
lastNum=i;
=lastNum;
ta.appendText("
"
+i);
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
30.1附加
importjavafx.application.Platform;
importjavafx.geometry.Pos;
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;
BorderPanepane=newBorderPane();
pane.setTop(lblStatus);
BorderPane.setAlignment(lblStatus,Pos.CENTER);
pane.setCenter(chessBoard);
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,50*SIZE,55*SIZE);
ExtraExercise30_01"
newThread(()->
{
while(true){
search();
Platform.runLater(()->
chessBoard.repaint());
if(k==-1){
lblStatus.setText("
Nosolutionfound"
));
break;
elseif(k==8){
Asolutionfound"
try{
Thread.sleep(1000);
catch(Exceptionex){
}).start();
chessBoard.repaint();
/**Searchforasolution*/
privatebooleansearch(){
//k-1indicatesthenumberofqueensplacedsofar
//Wearelookingforapositioninthekthrowtoplaceaqueen
if(k>
=0&
&
k<
SIZE){
//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<
SIZE;
j++){
if(isValid(k,j))
returnj;
//(k,j)istheplacetoputthequeennow
return-1;
/**Returntrueifaqueencanbeplacedat(row,column)*/
publicbooleanisValid(introw,intcolumn){
for(inti=1;
=row;
if(queens[row-i]==column//Checkcolumn
||queens[row-i]==column-i//Checkupleftdiagonal
||queens[row-i]==column+i)//Checkuprightdiagonal
//Thereisaconflict
//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
getChildren().addAll(newLine(0,i*h/SIZE,
w,i*h/SIZE),newLine(i*w/SIZE,0,
i*w/SIZE,h));
}
30.2
importjavafx.animation.KeyFrame;
importjavafx.animation.Timeline;
importjavafx.scene.input.KeyCode;
importjavafx.scene.shape.Circle;
importjavafx.scene.shape.Polygon;
importjavafx.util.Duration;
publicclassExercise30_02extendsApplication{
publicvoidstart(StageprimaryStage){
CarPanecar=newCarPane();
Scenescene=newScene(car,200,200);
Exercise30_02"
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();
});
}
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(()->
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;
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,
publicvoidsetW(doublew){
this.w=w;
publicvoidsetH(doubleh){
this.h=h;
baseY=h;
30.3
importjavafx.animation.PathTransition;
publicclassExercise30_03extendsApplication{
//Createapane
Panepane=newPane();
//Addanimageviewandaddittopane
ImageViewimageView=newImageView("
image/us.gif"
pane.getChildren().ad
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言程序设计基础篇 Java语言程序设计基础篇第10版 梁勇 著第三十章练习题答案 Java 语言程序设计 基础 10 第三十 练习题 答案