Final Exam Flashcards

1
Q

Declaring a class to be Iterable

A
public class SimpleLinkedList implements Iterable {
   // class body goes here up to the Cursor definition
   private class Cursor implements Iterator {
      // Same code for Cursor as before
   }
  public Iterator iterator() {
     return new Cursor();
  } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

base case

A

The exit condition describes the base case condition. The base case is the easiest problem to solve. Every recursive solution must have at least one base case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

exit condition

A

The body of the recursive method is controlled by a selection statement (if, etc). At least one of the alternatives does not contain a recursive call. The condition associated with that alternative is the exit condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

tail recursive

A

That is, the recursive call as the last statement in the method. Tail recursive methods are usually easy to convert to loops. Recursive methods that are not tail recursive can be more difficult. To convert those, you essentially have to mimic what the virtual machine does with your own stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

recursive method finds the largest element in an array.

A
public int max(int x, int y){
        if (x >= y) 
            return x;
        return y;
    }
/**
 * Find the largest element in the array.
 *  @param start is the smallest index to start looking. 
 *  @return the largest element in an array, beginning at index start.
 */
public int largest(int[] array, int start){
    if (start == array.length - 1) 
        return array[start];
    return max(array[start], largest(array, start + 1));
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The sum of digits in 1246 is- recursive method ex

A
/**
    * Find the sum of digits in an integer.
    *  @param num is a non-negative integer (num > -1). 
    *         This method fails if num < 0.
    *  @return the sum of digits in number
    */
    public int digitSum(int num){
        if (num == 0) 
            return 0;
        return (num%10) + digitSum(num/10); // num%10 is the one's digit
                                            // num/10 has the rest of the digits
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Recursive list methods code

A
public class RecursiveList {
    private List list;
    public RecursiveList() {
        list = null;
    }
    public void addToFront(String element) {
        list = new List(element, list);
    }
    public int size() {
        if (list == null)
            return 0;
        return list.size();
    }
private class List {
    public String element;
    public List next;
        public List(String element, List next) {
            this.element = element;
            this.next = next;
        }
        public int size() {
            if (next == null)
                return 1;
            else
                return 1 + next.size();
        }
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

components

A

The graphical elements of GUIs are components. Components such as windows that hold other components are containers. The top-level window, which has a border and a title bar, is a kind of component called a frame. Components also include widgets that the frame contains, such as buttons, text fields, labels, and menus. The frame layout refers to how the contents of the frame are arranged. Containers add widgets to their display. A widget must add an event listener in order to detect when a user interacts with it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

event-driven

A

GUI programs are event-driven. That means they respond to user interactions or events. Events are generated when the user clicks a button, presses a key, selects text, or such. With an event-driven programs, there is no “loop to request input followed by process the input,” which is the norm for console-based programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Model-View-Controller

A

The model is the backend logic. It maintains the state and data for the program.
The view is the part of the user interface that displays information about the model to the user. An example is a label that indicates the result of a calculation.
The controller is the part of the user interface for manipulating the application. An example is code that requests a calculation when the user clicks a particular button.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Event listeners

A

Event listeners are objects that determine when events occur. Buttons or other components with which the user can interact must register listeners. For example, an action listener for a button can determine (“listen for”) when the user clicks the button.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

content pane

A

Content panes are lower level containers, typically embedded into primary windows. They are used to organize the structure of the main window. Swing examples of content panes include JPanel, JOptionPane, JTabbedPane, and JSplitPane among others. Every JFrame has a content pane, which you can access via the JFrame method getContentPane(). In order to put a component on a JFrame, you must add the component to the JFrame’s content pane.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Dialogs

A

Dialogs are secondary windows. They are typically opened on special conditions and must be closed by the user. JDialog is the Swing class for dialogs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

linear search

A

A linear search of a collection requires looking at every element in the collection, one-at-a-time, stopping when the value is found. If you get through the entire collection without finding the value, then the search is unsuccessful.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

binary search

A

can be used on ordered arrays. The algorithm for a binary search is as follows. O(log n)
Compare the searched for value to the middle array item
value == middle ==> quit. You’ve found it.
value < middle ==> consider the sublist making up the first half of the array (before the middle item)
value > middle ==> consider the sublist making up the last half of the array (after the middle item)
If you didn’t find the value, repeat the search on the sublist. You are done as soon as you find the item or whenever the sublist that you are supposed to search is empty.

public int binarySearch(int [] a, int start, int stop, int toFind)
{
if (start > stop) // There’s no place to look. Quit.
return -1;

int middle = (start + stop) / 2; // Look here first
if (toFind == a[middle]) // Found item. Quit.
return middle;

   if (toFind > a[middle]) // Look in the last half
      return binarySearch(a, middle + 1, stop, toFind);
   else                    // OR look in the first half
      return binarySearch(a, start, middle - 1, toFind);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Logarithmic complexity

A

This means binary search is O(log n). We say that binary search has logarithmic complexity.

17
Q

Sorting a collection

A

Sorting a collection means arranging the elements in the collection into a particular order. Classes that implement Comparable are eligible as elements in a sorted collection. That means they must define compareTo(T). Some sorts on primitives are also available.
Classes in the Java API that support sorts include Arrays and Collections. Both are in the java.util package.

18
Q

There are three elementary sorts that are useful for small amounts of data:

A

Selection sort. The front part of the collection consists of the smallest elements and is completely sorted. Technique: look for the smallest remaining element in the collection and move it to the end of the sorted front part. O(n^2)
Insertion sort. The front part of the collection is sorted (though the elements are not necessarily the smallest in the collection). Technique: take the first element of the unsorted part and insert it into the proper position in the sorted front part. O(n^2)
Bubble sort. Swap out of order adjacent pairs of elements. Continue to do this until the collection is sorted.

19
Q

Merge sort

A

Merge sort is a classic example of a divide and conquer algorithm. The algorithm is simply described:

Break the list into two pieces.
Sort each piece.
Merge the sorted pieces together to form a sorted list.

The computational complexity of merge sort is O(n log n)
The application of the merge sort algorithm to an array of ints is shown below. It requires copying parts of arrays. This is done by Arrays.copyOfRange(array, from, upto)

20
Q

Quicksort

A

Select any particular element as a pivot element.
Move all elements smaller than the pivot into the first part of the list.
Move all elements larger than the pivot into the second part of the list. (The pivot will wind up in the middle.)
Quicksort the first part and the second part.
O(n log n)

21
Q

binary tree

A

A binary tree is a hieararchical data structure that consists of nodes. Each node contains an element and two other trees, which are called the a left subtree and a right subtree. In the picture below, each node is a circle with a letter (element) and 0, 1, or 2 arrows , which point the subtrees of the node.
The node at the top of the tree is called the root node. The root node in the figure above is A; it is colored gold. The nodes that have empty subtrees are called leaf nodes. The leaf nodes in the figure are C, F, and G; they are colored green. A branch node is any internal node (neither root nor leaf).

B is a left child of A, D is a right child of A. D is the parent of E. Two nodes such as B andD with a common parent are siblings. Any node in a subtree rooted at a given node is a descendant of the node. The given node is an ancestor.

22
Q

full binary tree

A

very node in a full binary tree has either 0 or 2 children.

23
Q

traversal types

A

Preorder traversal. The root of a tree is visited first. The left subtree is visited next. The right subtree is visited last.
Inorder traversal. The left subtree is visited first. The root of a tree is visited next. The right subtree is visited last.
Postorder traversal. The left subtree is visited first. The right subtree is visited next. The root of a tree is visited last

24
Q

binary search tree

A

A binary search tree is a binary tree in which:

the elements are comparable
the root element is greater than all the elements of the left subtree
the root element is less than all the elements of the right subtree
the right and left subtrees of the root are also binary search trees

25
Q

So these are the essential binary tree behaviors:

A

1) create. Create an empty binary tree.
2) search(element-key). Search for an element in a tree. You will typically search on some key field of the item. For example, if the tree elements are students, you might need to search on a name or id as a key field. Searching on a key requires that elements be ordered according to their keys.
3) insert(element). Insert a new element in a tree. The element must be inserted in the correct position in order that the tree remains as a binary search tree.
4) traverse(). Travel through the tree to visit each element. An inorder traversal visits the elements in sorted order.