Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.

Similar presentations


Presentation on theme: "Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own."— Presentation transcript:

1 Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.

2 Object Oriented Programs OOPs Classes are another way to extend the language. A class defines objects. Objects have variables and functions using those variables. The functions are called methods. OOPs provides inheritance, a mechanism for re-using code for things that are similar, but not identical.

3 Go immediately to example Re-do bouncing balls using a Ball class to hold the information. Advance to bouncing things by defining a Thing class, and then a Ball class and a Box class that inherit from the Thing class. Advance further by adding a Picture class.

4 Planning What do we want to do? – Create a ball with its information – Move a ball, making use of information – Show a ball, making use of information Note: for first version, will combine moving and showing. Will change for subsequence versions. Set up an array to hold all the balls.

5 Show code out of order, incomplete class Ball { float bx; //instance variables float by; float dx; float dy; int balldiam; Ball ( ) { //called the constructor … } void moveAndShow() { //method }

6 Plugs into previous class definition Ball (float x, float y, float vx, float vy, int diam) { bx = x; by = y; dx = vx; dy = vy; balldiam = diam; } void moveAndShow() { ellipse(bx,by,balldiam,balldiam); bx = bx + dx; by = by + dy; if ((bx>=width) || (bx<=0)) {dx = -dx;} if ((by>=height) || (by<=0)) {dy = -dy;} }

7 Declaring array of objects Ball[] balls = {}; This declares balls to be an array of Ball objects AND initializes it to be empty.

8 setup Create (construct) and add elements to the balls array. Makes use of append function. Since append is a general function for arrays of anything, needs what is called casting. void setup() { balls = (Ball[]) append(balls,new Ball(width/2,width/2,3,3,20)); balls = (Ball[]) append(balls,new Ball(width/4,width/3,2,1,40)); balls = (Ball[]) append(balls,new Ball(width*.75,width/6,1,2,60)); size (900,600); }

9 draw …. Is really simple void draw(){ background(0); //erase window for (int i=0;i<balls.length;i++) { balls[i].moveAndShow(); }

10 Aside By the way… setup, draw, mousePressed, and various other functions are actually methods of the Java PApplet class. The Processing programmer defines them as part of the sketch, which is, in face, part of a definition of a subclass. – MORE on this later.

11 Add rectangles Use the word Box to avoid conflicts. The coding for a circle and a rectangle are similar. So….define a class named Thing and two subclasses: Ball and Box. – and soon Picture. Replace moveAndShow with a move method and a show method. – The show methods are different. The critical code is class Ball extends Thing and super( ); // to call the constructor for the parent class

12 class Thing { float bx; float by; float dx; float dy; int wdiam; int hdiam; Thing (float x, float y, float vx, float vy, int w, int h) { bx = x; by = y; dx = vx; dy = vy; wdiam = w; hdiam = h; }

13 void move() { bx = bx + dx; by = by + dy; if ((bx>=width) || (bx<=0)) {dx = -dx;} if ((by>=height) || (by<=0)) {dy = -dy;} } void show() { // will be overridden by // inherited objects }

14 class Ball extends Thing { // no new variables Ball (float x, float y, float vx, float vy, int diam) { super(x,y,vx,vy,diam,diam); //wdiam and hdiam same } void show(){ ellipse(bx,by,wdiam,hdiam); } class Box extends Thing { // no new variables Box (float x, float y, float vx, float vy, int wdiam, int hdiam) { super(x,y,vx,vy,wdiam,hdiam); } void show(){ rect(bx,by,wdiam,hdiam); }

15 The draw function Invokes whatever the appropriate method is. void draw(){ background(0); //erase window for (int i=0;i<things.length;i++) { things[i].show(); things[i].move(); }

16 Add images? Just add an new subclass. Still need to add the image file to the Processing folder. class Picture extends Thing { PImage pic; Picture (float x, float y, float vx, float vy, int w, int h, String imagefilename) { super(x,y,vx,vy,w,h); pic = loadImage(imagefilename); } void show() { image(pic,bx,by,wdiam,hdiam); }

17 Semantic errors vs syntactic errors A syntactic error is an error Processing can catch. – Similar to grammatical error. A semantic error is an error of meaning. – I meant to add 10 instead of subtracting 10. – Timing wrong. – ?? – No help from Processing.

18 Reflection Use of classes pushes more into syntactic realm. – Use of wrong datatypes, wrong number of parameters, trying to use method that doesn't exist, etc. Processing catches errors sooner! Use of variables, functions, and classes means more over all planning.

19 Classwork / Homework Study examples. Make enhancements. Make your own project using classes.


Download ppt "Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own."

Similar presentations


Ads by Google