《Java语言程序设计基础篇》第10版梁勇著第十六章练习题答案.docx
《《Java语言程序设计基础篇》第10版梁勇著第十六章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版梁勇著第十六章练习题答案.docx(171页珍藏版)》请在冰点文库上搜索。
![《Java语言程序设计基础篇》第10版梁勇著第十六章练习题答案.docx](https://file1.bingdoc.com/fileroot1/2023-6/10/5102b503-6590-4016-aa3c-718c59cf180f/5102b503-6590-4016-aa3c-718c59cf180f1.gif)
《Java语言程序设计基础篇》第10版梁勇著第十六章练习题答案
Java语言程序设计(基础篇)》(第10版梁勇著)
第十六章练习题答案
16.1
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.control.RadioButton;
importjavafx.scene.control.ToggleGroup;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.HBox;
importjavafx.scene.layout.Pane;
importjavafx.scene.paint.Color;
importjavafx.scene.text.Font;
importjavafx.scene.text.Text;
importjavafx.stage.Stage;
extendsApplication{
publicclassExercise16_01
privatedoublepaneWidth=500;
privatedoublepaneHeight=150;
@Override//OverridethestartmethodintheApplicationclasspublicvoidstart(StageprimaryStage){
Texttext=newText(20,40,"Programmingisfun");
text.setFont(newFont("Times",20));
PanepaneForText=newPane();
paneForText.getChildren().add(text);
paneForText.setStyle("-fx-border-color:
gray");
newRadioButton("Red");
newRadioButton("Green");
ToggleGroupgroup=newToggleGroup();
rbRed.setToggleGroup(group);rbYellow.setToggleGroup(group);rbBlack.setToggleGroup(group);rbBlack.setSelected(true);rbOrange.setToggleGroup(group);
rbGreen.setToggleGroup(group);
HBoxhBox=newHBox(5);
hBox.getChildren().addAll(rbRed,rbYellow,rbBlack,rbOrange,rbGreen);hBox.setAlignment(Pos.CENTER);
ButtonbtLeft=newButton("<=");
ButtonbtRight=newButton("=>");
HBoxhBoxForButtons=newHBox(5);
hBoxForButtons.getChildren().addAll(btLeft,btRight);hBoxForButtons.setAlignment(Pos.CENTER);
BorderPaneborderPane=newBorderPane();
borderPane.setTop(hBox);
borderPane.setCenter(paneForText);borderPane.setBottom(hBoxForButtons);
//Createasceneandplaceitinthestage
Scenescene=newScene(borderPane,paneWidth,paneHeight+40);
primaryStage.setTitle("Exercise16_01");//SetthestagetitleprimaryStage.setScene(scene);//PlacethesceneinthestageprimaryStage.show();//Displaythestage
rbRed.setOnAction(e->text.setStroke(Color.RED));rbYellow.setOnAction(e->text.setStroke(Color.YELLOW));
rbBlack.setOnAction(e->text.setStroke(Color.BLACK));rbOrange.setOnAction(e->text.setStroke(Color.ORANGE));
rbGreen.setOnAction(e->text.setStroke(Color.GREEN));
btLeft.setOnAction(e->text.setX(text.getX()-1));btRight.setOnAction(e->text.setX(text.getX()+1));
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
16.1附加
importjavafx.application.Application;
importjavafx.scene.Scene;
importjavafx.scene.layout.*;
importjavafx.scene.paint.Color;
importjavafx.scene.control.*;
importjavafx.stage.Stage;
importjavafx.geometry.Pos;
publicclassExercise16_01ExtraextendsApplication{finalstaticdoublePANEL_WIDTH=400;
finalstaticdoublePANEL_HEIGHT=140;
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
GridPanepaneForOriginal=
GridPanepaneForInversed=
newGridPane();newGridPane();
TextField[][]tfOriginal=
TextField[][]tfInversed=
newTextField[3][3];
newTextField[3][3];
for(inti=0;i<3;i++){for(intj=0;j<3;j++){paneForOriginal.add(tfOriginal[i][j]=tfOriginal[i][j].setPrefColumnCount(4);paneForInversed.add(tfInversed[i][j]=tfInversed[i][j].setPrefColumnCount(4);
}
}
newTextField(),j,i);
newTextField(),j,i);
BorderPanep1=newBorderPane();
p1.setCenter(paneForOriginal);p1.setTop(newLabel("OriginalMatrix"));
BorderPanep2=newBorderPane();p2.setCenter(paneForInversed);
finalLabellblStatus=newLabel("InversedMatrix");p2.setTop(lblStatus);
HBoxhBox=newHBox(5);hBox.getChildren().addAll(p1,p2);
BorderPanepane=newBorderPane();pane.setCenter(hBox);
ButtonbtGetInverse=
newButton("GetInverse"
);
pane.setBottom(btGetInverse);
BorderPane.setAlignment(btGetInverse,Pos.CENTER);
Scenescene=newScene(pane,PANEL_WIDTH,PANEL_HEIGHT);
primaryStage.setTitle("Exercise16_01");//SetthestagetitleprimaryStage.setScene(scene);//PlacethesceneinthestageprimaryStage.show();//Displaythestage
btGetInverse.setOnAction(e->{
double[][]A=newdouble[3][3];
for(inti=0;i<3;i++)
for(intj=0;j<3;j++){
A[i][j]=Double.parseDouble(tfOriginal[i][j].getText());
}
double[][]inverseA=Exercise08_03Extra.inverse(A);
if(inverseA==null)
lblStatus.setText("Inveredmatrix:
Noinversematrix");
else{
for(inti=0;i<3;i++)
for(intj=0;j<3;j++){tfInversed[i][j].setText(inverseA[i][j]+"");
}
});
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.*/
publicstaticvoidmain(String[]args){launch(args);
}
}
16.2
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.CheckBox;
importjavafx.scene.control.RadioButton;importjavafx.scene.control.ToggleGroup;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.HBox;
importjavafx.scene.layout.StackPane;
importjavafx.scene.paint.Color;
importjavafx.scene.shape.Circle;
importjavafx.scene.shape.Ellipse;
importjavafx.scene.shape.Rectangle;
importjavafx.stage.Stage;
publicclassExercise16_02extendsApplication{privatedoublepaneWidth=500;privatedoublepaneHeight=150;
@Override//OverridethestartmethodintheApplicationclasspublicvoidstart(StageprimaryStage){
StackPanepane=newStackPane();
Circlecircle=newCircle(20,20,50);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
Rectanglerectangle=newRectangle(20,20,80,50);
rectangle.setStroke(Color.BLACK);
Ellipseellipse=newEllipse(20,20,50,70);
ellipse.setStroke(Color.BLACK);
pane.setStyle("-fx-border-color:
gray");pane.getChildren().add(circle);
newRadioButton("Circle");
newRadioButton("Rectangle"newRadioButton("Ellipse");newCheckBox("Fill");
ToggleGroupgroup=newToggleGroup();
rbCircle.setToggleGroup(group);rbCircle.setSelected(true);rbRectangle.setToggleGroup(group);rbEllipse.setToggleGroup(group);
HBoxhBox=newHBox(5);hBox.getChildren().addAll(rbCircle,rbRectangle,rbEllipse,chkFill);hBox.setAlignment(Pos.CENTER);
newBorderPane();
BorderPaneborderPane=borderPane.setCenter(pane);borderPane.setBottom(hBox);
//Createasceneandplaceitinthestage
Scenescene=newScene(borderPane,paneWidth,paneHeight+40);
primaryStage.setTitle("Exercise16_02");//SetthestagetitleprimaryStage.setScene(scene);//PlacethesceneinthestageprimaryStage.show();//Displaythestage
rbCircle.setOnAction(e->{
pane.getChildren().clear();pane.getChildren().add(circle);
if(chkFill.isSelected()){
circle.setFill(Color.BLACK);
}
else{
circle.setFill(Color.WHITE);
}
});
rbRectangle.setOnAction(e->{
pane.getChildren().clear();pane.getChildren().add(rectangle);
if(chkFill.isSelected()){
rectangle.setFill(Color.BLACK);
}
else{
rectangle.setFill(Color.WHITE);
}
});
rbEllipse.setOnAction(e->{
pane.getChildren().clear();pane.getChildren().add(ellipse);
if(chkFill.isSelected()){
ellipse.setFill(Color.BLACK);
}
else{
ellipse.setFill(Color.WHITE);
}
});
chkFill.setOnAction(e->{
if(chkFill.isSelected()){
circle.setFill(Color.BLACK);
rectangle.setFill(Color.BLACK);
ellipse.setFill(Color.BLACK);
}else{
circle.setFill(Color.WHITE);rectangle.setFill(Color.WHITE);
ellipse.setFill(Color.WHITE);
}
});
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.*/
publicstaticvoidmain(String[]args){launch(args);
}
}
16.2附加
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.layout.*;
import
javafx.scene.paint.Color;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.geometry.Pos;
public
classExercise16_02Extra
extendsApplication{
final
staticdoublePANEL_
WIDTH=400;
final
staticdoublePANEL_
HEIGHT=140;
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
GridPanepaneForOriginal=
GridPanepaneForInversed=
newGridPane();newGridPane();
TextField[][]tfA=newTextField[3][3];
TextField[]tfB=newTextField[3];
for(inti=0;i<3;i++){
for(intj=0;j<3;j++){
tfA[i][j]=newTextField();
tfA[i][j].setPrefColumnCount(4);
}
tfB[i]=newTextField();tfB[i].setPrefColumnCount(4);
}
HBox[]hBoxes=newHBox[3];
for(inti=0;i<3;i++){
hBoxes[i]=newHBox(5);
hBoxes[i].getChildren().addAll(tfA[i][0],newLabel("x"),tfA[i][1],newLabel("y"),tfA[i][2],newLabel("z="),tfB[i]);
}
VBoxvBox=newVBox(5);
vBox.getChildren().addAll(hBoxes[0],hBoxes[1],hBoxes[2]);
HBoxhBox=newHBox(5);
LabellblStatus=newLabel();
ButtonbtSolveEquation=newButton("SolveEquation");
hBox.getChildren().addAll(btSolveEquation,lblStatus);
BorderPanepane=newBorderPane();
pane.setCenter(vBox);pane.setBottom(hBox);
Scenescene=newScene(pane,PANEL_WIDTH,PANEL_HEIGHT);
primaryStage.setTitle("Exercise16_02");//SetthestagetitleprimaryStage.setScene(scene);//PlacethesceneinthestageprimaryStage.show();//Displaythestage
btSolveEquation.setOnAction(e->{
double[][]A=newdouble[3][3];
double[]B=newdouble[3];
for(inti=0;i<3;i++){
for(intj=0;j<3;j++){
A[i][j]=Double.parseDouble(tfA[i][j].getText());
}
B[i]=Double.parseDouble(tfB[i].getText());
}
double[]result=Exercise08_02Extra.getSolution(A,B);
if(result==
null)
lblStatus.setText(
"Nosolutions");
else
lblStatus.set