1
Name:_______________________
(150 minutes)
CSCI 1302 OO Programming
Armstrong Atlantic State University
Instructor: Dr. Y. Daniel Liang
I pledge by honor that I will not discuss this exam with anyone until my
instructor reviews the exam in the class.
Signed by ___________________ Date _____________________
Part I:
A. (2 pts)
Fill in the missing code in the following program.
import javafx.application.Application;
A:
primaryStage.setScene(scene);
primaryStage.show();
B. (2 pts)
Show the output of the following JavaFX program.
2
A:
launch application
Test constructor is invoked
start method is invoked
C. (5 pt)
Which of following statements are correct?
1. You can create and display multiple stages in JavaFX.
(Yes)
D. (3 pt)
Write the code for the following:
1. Create a line with the end points (5.5, 54) and (0.2,
45).
3
2. Create a circle with radius 5.5 centered at (5.4, 1).
3. Create a rectangle with width 8.4 and height 4.3
centered at (5.4, 1).
Part II: Write Programs
1. (5 pts) The ClockPane class presented in Listing 14.24
in text displays a clock. Use this class to display
four clocks as shown in the following figure.
Solution:
import javafx.application.Application;
4
import javafx.stage.Stage;
ClockPane clock1 = new ClockPane();
clock1.setPrefSize(1000, 1000);
pane.add(clock1, 0, 0);
5
pane.add(clock2, 1, 0);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 200, 200);
6
2. (10 pts) Write a program that clicks the Refresh
button to display the four distinct random numbers
between 0 and 51, as shown in the following figure.
Solution:
import javafx.application.Application;
public class Exercise15_20Extra extends Application {
private Label lbl1 = new Label();
7
// Create a button
Button btRefresh = new Button(“Refresh);
btRefresh.setOnAction(e -> {
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 250, 150);
primaryStage.setTitle(“Test”); // Set the stage title
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
8
}
}
3. (15 pts) Write a program that draws a fixed rectangle
centered at (100, 60) with width 100 and height 40.
Whenever the mouse is moved, display a message
indicating whether the mouse point is inside the
9
rectangle at the mouse point or outside of it, as
shown in the following figure. To detect whether a
point is inside a polygon, use the contains method
defined in the Node class.
Solution:
import javafx.application.Application;
}
text.setX(e.getX());
text.setY(e.getY());
});
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 400, 250);
}
10
}
11
Part III: Multiple Choice Questions: (1 pts each)
(Please circle your answers on paper first. After you
finish the test, enter your choices online to LiveLab. Log
in and click Take Instructor Assigned Quiz. Choose Quiz3.
You have 5 minutes to enter and submit the answers.)
1
What is the output of the following code?
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
public class Test {
public static void main(String[] args) {
IntegerProperty d1 = new SimpleIntegerProperty(1);
IntegerProperty d2 = new SimpleIntegerProperty(2);
d1.bind(d2);
System.out.print(“d1 is ” + d1.getValue()
+ ” and d2 is ” + d2.getValue());
d2.setValue(3);
System.out.println(“, d1 is ” + d1.getValue()
+ ” and d2 is ” + d2.getValue());
}
}
A. d1 is 1 and d2 is 2, d1 is 1 and d2 is 3
B. d1 is 2 and d2 is 2, d1 is 2 and d2 is 3
C. d1 is 1 and d2 is 2, d1 is 3 and d2 is 3
D. d1 is 2 and d2 is 2, d1 is 3 and d2 is 3
#
2
Analyze the following code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class Test extends Application {
12
@Override // Override the start method in the Application
class
public void start(Stage primaryStage) {
// Create a pane to hold the image views
Pane pane = new HBox(10);
pane.setPadding(new Insets(5, 5, 5, 5));
Image image = new
Image(“www.cs.armstrong.edu/liang/image/us.gif”);
pane.getChildren().addAll(new ImageView(image), new
ImageView(image));
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle(“ShowImage”); // Set the stage
title
primaryStage.setScene(scene); // Place the scene in the
stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with
limited
* JavaFX support. Not needed for running from the
command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. The addAll method needs to be replaced by the add method.
B. The program runs fine and displays two images.
C. The image object cannot be shared by two ImageViews.
D. new Image(“www.cs.armstrong.edu/liang/image/us.gif”) must be
replaced by new
Image(“http://www.cs.armstrong.edu/liang/image/us.gif”).
#
3
To place a node in the left of a BorderPane p, use
___________.
A. p.left(node);
B. p.setLeft(node);
C. p.setEast(node);
13
D. p.placeLeft(node);
#
4
To place two nodes node1 and node2 in a HBox p, use
___________.
A. p.addAll(node1, node2);
B. p.getChildren().addAll(node1, node2);
C. p.getChildren().add(node1, node2);
D. p.add(node1, node2)
#
5
Analyze the following code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
public class Test extends Application {
@Override // Override the start method in the Application
class
public void start(Stage primaryStage) {
HBox pane = new HBox(5);
Circle circle = new Circle(50, 200, 200);
pane.getChildren().addAll(circle);
circle.setCenterX(100);
circle.setCenterY(100);
circle.setRadius(50);
pane.getChildren().addAll(circle);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle(“Test”); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the
stage
primaryStage.show(); // Display the stage
}
/**
14
* The main method is only needed for the IDE with
limited
* JavaFX support. Not needed for running from the
command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. The program has a compile error since the circle is added to a
pane twice.
B. The program runs fine and displays two circles.
C. The program has a runtime error since the circle is added to a
pane twice.
D. The program runs fine and displays one circle.
#
6
To remove two nodes node1 and node2 from a pane, use ______.
A. pane.removeAll(node1, node2);
B. pane.getChildren().remove(node1, node2);
C. pane.getChildren().removeAll(node1, node2);
D. pane.remove(node1, node2);
#
7
Analyze the following code.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Test extends Application {
@Override // Override the start method in the Application
class
public void start(Stage primaryStage) {
// Create a button and place it in the scene
Button btOK = new Button(“OK”);
btOK.setOnAction(e > System.out.println(“OK 1”));
btOK.setOnAction(e > System.out.println(“OK 2”));
15
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle(“MyJavaFX”); // Set the stage
title
primaryStage.setScene(scene); // Place the scene in the
stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with
limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. When clicking the button, the program displays OK2.
B. When clicking the button, the program displays OK1.
C. When clicking the button, the program displays OK1 OK2.
D. The program has a compile error, because the setOnAction
method is invoked twice.
#
8
Suppose the following program displays a pane in the stage.
What is the output if the user presses the key for letter
B?
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
// import javafx classes omitted
public class Test1 extends Application {
@Override
public void start(Stage primaryStage) {
// Code to create and display pane omitted
Pane pane = new Pane();
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle(“MyJavaFX”); // Set the stage
title
primaryStage.setScene(scene); // Place the scene in the
16
stage
primaryStage.show(); // Display the stage
pane.requestFocus();
pane.setOnKeyPressed(e ->
System.out.print(“Key pressed ” + e.getCode() + ”
“));
pane.setOnKeyTyped(e ->
System.out.println(“Key typed ” + e.getCode()));
}
/**
* The main method is only needed for the IDE with
limited
* JavaFX support. Not needed for running from the
command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. Key pressed B Key typed UNDEFINED
B. Key typed UNDEFINED
C. Key pressed B Key typed
D. Key pressed B
#
9
Suppose A is an anonymous inner class in Test. A is compiled
into a file named _________.
A. Test$A.class
B. A$Test.class
C. A.class
D. Test$1.class
E. Test&1.class
#
10
Fill in the code below in the underline:
public class Test {
public static void main(String[] args) {
17
Test test = new Test();
test.setAction(______________________________);
}
public void setAction(T1 t) {
t.m();
}
}
interface T1 {
public void m();
}
A. (e) > {System.out.print(“Action 1! “)}
B. (e) > System.out.print(“Action 1! “)
C. () > System.out.print(“Action 1! “)
D. System.out.print(“Action 1! “)
#
11
To handle the mouse click event on a pane p, register the
handler with p using ______.
A. p.setOnMousePressed(handler);
B. p.setOnMouseDragged(handler);
C. p.setOnMouseReleased(handler);
D. p.setOnMouseClicked(handler);
#
12
A JavaFX action event handler is an instance of _______.
A. EventHandler<ActionEvent>
B. ActionEvent
C. EventHandler
D. Action
#
13
Analyze the following code:
import javafx.application.Application;
18
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Test extends Application {
@Override // Override the start method in the Application
class
public void start(Stage primaryStage) {
Pane pane = new FlowPane();
Button bt1 = new Button(“Java”);
Button bt2 = new Button(“Java”);
Button bt3 = new Button(“Java”);
Button bt4 = new Button(“Java”);
pane.getChildren().addAll(bt1, bt2, bt3, bt4);
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle(“Test”); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the
stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with
limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. Two buttons are displayed with the same text “Java”.
B. Three buttons are displayed with the same text “Java”.
C. One button is displayed with the text “Java”.
D. Four buttons are displayed with the same text “Java”.
#
14
The method __________ gets the contents of the text field
tf.
A. tf.findString()
19
B. tf.getText()
C. tf.getText(s)
D. tf.getString()
Please double check your answer before clicking the Submit
button. Whatever submitted to LiveLab is FINAL and counted
for your grade.
Have you submitted your answer to LiveLib? ______________