Ryerson University: School of Computer Science

CPS209, Midterm Test 2, Winter 2005












Last Name                                 
First Name                                
Student Number                                 
Section                                 
Circle Instructor           Harley           McInerney           



Instructions









 Question    Out of    Mark  
 Programming Q1    10            
 Programming Q2    10            
 Multiple Choice    10            
 Total    30           

    Programming

  1. Fill in the following graphical user interface which serves as a front end to the BankAccount class. The GUI has buttons for Deposit and Withdraw along with a textfield for the amount, and a text field that shows the balance. The user interface is shown below.
     Hint: Here is The syntax for adding a panel to the CENTER of the content pane and the code is similarly for SOUTH:
        getContentPane().add(panel, BorderLayout.CENTER);

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class BankAccountTest
    {
        public static void main(String[] args)
        {
            BankAccountFrame frame = new BankAccountFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    public class BankAccountFrame extends JFrame
    {
        private BankAccount ba;
    
        public BankAccountFrame()
        {
            ba = new BankAccount();
                                                                            
            /** 
               make the two JButtons and two textfields and one JLabel
             */
            JLabel balanceLabel = new JLabel("Balance");
            JButton depositButton = new JButton("Deposit");
            final JTextField amountField = new JTextField(5);
            final JTextField balanceField = new JTextField(5);
            JButton withdrawButton = new JButton("Withdraw");
                                                                                   
            /**
               Design listener for deposit button 
             */
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	/**
    	 Install listener in the deposit button 
    	 */
    	______________________________________________________________________
    
    	______________________________________________________________________
    
            /**
    	 For brevity we omit similar code for withdraw 
    	 */
    
            //   ***** omitted code (skip this area) *****
    
            /**
    	 Using one or more panels, put the components in the frame to make the frame appear as shown above.
    	 */
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
    	______________________________________________________________________
    
        }//constructor
    }//class
    

  2. Write a program Find which prints out all lines of given files that contain a given keyword. The keyword is the first command line argument, and the files to check are the next command line arguments. For example in the following command, the word class is the keyword for which to search in the files BankAccount.java, README.txt, HoundDog :
    java Find class BankAccount.java README.txt HoundDog
    
    might produce:
    
         BankAccount.java: public class BankAccount
         README.txt : The classes in this assignment are as follows
         README.txt : we studied the classification of words
         HoundDog: She said she was high class, but that was just a lie.
    
    
    b) Include an appropriate usage method which shows how to run the program and then exits the program. Call the usage method unless there are least 2 arguments.
    Hint: the indexOf(String str) method in String class returns the index of substring str if present, otherwise -1
    
    
    c) Catch any IOException and print a message, but continue to search the other files if any.
     
    public class Find
    {
         public static void main(String[] args)
         {
    

      Multiple Choice Questions

    1. What is the difference between a frame and a panel?
          a) Frame has a content pane but panel does not.
          b) Frame has special border with X in the top right corner, but panel does not.
          c) Frame is the outer most component of a GUI, whereas panel is an inner component that can be added to a frame.
          d) None of the above
          e) All of the above 
      
    2. What is difference between paint and paintComponent methods?
          a) When you use paint you don't need to call the paint method in the super class, 
             but in the paintComponent method you must call super.paintComponent(g).
          b) paint is used in a panel whereas paintComponent is used in an applet.
          c) paint has a parameter of type Graphics2D whereas paintComponent has parameter of type Graphics.
          d) paint can be called indirectly by repaint() but paintComponent cannot.
          e) None of the above
      
    3. What is difference between radio buttons and check boxes
          a) Check boxes are added to a button group, radio buttons are not. 
          b) When using radio buttons there must be at least four, but you can have any number of check boxes.
          c) Radio buttons are mutually exclusive, check boxes are not.
          d) A radio button can use an action listener, but a check box does not.
          e) All of the above
      
    4. What is the difference between throwing and catching an exception
          a) If an exception is thrown the program terminates, whereas if an exception is caught the program continues.
          b) When you throw an exception you must specify which kind (like NullPointerException), 
             but when you catch an exception you do not have to specify the exact type you are catching. 
          c) Throwing an exception is done inside a method whereas catching an exception is done outside the method.  
          d) None of the above
          e) All of the above
      
    5. What is a checked exception?
          a) Any exception such as NumberFormatException or IOException that is the fault of the user or the system.
          b) A condition which the JVM (Java Virtual Machine) watches for and interrupts your program if it occurs.
          c) An exception which the compiler requires you to acknowledge, either with a throws clause or a try/catch.
          d) All of the above
          e) None of the above
      
    6. Why don't you need to declare that your method might throw NullPointerException?
          a) Because it might not be thrown.
          b) Because that exception is not a checked exception
          c) Because your method might have no object references.
          d) None of the above
          e) All of the above
      
    7. The statements in a finally clause
          a) are guaranteed to be executed if the try block is entered (whether or not an exception is thrown)
          b) are executed only if an exception is thrown.
          c) are executed just before the program terminates.
          d) None of the above
          e) All of the above.
      
    8. Why does the read method of FileReader return an int?
          a) Because it reads integer values.
          b) Actually it does not return an int but rather type char.
          c) Because it is not a stream reader (which reads byte values).
          d) So it can return 0 at end of file.
          e) None of the above.
      
    9. The (1)________ sends (2)________ to (3)__________ when an event occurs.
          a) (1)event object   (2)event listener  (3)event source
          b) (1)event listener (2)event object    (3)event source
          c) (1)event source   (2)event object    (3)event listener
          d) (1)event source   (2)event listener  (3)event object
          e) None of the above
      
    10. What is the difference between ActionEvent and MouseEvent
          a) They are different class names for same code.
          b) ActionEvent has one method while MouseEvent has five methods.
          c) MouseEvent has methods getX() and getY() which ActionEvent does not have.
          d) ActionEvent has a getSource() method which MouseEvent does not have.
          e) None of the above
      
    11. Why do we use inner classes for event listeners?
          a) Because a listener must be placed inside a component.
          b) Because this is a convenient way for the listener to obtain the information it needs.
          c) Because an inner class automatically "listens" to events in the outer class.
          d) Because this is the only way the listener can gain control of buttons or other components in the outer class.
          e) None of the above