Java

Processing is really Java, but allowing some short-cuts, and some special functions defined for you.

Consider the very simple Processing program:

int x = 1;
println(x);

Normally, you couldn't run this straight away in Java, because every line of code in Java must be in a class. So, in order to run that same code in Java, you'd need to turn that into:

public class MyClass {
    public static void main(String[] args) {
        int x = 1;
        System.out.println(x);
    }
}

Note that MyClass could be called anything in this example. The method main is a special method that will be called automatically. You can't just use println, but we must refer to its full name.

We need to use public to indicate that it is accessible from outside the object. We use static to indicate that it doesn't need to be part of a instance.

We'll explore three examples converting Processing code to regular Java below.

Tutor Magic

In Jupyter, there are some metacommands that don't belong to Processing, but are instead part of Jupyter. These are called "magics" and start with a percent character.

A new magic is called %%tutor and will allow you to visualize Java code.

This is useful for watching the sorting programs from last week.

You may need to download/upload these to your own account to see the output.

In [1]:
%%tutor

public class SquaredSort {
    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void squared_sort() {
        int comparisons = 0;
        int swaps = 0;
        int[] array = new int []{8, 3, 7, 2};
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i; j < array.length; j++) {
                comparisons++;
                if (array[i] > array[j]) {
                    swaps++;
                    swap(array, i, j);
                }
            }
        }
    }
    
    public static void main(String[] args) {
        squared_sort();
    }
}
In [2]:
%%tutor

public class BubbleSort {
    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void bubble_sort() {
        int comparisons = 0;
        int swaps = 0;
        int[] array = new int []{8, 3, 7, 2};

        int stop = array.length;
        int last_swap;
        while (stop != 0) {
            last_swap = 0;
            for (int i = 0; i < stop - 1; i++) {
                comparisons++;
                if (array[i] > array[i + 1]) {
                    swaps++;
                    swap(array, i, i + 1);
                    last_swap = i + 1;
                }
            }
            stop = last_swap;
        }
    }
    
    public static void main(String[] args) {
        bubble_sort();
    }
}
In [6]:
%%tutor

public class BubbleSort {
    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void bubble_sort() {
        int comparisons = 0;
        int swaps = 0;
        int[] array = new int []{2, 3, 7, 8};

        int stop = array.length;
        int last_swap;
        while (stop != 0) {
            last_swap = 0;
            for (int i = 0; i < stop - 1; i++) {
                comparisons++;
                if (array[i] > array[i + 1]) {
                    swaps++;
                    swap(array, i, i + 1);
                    last_swap = i + 1;
                }
            }
            stop = last_swap;
        }
    }
    
    public static void main(String[] args) {
        bubble_sort();
    }
}
In [10]:
%%tutor
    
public class QuickSort {
    public static int comparisons = 0;
    public static int swaps = 0;

    public static void swap(int[] array, int i, int j) {
        swaps++;
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void quicksort(int[] array, int lo, int hi) {
        if (lo < hi) {
            int p = partition(array, lo, hi);
            quicksort(array, lo, p - 1);
            quicksort(array, p + 1, hi);
        }
    }

    public static int partition(int[] array, int lo, int hi) {
        int pivotIndex = choosePivot(lo, hi);
        int pivotValue = array[pivotIndex];
        // put the chosen pivot at array[hi]
        swap(array, pivotIndex, hi);
        int storeIndex = lo;
        // Compare remaining array elements against pivotValue = array[hi]
        for (int i = lo; i <= hi - 1; i++)  {
            comparisons++;
            if (array[i] < pivotValue) {
                 swap(array, i, storeIndex);
                 storeIndex = storeIndex + 1;
            }
        }
        swap(array, storeIndex, hi);  // Move pivot to its final place
        return storeIndex;
    }

    public static int choosePivot(int lo, int hi) {
        return ((hi + lo) / 2);
    }
    
    public static void main(String[] args) {
        int[] array = new int []{8, 3, 7, 2};
        quicksort(array, 0, 3);
    }
}