Answers for Quiz on Chapter 13
This page contains sample answers to the quiz on Chapter 13 of Introduction to Programming Using Java. Note that generally, there are lots of correct answers to a given question.
Question 1:
What is an "observable property"?
Answer:
An observable value is an object that emits events when its value is changed. It is possible to add listeners to the object, and the listeners will be notified when its value changes.
An observable property is a property (that is, an instance variable) in an object that is an observable value. Furthermore, the use of the term "observable property" usually implies that it is possible to "bind" other properties of the same type to this one, so that the value of the bound property will track the value of the property to which it is bound.
Question 2:
Suppose that input is a TextField and that label is a Label. Suppose that you want the text on the label to always be the same as the text in the text field. Write two code segments to accomplish that, one using an event listener and one using property binding.
Answer:
The text field has a property input.textProperty() that represents the text in the input box. We can add an event listener to that property, and the handler for that event can set the text in the label:
input.textProperty().addListener( e -> label.setText(input.getText()) );
The label also has a property, label.textProperty(), representing the text that it displays, so another option is to bind that property to the property of the input box:
label.textProperty().bind( input.textProperty() );
One difference between these two approaches is that the binding will immediately copy the current text from the text field into the label. When using the listener, we would have to manually set up the initial texts to be the same.
By the way, the listener that was used in the above command is an InvalidationListener. Another approach would be to use a ChangeListener. The handler for a ChangeEvent takes three parameters, where the third parameter is the new value of the observable property, so the task could also be accomplished by
input.textProperty().addListener( (o,oldVal,newVal) -> label.setText(newVal)) );
Question 3:
Describe the picture that is produced by the following code, where canvas is a Canvas:
GraphicsContext g = canvas.getGraphicsContext2D(); g.setFill(Color.WHITE); g.fillRect(0,0,canvas.getWidth(),canvas.getHeight()); g.translate( canvas.getWidth()/2, canvas.getHeight()/2 ); g.rotate( 30 ); g.setFill(Color.RED); g.fillRect(0,0,100,100);
Answer:
The entire canvas is filled with white, except for a filled red square that is 100-by-100 pixels in size. The corner of the square is at the center of the canvas that is being painted, and the top side of the square descends at a 30 degree angle from that point. The command g.fillRect(0,0,100,100) draws a red square with its top left corner at (0,0). But that square is first rotated by 30 degrees and then translated. The translation moves the point (0,0) to the point (canvas.getWidth()/2,canvas.getHeight()/2), placing the top left corner of the square at the center of the canvas. (Transformations are applied to objects in the opposite of their order in the code.)
Of course, this answer assumes that no transformation had been applied before this code is executed. If there is a previous transformation, then that transform would be applied to the picture that is described in the answer, before that picture appears on the screen.
Question 4:
Create a LinearGradient paint and use it to fill the rectangle drawn by g.fillRect(100,100,300,200). The rectangle should look like this, light gray at the top and black at the bottom:
(The API for creating gradient paints is complicated. It's OK to look it up!)
Answer:
The gradient can be defined by two color stops, one at 0, with color Color.LIGHTGRAY and one at 1, with color Color.BLACK. Since the rectangle is covered by one copy of the gradient, the CycleMethod for the gradient is irrelevant. We can specify the start point for the gradient to be the top left corner of the rectangle, (100,100), and the end point for the gradient to be the bottom left corner, (100,300). (Note that the height of the rectangle is 200; since the top of the rectangle is at y=100, the bottom is at y=100+200. The end point of the gradient is specified using (x,y)-coordinates, not width and height like the rectangle.) So the rectangle can be drawn with
LinearGradient gradient = new LinearGradient( 100,100, 100,300, false, CycleMethod.REPEAT, new Stop(0,Color.LIGHTGRAY), new Stop(1,Color.BLACK)); g.setFill(gradient); g.fillRect(100,100,300,200)
Many variations on this are possible. For example, the start point could be at the bottom right corner, and the end point at the top right corner. In that case, the color for the first color stop would be black, and the color for the second color stop would be light gray. We could also use a proportional gradient, where the coordinates for the start and end points are given in a coordinate system where the coordinates of the top left corner are (0,0) and the coordinates of the bottom left corner are (0,1):
LinearGradient gradient = new LinearGradient( 0,0, 0,1, true, CycleMethod.REPEAT, new Stop(0,Color.LIGHTGRAY), new Stop(1,Color.BLACK));
Question 5:
Suppose that g is a GraphicsContext. Explain the purpose of the methods g.save() and g.restore().
Answer:
A call to g.save() pushes a copy of the current state of the graphics context onto a stack of saved states. The state include properties of the graphics context that affect drawing such as the transform, the stroke and fill paints, and the line width. A call to g.restore() will pop the most recently saved state from the stack and restore graphics context properties to their values from that state. Any changes to the state that were made after the corresponding call to g.save() will be undone. This allows you to make temporary changes to the state while being sure that those changes will not carry over to future drawing.
Question 6:
What does the acronym MVC stand for, and how does it apply to the List class?
Answer:
MVC stands for "Model-View-Controller." In a List, the view is the actual visible component on the screen. The model is the collection of data that specifies, among other things, what appears in each cell of the list. This data is stored in an observable list, and the view listens for changes in that list and redraws itself when any change occurs. (Another part of the model is the "selection model", which keeps track of which item is currently selected in the list.) The controller is responsible for interaction with the user. It consists mostly of a bunch of listener objects that listen for events generated when the user interacts with the list. The listeners respond by making changes in the model, which will in turn cause a change in the view.
Question 7:
What is the difference between a "modal" dialog box and a "modeless" dialog box?
Answer:
When a modal dialog box is shown, the user will not be able to use the parent window of the dialog box until the dialog box is closed. With a modeless dialog box, it is possible for the user to work with both the dialog box and its parent window. (Some dialog boxes are "application modal," meaning that they block interaction with the entire application, not just with one window.)
Question 8:
The Java API includes some classes in a package named org.w3c.dom. Why such a funny package name?
Answer:
The packages presumably come from an organization that owns the Web domain name w3c.org. (In fact, it's the organization that controls standards for the web.) This follows the recommended naming convention for packages: The package name starts with the pieces of the domain name in reverse order. The last part of the package name, "dom", doesn't come from the domain name; it was something chosen by the organization for this particular package, to distinguish it from other packages that might be put out by the same organization. (In fact, the org.w3c.dom package contains classes and subpackages related to the "Document Object Model," which is used in the processing of XML and HTML documents.)
Question 9:
Suppose that closeItem is a MenuItem. What is done by the following statement? (What is an "accelerator"?)
closeItem.setAccelerator( KeyCombination.valueOf("ctrl+W") );
Answer:
An accelerator for a menu item is a combination of keyboard keys that is equivalent to selecting that item from a menu. KeyCombination.valueOf("ctrl+W") is a key combination that represents pressing the "W" key while holding down the control key. Since that key combination is installed as the accelerator for closeItem, hitting "W" while holding down the control key will invoke the same action as selecting closeItem with the mouse.
Question 10:
What is meant by Internationalization of a program?
Answer:
Internationalization refers to writing the program in a way that will make it easy to adapt the program for use in a variety of "locales." For example, it should be easy to translate all the strings that are used in the program into other languages. To make this possible the strings should not be hard coded into the program itself. Instead, they are placed in a separate resource file, so that the program can be translated into another language simply by writing a resource file for that language. (Internationalization also applies to the format that is used for dates and numbers.)