Final Exam Flashcards
Declaring a class to be Iterable
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(); } }
base case
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.
exit condition
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.
tail recursive
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.
recursive method finds the largest element in an array.
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)); }
The sum of digits in 1246 is- recursive method ex
/** * 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 }
Recursive list methods code
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(); }
} }
components
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.
event-driven
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.
Model-View-Controller
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.
Event listeners
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.
content pane
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.
Dialogs
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.
linear search
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.
binary search
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); }