______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW.SIAT.SFU.CA.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Written by: Dr. JJ Shepherd
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
1 Object-Oriented Programming Concepts. 2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result.
OBJECT-ORIENTED PROGRAMMING CONCEPTS (Review). What is an Object? What is an Object? Objects have states and behaviors. Example: A dog has states - color,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Nov 5, Fall 2006IAT 8001 Lecture 5 Objects, Classes.
Object-oriented Programming Concepts
Abstract Classes and Interfaces
IAT 800 Lecture Oct, Fall 2006IAT 8002 Outline  Programming concepts –Super and this –Java SDK classes Lists Reading the Java docs –Programming.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Multiple Choice Solutions True/False a c b e d   T F.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Chapter 4 Objects and Classes.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
10-Nov-15 Java Object Oriented Programming What is it?
Object Oriented Programming Concepts. Object ► An object is a software bundle of related state and behavior. ► Software objects are often used to model.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Object Oriented Programming
Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
LCC 6310 Computation as an Expressive Medium Lecture 6.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Classes, Interfaces and Packages
What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
OOP Basics Classes & Methods (c) IDMS/SQL News
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
IAT 265 Lecture 7 Objects and ArrayList. May 19, 2016IAT 2652 Outline  Programming concepts –Super and this –Java SDK classes Lists Reading the Java.
Computer Graphics: Rocket, Java: Class
University of Central Florida COP 3330 Object Oriented Programming
LCC 6310 Computation as an Expressive Medium
LCC 6310 Computation as an Expressive Medium
University of Central Florida COP 3330 Object Oriented Programming
CS2102: Lecture on Abstract Classes and Inheritance
Object Oriented Programming (OOP) LAB # 8
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Lecture 6 Methods and Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Lecture 6 Methods, Classes, Inheritance
Presentation transcript:

______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | IAT 334 OO programming IAT 3341

Outline  Object-oriented programming –objects –classes sets (mutators) and gets (accessors) object methods –Inheritance: Subclasses Rocket, ArmedRocket  Collections –ArrayList June 22, 2010IAT 3342

June 22, 2010IAT 3343 Parts of a class  Classes define fields, constructors and methods  Fields are the variables that will appear inside every instance of the class –Each instance has its own values  Constructors are special methods that define how to build instances (generally, how to set the initial values of fields)  Methods are how you do things to instances

June 22, 2010IAT 3344 Defining the rocket class class Rocket { // fields float rotation = 0; float xPos; float yPos; final int halfWidth = 10; final int halfHeight= 10; // constructor Rocket( int initialX, int initialY, float initialRot ) { xPos = initialX; yPos = initialY; rotation = initialRot; }

June 22, 2010IAT 3345 Using the class to create instances  Classes define a type  You can now declare variables of this type and initialize them using the constructor  Like arrays, the keyword new is used to tell Java to create a new object Rocket r1, r2 ; void setup() { r1 = new Rocket(75, 10, 0); r2 = new Rocket(50, 50, PI/2); }  Nice, but my rockets don’t do anything (need methods!)

June 22, 2010IAT 3346 Adding a draw routine to our Rocket void draw() { pushMatrix(); translate(xPos, yPos); rotate(rotation); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); popMatrix(); } Don’t need arguments because we use the fields But we could define additional arguments if we wanted to No Arguments!

June 22, 2010IAT 3347 Calling methods on objects  You call methods on instances  Think of the method as something you are asking the object to do  For example, we can now ask the rockets to draw themselves r1.draw();  In general, to call a method, take the name of the variable holding the object + “.” + the method name myObject.myMethod();

Objects June 22, 2010IAT 3348

Real Objects  Real-world objects have –State –Behavior  Bicycle –State selected gear, current pedal cadence, speed –Behavior Change Gear, Set Cadence, Apply Brakes June 22, 2010IAT 3349

Software Object  State int gear ; float speed ; float cadence ;  Behavior ChangeGears(int g); Brake( float level ); ChangeCadence( float c ); int GetGear(); float GetSpeed(); … June 22, 2010IAT 33410

Java directly supports Objects  Java has direct syntactic and semantic support for Objects Syntax: class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } IAT 33411

Java directly supports Objects  Java has direct syntactic and semantic support for Objects Semantics: class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } IAT Only these methods can read or write Bicycle private data

Java Semantic support  Programming usually takes place with objects: ClockThing clock = new ClockThing(); clock.setSecond( 12 ); clock.setMinute( 18 ); clock.setHour( 3 ); June 22, 2010IAT 33413

