CS206: Data Structures

Douglas Blank
Bryn Mawr College

1. Introduction to Java

The goal of this course is to explore the structures of computation. These include:

  • Data structures
  • Object-Oriented Programming
  • Problem solving
  • Processes

If you took the Introduction to Computer Science at Bryn Mawr College or Haverford College, then you have learned some Processing or Python, respectively. We will be transitioning from those languages and systems to a new one based on Java9. To that, we will use many different tools, starting with Jupyter.

1.1 Jupyter

Jupyter is a set of new technologies:

  • Web-based computational tools; nothing to download or install
  • Major focus on the "notebook" (one of which you are looking at)
    • This is a Java9 notebook
    • Composed of cells
    • Blends writing, graphics, and computation together
    • Languages (called "kernels") can be the focus of a notebook
    • Code in other languages can be run through a magic cell
  • Share code with others by "publishing" notebooks

Here is an example of how you will use the notebook with computation:

In [16]:
System.out.println("Hello, world!");
Hello, world!

1.2 Python

Some of you may have learned a bit about Python:

  • Python has implicit types
  • Whitespace is meaningful (indentation is part of language)
  • Almost everything is an "object"
  • Comes with a large library of utility functions (modules)

Here is an example of Python in a Java9 notebook via a %% magic:

In [7]:
%%python

# Here is a function definition:
def multiply(a, b):
    return a * b

# And some output:
print("Hello, world!")
print(multiply(34, 5)) # Calling a function
Hello, world!
170

1.3 Processing

  • Simplified Java
  • Designed for artistic creations
  • Uses curly braces rather than indentation for the computer; indentation is useful for the human
  • Process is pre-defined around setup() and draw() functions

Here is an example of Processing in a Java9 notebook via a %% magic:

In [8]:
%%processing

// Here is a function definition:
int multiply(int a, int b) {
    return a * b;
}

void setup() {
    fill(0);
}

void draw() {
    text("Hello, world!", 10, 20);
    text(str(multiply(34, 5)), 10, 35); // Calling a function
    noLoop();
}

There is better support for both Python and Processing via their own kernels rather than these magics.

2. Java

Java is typically a "compiled" language. This means that you generally can't do anything unless you compile code into something that can be run.

In [11]:
%%file TestClass.java

class TestClass {

    public static void main(String [] args) {
        System.out.println("Hello, world!");
    }

}
Created file '/home/dblank/public_html/CS206 Data Structures/2016-Spring/Notebooks/TestClass.java'.
In [14]:
%%shell

javac TestClass.java
java TestClass
Hello, world!

But in this course, we will be using an interpreter rather than a compiler. Sometimes this is called a REPL which stands for Read-Eval-Print-Loop. Basically, you can type "snippets" of code interactively, one cell at a time.

In [21]:
System.out.println("Hello, world!");
Hello, world!

In [18]:
String my_greeting = "Hello, Doug!";
|  Added variable my_greeting of type String with initial value "Hello, Doug!"

In [19]:
System.out.println(my_greeting);
Hello, Doug!