Title: Computation as an Expressive Medium
1Computation as an Expressive Medium
- Lab 3 Methods and Classes
- Annie Lausier
2ARE YOU READY TO RUMMMMMMBBLLLLLLE?!
- Courseware!!
- A few good programming practices
- Methods Oi, whats the deal, eh?
- Classes Heres where it gets really fun
- Assignment 2!
3Using Courseware!
http//idt.gatech.edu/mazalek/courses/fall06/lcc6
310/courseware.html
4Drive friendly. Code friendly.
- Comments are your friend!
- 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 when you appropriate code!!
- Auto-format is also your friend!!
- Your programs will have at least three sections
- Variable declaration
- setup()
- draw()
Also known asgank, borrow, reuse, yoinks,
etc. Give due credit!
5Methods. Reloaded.
- Methods, also known as
- Functions (methods which return something)
- Procedures (methods which dont return stuff)
- Method declaration
-
- Method call
void vendingMachine( int coinCents )
println("You inserted "coinCents" cents.")
int quarter 25vendingMachine(quarter)
6Methods. Reloaded.
- Method declaration
-
- The method declaration is like a blueprint
- 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
void vendingMachine( int coinCents )
println("You inserted "coinCents" cents.")
7Methods. Reloaded.
- Method call
- vendingMachine(quarter) vendingMachine(25)
- Method call must occur in either setup(), draw(),
or another method - You can call it as many times as you like!
- vendingMachine()s for everyone!! Hooray!
argument
int quarter 25vendingMachine(quarter)
8Avast, Cap'n! Java Ahoy!
- Classes Theyre not just for breakfast, anymore.
- Remember that slide a couple labs ago?
- Types
- Primitives int, float, char, boolean
- Objects array, string, class
This bit of information is courtesy your memory
of things learned. Don't leave home without it.
9Objects
- Weve worked with some objects before, like
Arrays. - We can make our own objects, to keep related data
together, and methods to control that data.
10Classes
Hey! You reused that metaphor!
- Classes are the blueprints for our new objects.
- To declare a new Class (a new type of object)
class MyToy // fields (class variables)
// methods (class functions)
11Fields and Methods
class MySquare int xPos, yPos
MySquare(x, y) xPos x yPos y
void drawMe() rect(xPos, yPos, 50, 50)
fields
x
y
constructor
drawMe()
(one kind of method)
methods
12Fields and Methods
class MySquare int xPos, yPos
MySquare(int x, int y) xPos x yPos y
void drawMe() rect(xPos, yPos, 50, 50)
10
10
20
90
drawMe()
drawMe()
square1
square2
MySquare square1 new MySquare(10, 10) MySquare
square2 new MySquare(20, 90)
13Fields 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)
square1.drawMe() square2.drawMe()
14Arrays of Objects?
- Sure! Lets make a bunch of squares!
MySquare squares new MySquare10 //
initialize all of our squares. for (int i 0 i
i10) squares3.drawMe() // draw the 4th
square.
15Asteroids
- Lets 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
16Asteroids
- When we create an asteroid, lets 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
17Asteroids
Class Asteroid // draw method void
drawMe()
off to Processing!
18OOP, we'll do it again.
The more the merrier! MUHAHAHA!
- Programming practices
- Methods, redux
- Classes, hoo-boy!
clone-o-matic
19Assignment 2!
- A2-01 Using beginShape() and endShape(), create
a composition with five or more vertices. - A2-02 Using beginShape() and endShape(), create
a composition with ten or more vertices. - A2-03 Create an image different from A2-02, but
using the same vertex data. - A2-04 Write a function with one parameter and
demonstrate it visually. - A2-05 Write a function for drawing triangles and
visually demonstrate its flexibility. - A2-06 Write a function with three or more
parameters and visually demonstrate its
flexibility. - A2-07 Create a dynamic animation using the cos()
function as a generator for motion. - A2-08 Create a dynamic animation using the cos()
and sin() function as a generator for motion. - A2-09 Move two visual elements across the screen
using the random() function as a generator of
movement. Give each element a unique nonlinear
motion. - A2-10 Create an event that begins when the mouse
is pressed and ends when the mouse is released. - A2-11 Create a responsive image that behaves
differently when the mouse is moving and the
mouse is dragging. - A2-12 Create a button that changes the color of
the background when it is clicked. - A2-13 Program your moving elements from A2-09
but use classes to represent the two visual
elements. - A2-14 Create a subclass of one of the asteroids
classes that adds a new capability. Some examples
of what you could do create a subclass of Rocket
(or ArmedRocket) that shoots flame when the
thrusters are fired and/or plays a sound when
thrusters are fired, create a subclass of
Asteroid that know when it's been hit (instead of
doing this test in draw()), create a subclass of
Asteroid that splits into two smaller Asteroids
when it's hit.