Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.

Similar presentations


Presentation on theme: "Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman."— Presentation transcript:

1 Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman

2 ARE YOU READY TO RUMMMMMMBBLLLLLLE?! A few good programming practices A few good programming practices Methods: Oi, what’s the deal, eh? Methods: Oi, what’s the deal, eh? Classes: Here’s where it gets really fun… Classes: Here’s where it gets really fun…

3 Drive friendly. Code friendly. Comments are your friend! Comments are your friend! Put a block of commenting at the top of each program that explains what it does Put a block of commenting at the top of each program that explains what it does Comment in code what a section will do Comment in code what a section will do Comment when you appropriate code!! Comment when you appropriate code!! Auto-format is also your friend!! Auto-format is also your friend!! Your programs will have at least three sections Your programs will have at least three sections Variable declaration Variable declaration setup() setup() draw() draw() Also known as: gank, borrow, reuse, yoinks, etc. Give due credit!

4 Methods. Reloaded. Methods, also known as Methods, also known as Functions (methods which return something) Functions (methods which return something) Procedures (methods which don’t return stuff) Procedures (methods which don’t return stuff) Method declaration Method declaration Method call Method call void vendingMachine( int coinCents ){ println("You inserted "+coinCents+" cents."); } int quarter = 25; vendingMachine(quarter);

5 Methods. Reloaded. Method declaration Method declaration The method declaration is like a blueprint The method declaration is like a blueprint Doesn't matter if declaration is before or after call! Doesn't matter if declaration is before or after call! Parameter name (e.g. coinCents) is just the name given to data passed into the method… Parameter name (e.g. coinCents) is just the name given to data passed into the method… void vendingMachine( int coinCents ){ println("You inserted "+coinCents+" cents."); } void means doesn't return anything parameter

6 Methods. Reloaded. Method call Method call vendingMachine(quarter) == vendingMachine(25) vendingMachine(quarter) == vendingMachine(25) Method call must occur in either setup(), draw(), or another method… Method call must occur in either setup(), draw(), or another method… You can call it as many times as you like! You can call it as many times as you like! vendingMachine()s for everyone!! Hooray! vendingMachine()s for everyone!! Hooray! int quarter = 25; vendingMachine(quarter); argument

7 Avast, Cap'n! Java Ahoy! Classes: They’re not just for breakfast, anymore. Classes: They’re not just for breakfast, anymore. Remember that slide a couple labs ago? Remember that slide a couple labs ago? Types Types Primitives: int, float, char, boolean … Primitives: int, float, char, boolean … Objects: array, string, class … Objects: array, string, class … This bit of information is courtesy your memory of things learned. Don't leave home without it.

8 Objects We’ve worked with some objects before, like Arrays. We’ve worked with some objects before, like Arrays. We can make our own objects, to keep related data together, and methods to control that data. We can make our own objects, to keep related data together, and methods to control that data.

9 Classes Classes are the blueprints for our new objects. Classes are the blueprints for our new objects. To declare a new Class (a new type of object): To declare a new Class (a new type of object): class MyToy { // fields (class variables) // methods (class functions) } Hey! You reused that metaphor!

10 Fields and Methods class MySquare { int xPos, yPos; MySquare(x, y) { xPos = x; yPos = y; } void drawMe() { rect(xPos, yPos, 50, 50); } xy drawMe() fields constructor methods (one kind of method)

11 Fields and Methods class MySquare { int xPos, yPos; MySquare(x, y) { xPos = x; yPos = y; } void drawMe() { rect(xPos, yPos, 50, 50); } xy drawMe() MySquare square1 = new MySquare(10, 10); MySquare square2 = new MySquare(20, 90); 10 drawMe() 2090 drawMe() square1square2

12 Fields and Methods class MySquare { int xPos, yPos; MySquare(int x, int y) { xPos = x; yPos = y; } void drawMe() { rect(xPos, yPos, 50, 50); } MySquare square1 = new MySquare(10, 10); MySquare square2 = new MySquare(20, 90); xy drawMe() 10 drawMe() 2090 drawMe() square1 square2 square1.drawMe(); square2.drawMe();

13 Arrays of Objects? Sure! Let’s make a bunch of squares! Sure! Let’s make a bunch of squares! MySquare[] squares = new MySquare[10]; // initialize all of our squares. for (int i = 0; i < 10; i ++) { squares[i] = new MySquare(i*10, i*10); } squares[4].drawMe(); // draw the 4 th square.

14 Asteroids Let’s adapt this to make an array of Asteroids for our rocket from lecture. Let’s adapt this to make an array of Asteroids for our rocket from lecture. class Asteroid { //fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; … } …except you didn't finish going over the Asteroids code in class, did you? Just follow along and we'll go over the code in a second (not on slides).

15 Asteroids When we create an asteroid, let’s have it start in a random position, and move in a random direction. When we create an asteroid, let’s have it start in a random position, and move in a random direction. Class Asteroid { … // constructor Asteroid() { xPos = random(0, 400); yPos = random(0, 400); rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; }

16 Asteroids Class Asteroid { … // draw method void drawMe() {

17 OOP, we'll do it again. Programming practices Programming practices Methods, redux Methods, redux Classes, hoo-boy! Classes, hoo-boy! clone-o-matic The more the merrier! MUHAHAHA!


Download ppt "Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman."

Similar presentations


Ads by Google