CPSC 124, Winter 1998

Quiz Number 7

This is the seventh and final quiz given in CPSC 124: Introductory Programming, Winter 1998. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers.

Question 1: Among the standard classes in Java are Container and Component. In order to create a GUI, the programmer uses subclasses of these two classes. What are some examples of subclasses of Container and Component? What is the essential difference between Containers and Components?

Answer: Java's Component class represents visual elements of a Graphical User Interface. Its subclasses include Button, Checkbox, TextField, Choice, and Canvas. The Container class is another subclass of Component. A Container is a component that can contain other components (including other containers). This is the essential difference between containers and other types of component. Subclasses of Container include Frame, Panel, and Applet.

Question 2: Briefly explain the purpose of a LayoutManager in Java.

Answer: The components in a contain must be laid out (that is, positioned and sized) in the container. A LayoutManager is an object that implements some particular policy for how components are to be laid out in a container. In general, each container has a LayoutManager which decides how to arrange components that are added to the container using one of its add() methods. (However, it is also possible for a container to do its own layout, if its layout manager is set to null.)

Question 3: What does it mean to say that a program is robust? Briefly discuss some of the guidelines for writing robust programs.

Answer: A robust program is one that can continue without crashing and give valid results, even if unexpected errors or exceptions occur. It is difficult for a program to anticipate all possible errors that might occur (especially errors in input data), and so writing robust programs is difficult. One guideline is to identify "preconditions" -- that is, conditions that must be true at a given point in the execution of a program if the program is to continue correctly -- and to check that the preconditions are satisfied. Another guideline is to use exception-handling to catch and deal with unexpected errors.

Question 4: What would be the purpose of the following action() method? When would it be called, and why? What does it do when it is called? What do you suppose cmdButton1 and cmdButton2 are?

              public boolean action(Event evt, Object obj) {
                 if ( evt.target == cmdButton1 ) {
                    return true;
                 else if ( evt.target == cmdButton2 ) {
                    return true;
                    return super.action(evt,obj);

Answer: An action() method is called by the system when the user generates an event by interacting with one of the components that make up a graphical user interface. Component classes that can generate action events include Button, Choice, TextField, and Checkbox. The "target" of the event is the object that caused the event, that is, the Button, Choice, etc. Given their names, we can assume that in this case, the possible targets -- cmdButton1 and cmdButton2 -- are Buttons. When the user clicks on one of these buttons, the action() method is called to give the program a chance to respond to that event. In this case, the action() method simply calls a procedure to handle the event.

Question 5: Suppose that a method, processInput, has already been defined and that its first line is

void processInput(String inputLine) throws IllegalInputException}

Write a code segment that will read one line of input from the user and call the processInput method to process that line of input. The three lines that you need to do this should be in a try statement, and you should catch the possible IllegalInputException. If an exception occurs, you should print out an error message. You can assume that a "console" variable is available for doing input/output.


            try {
               console.put("Type a line of input: ");
               String line = console.getln();
            catch (IllegalInputException e) {
               console.putln("Sorry, your input is illegal.");

David Eck, 12 March 1998