1. Java, Arrays, and Sorting

Douglas Blank, Fall 2015, CS110: Introduction to Computing

1.1 Review

We can now:

  • define new types using the class keyword
  • create new instances of a type with the new keyword
  • call methods of instance by using the dot

This whole philosophy is called Object Oriented Programming or OOP. Type and object are largely synonyms.

  • Objects are typically nouns
  • Methods (ie, functions in classes) are verbs
  • Properties values are adjectives
  • this is a pronoun that means me

1.1.1 An example:

Imagine the alien creature Zoog from http://www.openprocessing.org/sketch/12199:

class Zoog {
    Zoog(float tempX, float tempY, float tempW, 
         float tempH, float tempEyeSize, 
         float tempBodyColor1, float tempBodyColor2, float tempBodyColor3) {
        // ...
    }
    void jiggle(float speed) {
        // ...
    }
    void display(color eyeColor) {
        // ...
    }
}
Created file '/home/dblank/public_html/CS110 Intro to Computing/2017-Spring/Lectures/Zoog.java'.
In [2]:
%include Zoog.java

// x, y, width, height, eye size, r, g, b:
Zoog zoog1 = new Zoog(20, 70, 30, 10, 5, 128, 0, 0);
Zoog zoog2 = new Zoog(50, 70, 30, 20, 5, 0, 128, 0);
Zoog zoog3 = new Zoog(80, 70, 30, 30, 5, 0, 0, 128);

void setup() {
    // eye color:
    zoog1.display(color(128, 0, 0));
    zoog2.display(color(255, 0, 0));
    zoog3.display(color(128, 0, 0));
}
Sketch #1:

Sketch #1 state: Loading...

1.2 Java

The truth about Java:

  1. All code must be in a class, in a file
  2. Code must be "compiled"
  3. There is a special method called main whose type is public static void
    • public, because it can be seen from outside the class
    • static, because it doesn't require an "instance"
    • void, as before, it doesn't return anything

In Jupyter, we can create a file in a number of ways. The easiest is to use the %%file magic:

In [3]:
%%file SomeClass.java

public class SomeClass {

    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
Created file '/home/dblank/public_html/CS110 Intro to Computing/2017-Spring/Lectures/SomeClass.java'.

Then we compile the file using the Java Compiler, javac:

In [9]:
%%shell
    
javac SomeClass.java

Finall, we can run the file using the Java Runtime called java:

In [10]:
%%shell

java SomeClass
Hello, world!

1.2.1 Differences between Java and Processing?

1.3 Arrays

We saw that Java has arrays:

We can:

  • define an array with Type[] name
  • create space for the array with new Type[SIZE]
  • create individual items with new Type(...)
Zoog[] army;

army = new Zoog[1000];

for (int i; i < army.length; i++)  {
    army[i] = new Zoog(...);
}

1.3.1 Primitive types

  • int
  • float
  • bool

With primitive types, you don't have to do the third step:

int[] heights;

heights = new int[100];

1.3.2 Arrays and Functions

In [4]:
int biggest(int[] myarray) {
    // what goes here?
    int biggest_so_far = -10000;
    for (int i = 0; i < myarray.length; i++) {
        if (myarray[i] > biggest_so_far) {
            biggest_so_far = myarray[i];
        }
    }
    return biggest_so_far;
}

int smallest(int[] myarray) {
    // what goes here?
    int smallest_so_far = 10000;
    for (int i = 0; i < myarray.length; i++) {
        if (myarray[i] < smallest_so_far) {
            smallest_so_far = myarray[i];
        }
    }
    return smallest_so_far;
}

void printArray(int[] myarray) {
    print("[");
    for (int i = 0; i < myarray.length; i++) {
        print(myarray[i]);
        print(", ");
    }
    println("]");
}

int[] heights = new int[100];

void setup() {
    for (int i = 0; i < 100; i++) {
        heights[i] = int(random(50));
    }

    int b = biggest(heights);
    int s = smallest(heights);
    
    printArray(heights);

    println("Biggest: " + b + " Smallest: " + s);
}
Sketch #2:

Sketch #2 state: Loading...
[46, 27, 27, 12, 38, 12, 28, 5, 40, 6, 0, 37, 1, 39, 34, 17, 19, 46, 1, 9, 3, 28, 32, 6, 32, 28, 41, 22, 33, 28, 13, 19, 24, 32, 18, 14, 43, 33, 4, 21, 23, 10, 26, 19, 40, 8, 25, 29, 10, 24, 19, 27, 8, 24, 26, 2, 42, 2, 46, 1, 4, 46, 32, 13, 14, 1, 0, 6, 45, 29, 42, 16, 26, 17, 14, 23, 11, 34, 1, 25, 33, 3, 22, 18, 16, 15, 23, 46, 46, 43, 49, 8, 27, 35, 22, 24, 0, 3, 42, 8, ]
Biggest: 49 Smallest: 0

1.4 Sorting

In [5]:
void sorted(int[] myarray) {
    // what goes here?
    for (int i = 0; i < myarray.length - 1; i++) {
        for (int j = i + 1; j < myarray.length; j++) {
            if (myarray[i] > myarray[j]) {
                int temp = myarray[i];
                myarray[i] = myarray[j];
                myarray[j] = temp;
                
            }
        }        
    }
}

void printArray(int[] myarray) {
    print("[");
    for (int i = 0; i < myarray.length; i++) {
        print(myarray[i]);
        print(", ");
    }
    println("]");
}

int[] heights = new int[100];

void setup() {
    for (int i = 0; i < 100; i++) {
        heights[i] = int(random(50));
    }
    
    printArray(heights);
    sorted(heights);
    printArray(heights);
}
Sketch #3:

Sketch #3 state: Loading...
[7, 10, 10, 6, 0, 7, 40, 7, 14, 32, 2, 32, 23, 44, 31, 25, 29, 10, 47, 32, 17, 12, 3, 28, 35, 48, 42, 17, 27, 46, 36, 39, 11, 43, 0, 27, 21, 40, 29, 25, 23, 6, 14, 5, 8, 25, 22, 16, 6, 7, 38, 28, 31, 10, 22, 6, 6, 39, 36, 37, 22, 2, 39, 39, 38, 29, 48, 29, 49, 47, 29, 36, 43, 42, 32, 46, 29, 23, 10, 5, 0, 5, 43, 26, 3, 22, 46, 4, 26, 35, 15, 18, 12, 18, 11, 32, 18, 4, 12, 42, ]
[0, 0, 0, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 10, 10, 10, 10, 10, 11, 11, 12, 12, 12, 14, 14, 15, 16, 17, 17, 18, 18, 18, 21, 22, 22, 22, 22, 23, 23, 23, 25, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29, 29, 29, 29, 31, 31, 32, 32, 32, 32, 32, 35, 35, 36, 36, 36, 37, 38, 38, 39, 39, 39, 39, 40, 40, 42, 42, 42, 43, 43, 43, 44, 46, 46, 46, 47, 47, 48, 48, 49, ]