## Jupyter at Bryn Mawr College

Public notebooks: /services/public/dblank / CS206 Data Structures / 2017-Spring / Notebooks

# 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
• Processing

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 Java. To that, we will use many different tools, starting with Jupyter.

## 1.1 Jupyter¶

Jupyter is a set of new technologies:

• Major focus on the "notebook" (one of which you are looking at)
• This is a processing notebook, but we will later use Java9 notebooks
• Composed of cells
• Blends writing, graphics, and computation together
• Languages (called "kernels") are the focus of a notebook
• Share code with others by "publishing" notebooks

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

In [1]:
println("Hello, world!");

Sketch #1:

Hello, world!
Hello, world!
Hello, world!


## 1.3 Processing¶

• Simplified Java
• Designed for artistic creations
• Java uses curly braces rather than Python's indentation for the computer; BUT indentation is still useful for the human!
• Processing is pre-defined around setup() and draw() functions

Here is a reference for drawing in Processing:

http://processingjs.org/reference/

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

void setup() {
println(multiply(34, 5)); // Calling a function
}

Sketch #1:

170
170

In [5]:
// 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();
}

Sketch #4:

# 2. Java¶

## Classes and Instances¶

Now we turn to the most useful aspect of Java: classes, and the root of the power of Object Oriented Programming, OOP.

Classes are recipes for building a "thing" or more generall just called an "object". The word "class" is used in Java to indicate that you are defining one. It is followed by a name that you make up. By convention we start with a capital letter.

After you define a class, you can make an "instance" of it using the word "new".

Here is the simplest example:

In [8]:
class Person {
}

void setup() {
new Person();
}

Sketch #5:

That wasn't very useful! To make this more useful, we want to be able to pass in some specific details about this particular instance, such as it's name. Let's make two instances of the Person class:

In [10]:
class Person {
Person(String name) {
}
}

void setup() {
new Person("Donnatella");
new Person("Mario");
}

Sketch #7:

In [11]:
class Person {
String name;
Person(String name) {
this.name = name;
}
}

void setup() {
new Person("Donnatella");
new Person("Mario");
}

Sketch #8:

In [14]:
class Person {
String name;
Person(String name) {
this.name = name;
}
}

void setup() {
Person p1 = new Person("Donnatella");
Person p2 = new Person("Mario");
}

Sketch #10:

In [12]:
Person[] people = new Person[2];

class Person {
String name;
Person(String name) {
this.name = name;
}
}

void setup() {
people[0] = new Person("Donnatella");
people[1] = new Person("Mario");
}

Sketch #9: