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/2015-Fall/Lectures/Zoog.java'.
In [25]:
%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 #13:

Sketch #13 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 [8]:
%%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/2015-Fall/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 [36]:
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 #19:

Sketch #19 state: Loading...

1.4 Sorting

In [41]:
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 #23:

Sketch #23 state: Loading...