Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2017-Fall / Notebooks |
Review of terms:
class Human {
String name;
Human(String x) {
name = x;
}
void speak() {
println("I'm a human");
}
}
class Monkey {
void speak() {
println("I'm a monkey");
}
}
Human human0;
Human human1;
Monkey monkey0;
void setup() {
human0 = new Human("Kevin");
human1 = new Human("Sylvia");
monkey0 = new Monkey();
}
void draw() {
human0.speak();
human1.speak();
monkey0.speak();
noLoop();
}
Review of ideas:
class Human {
void speak() {
println("I'm a human");
}
}
class Monkey {
void speak() {
println("I'm a monkey");
}
}
Human[] human = new Human[25];
Monkey[] monkey = new Monkey[25];
void setup() {
for (int i=0; i < human.length; i++) {
human[i] = new Human();
}
for (int i=0; i < monkey.length; i++) {
monkey[i] = new Monkey();
}
}
void draw() {
for (int i=0; i < human.length; i++) {
human[i].speak();
}
for (int i=0; i < monkey.length; i++) {
monkey[i].speak();
}
noLoop();
}
class Primate {
void speak() {
println("I'm a primate");
}
}
class Human extends Primate {
void speak() {
println("I'm a human");
}
}
class Monkey extends Primate {
void speak() {
println("I'm a monkey");
}
}
Primate primate0;
Human human0;
Monkey monkey0;
void setup() {
primate0 = new Primate();
human0 = new Human();
monkey0 = new Monkey();
}
void draw() {
primate0.speak();
human0.speak();
monkey0.speak();
noLoop();
}
class Primate {
void speak() {
println("I'm a primate");
}
}
class Human extends Primate {
void speak() {
println("I'm a human");
}
}
class Monkey extends Primate {
void speak() {
println("I'm a monkey");
}
}
class SpiderMonkey extends Monkey {
void speak() {
println("eeee eee ooo oo");
}
}
Primate[] primate = new Primate[25];
void setup() {
for (int i=0; i < primate.length; i++) {
if (i % 2 == 0) {
primate[i] = new Human();
} else {
primate[i] = new Monkey();
}
}
}
void draw() {
for (int i=0; i < primate.length; i++) {
primate[i].speak();
}
noLoop();
}
New ideas:
polymorphism
Primate is the base class, or super class
See pages 223 - 232 of your textbook for additional information.