Recitation 11 – OOP Workshop Jessica Xing

I attended the OOP class workshop taught by Tristan. Tristan taught us how to construct classes so that our code for our final projects can be more organized and flexible to manipulation. He taught us how to change and remove words in an Array List on a screen. I am really glad I went to this work shop because my final project required a lot of class construction. Here is the code and a video below: 

Here is the code: 

float start_y=200;
Terms words;
ArrayList<Terms> soup;

void setup() {
size(600, 600);
soup= new ArrayList<Terms>();
soup.add(new Terms(“potato”, width/2, start_y, 64));
soup.add(new Terms(“carrot”, 0, 450, 30));
soup.add(new Terms(“onion”, 300, 550, 100));
soup.add(new Terms(“leek”, 45, 190, 60));
}

void draw() {
background(0);
soup.get(0).display();
soup.get(1).display();
soup.get(2).display();
for (int i=0; i < soup.size(); i++) { //you can now make as many as you want

soup.get(i).display();
}
}

void mousePressed() {
soup.remove(0); //how to remove soup in order
soup.remove(soup.size()-1); //how to remove soup in reverse order

}

class Terms { //write in capital letters so you can distinguish between class vs obj
String words;
int opacity;
float x, y;
float size;

Terms (String _words, float _x, float _y, float _size) { //for the constructor you write the class as a function — no return type, constructor needs to be called something different from the terms above to establish this is what YOU are bringing in
words = _words ;
opacity = 127;
x = _x;
y = _y;
size = _size;
}

void display() {
textSize(size);
fill(255, opacity);
text(words, x, y);
}
}

Leave a Reply