Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2015-Fall / Lectures |
Douglas Blank, Fall 2015, CS110: Introduction to Computing
We can now:
This whole philosophy is called Object Oriented Programming or OOP. Type and object are largely synonyms.
this
is a pronoun that means meImagine 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) {
// ...
}
}
%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));
}
The truth about Java:
main
whose type is public static void
In Jupyter, we can create a file in a number of ways. The easiest is to use the %%file
magic:
%%file SomeClass.java
public class SomeClass {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Then we compile the file using the Java Compiler, javac
:
%%shell
javac SomeClass.java
Finall, we can run the file using the Java Runtime called java
:
%%shell
java SomeClass
We saw that Java has arrays:
We can:
Type[] name
new Type[SIZE]
new Type(...)
Zoog[] army;
army = new Zoog[1000];
for (int i; i < army.length; i++) {
army[i] = new Zoog(...);
}
With primitive types, you don't have to do the third step:
int[] heights;
heights = new int[100];
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);
}
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);
}