CSC 224 -- Summary for Day8, July 9, 2001.

Review Questions

  1. Name three requirements for a good equals method.

    Answer: It should be

    • Reflexive:   a.equals(a) == true
    • Symmetric:   a.equals(b) == b.equals(a)
      A good way to insure this is to check that a and b are objects of the same class.
    • a.equals(null) == false

  2. For an arbitrary class A, write the code to define an equals method where a.equals(b) if and only if a == b.
    Answer:
    public boolean equals(b)
    {
        this == b;
    }

  3. When is a method not allowed to throw an exception?
    Answer:
    • When the method overrides a method in a base class that does not throw the exception.
    • When the method implements an interface that does not throw the exception.

  4. What are the steps needed to read from an input file?
    Answer:   Create the following objects: FileReader, BufferedReader, StringTokenizer. Read lines using the buffered reader method readLine until a null line is obtained. Get the fields out of line with the StringTokenizer. You specify the delimiters when you create the StringTokenizer object.

  5. What is a file chooser?
    Answer:   A file open or save dialog that prompts the user to choose a file. It is implemented with a JFileChooser object in Java.

  6. What is wrong with line of code for the Deck class in the Dealer Project?
    Stack leftstack = new Stack(26);
    Answer:   Here you are declaring a local variable leftstack that hides the instance variable leftstack. Here is what you should do:
    leftstack = new Stack(26);

  7. Here is code from the Recursive Example from Day8 and the answer. The problem is to predict the output using the recursion tree.

    public class Main
    {
        public static void main(String[] args)
        {
            f(3);
        }

        public static void f(int n)
        {
            if (n > 0)
            {
                System.out.print(n + " ");
                f(n - 1);
                System.out.print(n + " ");
                f(n - 2);
                System.out.print((n + 1) + " ");
            }
        }
    }

    Here is the recursion tree. f(n) represents the recursive method calls when n > 0 and the numbers represent output.

    To obtain the output, pretend that you are an ant. Start at the root node f(3) and crawl counterclockwise around the tree with your left front leg touching the tree at all times. Output every number that you encounter. Here is the output:

    3 2 1 1 2 2 3 3 1 1 2 4

New Topics

  1. The FileChooser Class.   Look at Example ShowWords from Day7 examples (Day7.zip and Day7.txt.)
    This example also uses the FileWriter, BufferedWriter, and PrintWriter classes for output to a text file.

  2. The StringTokenizer Class Look at Examples StringTokenizer from Day7 (Day7.zip and Day7.txt.)
    Reading files with comma delimiters.

  3. More about Interfaces   Look at Examples Interface and Sorter from Day7 (Day7.zip and Day7.txt.)

    1. In the Interface example, both the Philosopher and the Dog classes implement the Speaker interface.

    2. In the Sorter example, a Sorter object sorts objects that implement the Comparable interface. Therefore, if we want to sort Kid objects in alphabetical order by name, the Kid class must implement the Comparable interface. To do this, we supply a comparesTo method for the Kid class. Note that this method uses the comparesTo method from the String class to compare the names.

  4. Running JDK1.3 from the DOS prompt.

    1. Create a folder with the source code (.java) files, one file for each class. To start, just create a simple Main class with a main method with WordPad.

    2. Open a DOS Window (called a Command Window in Windows 2000).

    3. Check the path by entering "path". See if C:\JDK1.3\BIN is in the path. Also check if

    4. If jdk1.3 is not in the path, add it to the path:
      set PATH=%PATH%;C:\JDK1.3\BIN
      Note: the path should be okay in the computer labs.

    5. Compile all .java files. For example:   javac Main.java

    6. Clear the CLASSPATH as follows:   set CLASSPATH=
      There have been problems with the CLASSPATH in the computer labs, so I can't guarentee that you will not have any problems.

    7. Execute the main method in the class Main. For example:   java Main

  5. Relational Databases and Jdbc   Look at Examples KidsDB, KidsDB2, KidsDB3, and ByGender from Day8 (Day8.zip and Day8.txt.)
    Here is a Summary of the SQL SELECT Statement. Here are also Steps to Setting up an ODBC Connection.

  6. The JComboBox object Look at Examples ComboBox (Day8.zip and Day8.txt.)
    This example picks a color from the combo box and uses it so set the background color of the Panel.

  7. Window events and the WindowListener interface   Look at WindowClosing and WindowEvents from Day8 (Day8.zip and Day8.txt.)
    The methods that must be implemented for the WindowListener interface are windowClosing, windowClosed, windowActivated, windowDeactivated, windowIconified, window DeIconified.

    Note:   If you are executing a frame based user interface from a DOS Window, the application running from the DOS Window will not automatically close when you close the user interface. You have two choices for closing the application after the window is closed:

    • Type Control-C.
    • Implement a windowClosing method for the WindowListener interface. In that method, make a call to the method System.exit(0);   This will close the application when the user interface window is closed.

  8. Mouse events and the MouseListener interface   Look at MouseClicks Example from Day8 (Day8.zip and Day8.txt.)
    The methods that must be implemented for the MouseListener interface are mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased.

  9. The Vector Java Collection Class.   Look at Vector Example from Day8 (Day8.zip and Day8.txt.)
    Any object can be added to the collection. However, once an object is in the vector, it is considered to be from the Object class, and must be cast back to its original class to be used. To store primitive data types in a vector first put them into wrapper class objects. The wrapper class for an int is Integer; for a double is Double, etc.

  10. Recursive Methods.   Look at Recursive and FamilyTree Examples from Day8 (Day8.zip and Day8.txt.)