Even Arrays are objects int[] bob = new int[10] ; bob[4] = 123 ; println( bob.size() ); Bicycle[]bikes = new Bicycle[10] ; bikes[0] = new Bicycle(); June 22, 2010IAT 33414

Sets and Gets  what can you do with private data? –to set it: setVarName( varType newValue) –to get it: varType getVarName()  Why? June 22, 2010IAT 33415

Temperature object class temp { private floatkelvin ; setCelsius( float C ); { if( C < ) return ;// perhaps an error message would be in order else kelvin = C ; } float getCelsius() { return( kelvin ); } float setKelvin( float k ) { if( k < 0 ) return ; else kelvin = k ; } IAT 33416

Temperature object  Controls access  Ensures correctness –can only run a setXYZ() to change temp –can only do getXYZ() to get the value in the desired scale  Who cares? June 22, 2010IAT 33417

Who cares?  When you want to: –Solve the problem once and forget it –Reuse the solution elsewhere –Establish rules for use and change of data  The principle: –Information hiding –By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world. June 22, 2010IAT 33418

Principle: Code re-use  If an object already exists, you can use that object in your program.  Specialists build, you use June 22, 2010IAT 33419

Principle: Define the Interface  Define the interface: –The list of methods with Defined Operation  The interface is the thing that other people use  If you have the same interface with the same meaning –You can plug in a better implementation! June 22, 2010IAT 33420

Define the Interface  If you have the same interface with the same meaning –You can plug in a better implementation! –You can plug in a More Interesting implementation! June 22, 2010IAT 33421

Summary of principles  Hide unnecessary details  Clearly define the interface  Allow and support code re-use  Build on the work of others June 22, 2010IAT 33422

Inheritance June 22, 2010IAT 33423

Classes  Types –Primitives: int, float, char, boolean … –Objects: array, string, class … June 22, 2010IAT 334

Objects  We’ve worked with some objects before, like Arrays.  We can make our own objects, to keep related data together, with methods to control that data. June 22, 2010IAT 334

Classes  Classes are the blueprints for our new objects.  To declare a new Class (a new type of object): June 22, 2010IAT 334 class MyToy { // fields (class variables) // methods (class functions) }

Fields and Methods June 22, 2010IAT 334 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)

Fields and Methods June 22, 2010IAT 334 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

Fields and Methods June 22, 2010IAT 334 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();

Arrays of Objects?  Let’s make a bunch of squares! June 22, 2010IAT 334 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.

Recap: Rocket  In Lab 2, we created the Rocket class –Constructor: Rocket(int initialX, int initialY, float initialRot ) –Methods draw() rotateClockwise() rotateCounterClockwise() fireThrusters() June 22, 2010IAT 334

Asteroids  Let’s adapt this to make an array of Asteroids for our rocket from Lab 2 June 22, 2010IAT 334 class Asteroid { //fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; … }

Asteroids  When we create an asteroid, let’s have it start in a random position, and move in a random direction. June 22, 2010IAT 334 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; }

Asteroids June 22, 2010IAT 334 class Asteroid { … // draw method void draw () {

Revisit our example  So far we have a rocket that flies around in a field of asteroids  What if we want our rocket to be able to fire –But we don’t want to get rid of our non-firing rocket  Create a subclass! June 22, 2010IAT 334

Inheritance  Subclasses inherit fields and methods from parent class ArmedRocket extends Rocket { … } June 22, 2010IAT 334

Our subclass needs a constructor  Our empty ArmedRocket example creates an error –Processing doesn’t know how to construct an ArmedRocket  We want the ArmedRocket constructor to do the same work as the Rocket constructor ArmedRocket(int initialX, int initialY, float initialRot) { super(initialX, initialY, initialRot); } The keyword super means to refer to the parent class. In this case, to call the Parent Class Constructor June 22, 2010IAT 334

Now we have ArmedRocket  We can use an ArmedRocket now in our example  But, it’s basically just a copy of Rocket  The only reason to define an ArmedRocket is to add new capabilities or to override old ones June 22, 2010IAT 334

Add a fire() method  We want our fire method to draw a missile that shoots out of the rocket  We could have the fire method draw the missile… –Is there a problem with this? June 22, 2010IAT 334

Missiles should also be objects  The object oriented solution is to make the missile an object as well –All the different types of “things” in our domain should have a corresponding class  Like asteroids and rockets, the missile class should know how to draw itself –A Missile is similar to a rocket (position, rotation, draw method, etc.)  Now our ArmedRocket.fire() method can just create and return a missile June 22, 2010IAT 334

The fire() method Missile fire() { Missile m = new Missile(xPos, yPos, rotation); return m; }  Now add code in loop to draw missiles June 22, 2010IAT 334

Missiles destroy asteroids  So far we have a rocket that flies around in a field of asteroids and fires  Now we want our missiles to blow up asteroids –This means we need a variable number of asteroids. –How do we do this with an array? –Use an ArrayList! –Also need to figure out when we have a collision June 22, 2010IAT 33442

The Java SDK  Java comes with thousands of classes in the Java Platform API  Documentation is available on Sun’s website  Let’s look at ArrayList June 22, 2010IAT 33443

ArrayList  It’s a resizeable list –Can add and delete things without worrying about declaring the size  The main methods we care about are add(), get(), and remove(), and size()  Steps in using ArrayList –Declare a variable of type ArrayList –Create a new ArrayList and assign it to the variable –Call add(), get() and remove() and size() on ArrayList as you need them June 22, 2010IAT 33444

Parents and children  Remember that we declared a child class ArmedRocket whose parent was Rocket  Remember that classes are types –So ArmedRocket is a type and Rocket is a type  So, here are some legal assignments –ArmedRocket r1 = new ArmedRocket(50, 60, 0); –Rocket r2 = new Rocket(50, 60, 0); –Rocket r3 = new ArmedRocket(50, 60, 0);  But this is illegal –ArmedRocket r4 = new Rocket(50, 60, 0);  Same goes for method arguments as well… June 22, 2010IAT 33445

Rocket Inheritance June 22, 2010IAT Rocket: xPos, YPos, velocityX, velocityY, rotation Rocket(x,y,rotation) draw() ArmedRocket extends Rocket xPos, YPos, velocityX, velocityY, rotation ArmedRocket(x,y,rotation) draw() fire() Inherits from

Using ArrayList.add()  The argument type of the add method is Object –Object is the parent class of all classes –With an object argument type, you can pass in an object of any class  So, to initialize our asteroids… ArrayList asteroids = new ArrayList(); for(int i = 0; i < numAsteroids; i++) asteroids.add(new Asteroid()); June 22, 2010IAT 33447

Getting things out of an ArrayList  ArrayList.get(int i) – returns the ith object (starting with 0)  But this doesn’t work! asteroids.get(i).draw(); Why? June 22, 2010IAT 33448

Need to cast back from Object  Since things are put in an ArrayList as Object, they come back out as Object –It’s like they forget their more detailed type –So, when using ArrayList (or any container class), need to cast back to the more detailed type Asteroid asteroid = (Asteroid)asteroids.get(i); if (!asteroid.collision(r1)) asteroid.draw(); June 22, 2010IAT 33449

Pushing collision detection into the Asteroid  In the current code, detecting collision takes place in loop()  But it is cleaner (more object-oriented) if Asteroid itself knows how to detect collision –Detecting collision depends on knowing the boundaries of the asteroid, which properly belongs in the asteroid class boolean collision(Rocket r) { if ((r.xPos >= xPos - 26 && r.xPos <= xPos + 22) && (r.yPos >= yPos - 24 && r.yPos <= yPos + 26)) return true; else return false; } June 22, 2010IAT 33450

Destroying asteroids  When a missile hits an Asteroid, we need to destroy it –This was the whole reason for using ArrayList –Big asteroids turn into two small asteroids –Small asteroids disappear void destroy(ArrayList asteroids) { asteroids.remove(this); if (large) { asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); } June 22, 2010IAT 33451

Super and this  this is a special variable that always refers to the current instance (object) –Useful in methods to refer to yourself –this.method() – calls a method on yourself (but normally you just directly call the method) –this() – calls a constructor on yourself (useful for one version of a constructor to call another)  super is a special variable that always refers to the superclass portion of an object (the object cast into it’s superclass) –super.method() – calls the superclass’s method –super() – calls the superclass’s constructor June 22, 2010IAT 33452

Summary  ArrayList, a Java Platform collection class  Learned about super and subclasses as types –Any instance of a subclass is an instance of the superclass, but not visa-versa –Can cast more abstract classes (parents) into more concrete classes (children)  The Java keywords super and this –Special variables that can be used within a method to refer to yourself (the superclass portion of yourself and all of yourself) June 22, 2010IAT 33453