public class NumberPad extends JApplet implements ActionListener {
/** Runs when applet loads. */ public void init() { NumberPad pad = new NumberPad(); this.setContentPane(pad.top); }
<html> <head><title>Number Pad Applet</title></head> <body> <applet code="NumberPad.class" width="200" height="180"> You do not have a Java plugin. </applet> </body> </html>
appletviewer numpad.html
chmod
command or SFTP options to set permissions correctly.
ls -la
command:
-rw-r--r-- 1 ztomasze uh 2401 Oct 14 22:54 NumberPad.class -rw-r--r-- 1 ztomasze uh 176 Oct 14 22:57 numpad.html
drwxr-xr-x 2 ztomasze uh 512 Oct 14 23:05 ./ drwxr-xr-x 5 ztomasze uh 512 Oct 14 23:05 ../
//assuming array-based implementation public int indexOf(E item) { for (int i = 0; i < this.array.length; i++) { E element = this.array[i]; if (element.equals(item)) { return i; } } return -1; //not found }
element.compareTo(item) == 0
too, but then limited to Comparables
public int indexOf(E item) { return binarySearch(this.array, item, 0, array.length - 1); } public static <E extends Comparable<E>> int binarySearch(E[] data, E value, int start, int end) { if(start > end) { return -1; //not found } int middle = (start + end) / 2; if(data[middle].compareTo(value) == 0) { return middle; //found it! Return its index } if(data[middle].compareTo(value) < 0) { return binarySearch(data, value, middle + 1, end); } else { return binarySearch(data, value, start, middle - 1); } }
public int indexOf(E item) { int low = 0; int high = array.length - 1; while (low <= high) { int mid = (low + high) / 2; if (item.compareTo(array[mid]) == 0) { return mid; }else if (item.compareTo(array[mid]) < 0) { high = mid - 1; } else { low = mid + 1; } } return -1; //not found }
public void add(E item) { if (size == array.length) { //increase array array = java.util.Arrays.copyOf(array, array.length * 2); } //then shift to produce empty spot at insertion point: int i = size; //need i after loop while (i > 0 && array[i - 1].compareTo(item) > 0) { array[i] = array[i - 1]; i--; } array[i] = item; size++; }
//from wikipedia pseudocode for (int i = 1; i < array.length; i++) { E item = array[i]; int hole = i; while (hole > 0 && array[hole - 1].compareTo(item) > 0) { array[hole] = array[hole - 1]; hole--; } array[hole] = item; }
Initial: B/8 A/5 C/7 B/7 D/7 A/8 Sort by number: A/5 C/7 B/7 D/7 B/8 A/8 Now by letter: A/5 A/8 B/7 B/8 C/7 D/7 (stable) Now by letter: A/8 A/5 B/7 B/8 C/7 D/7 (unstable)
//from wikipedia code for (int i = 0; i < array.length - 1; i++) { int min = i; //maybe current item is min... //find smaller than min for (int seek = i + 1; seek < array.length; seek++) { if (array[seek].compareTo(array[min]) < 0) { min = seek; } } //swap min if (min != i) { //optional check to reduce writes E temp = array[i]; array[i] = array[min]; array[min] = temp; } }