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

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
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,
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Classes and Objects in Java. What Is an Object?. An object is a software bundle of related state and behavior. Software objects are often used to model.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
Nov 5, Fall 2006IAT 8001 Lecture 5 Objects, Classes.
Object-oriented Programming Concepts
Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Object-Oriented Programming Concepts
Programming Languages and Paradigms Object-Oriented Programming.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 4 Objects and Classes.
Object Oriented Programming Concepts Fatih University Ceng-104-A Introduction to Object Oriented Programming Harun Reşit Zafer This is a slide version.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Java Language and SW Dev’t
CSC 212 Object-Oriented Programming and Java Part 1.
Java Classes Using Java Classes Introduction to UML.
The Java Programming Language
Your First Java Application Chapter 2. 2 Program Concepts Modern object-oriented programs help us build models to manage the complexity found in a problem.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Programming Concepts. Object ► An object is a software bundle of related state and behavior. ► Software objects are often used to model.
11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
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.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
A Java program consists of a set of class definitions, optionally grouped into packages. Each class encapsulates state and behavior appropriate to whatever.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Objects as a programming concept
Chapter 4 Assignment Statement
Computer Graphics: Rocket, Java: Class
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
LCC 6310 Computation as an Expressive Medium
University of Central Florida COP 3330 Object Oriented Programming
البرمجة الكينونية بلغة جافا 1294
Object-Oriented Programming
Interfaces.
Object-Oriented Programming
Simple Classes in Java CSCI 392 Classes – Part 1.
Object-Oriented Programming
Defining Classes and Methods
Defining Classes and Methods
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
Presentation transcript:

______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | IAT 265 Objects IAT 2651

Outline  Object-oriented programming –Object components –Rocket –Primitive types and Object References  Objects, another metaphor  Why objects? May 28, 2015IAT 2652

Classes vs Objects  A Class is a blueprint for a bicycle  An Object is a bicycle  Many bicycles, one blueprint May 28, 2015IAT 2653

May 28, 2015IAT 2654 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

May 28, 2015IAT 2655 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; } 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(); }

May 28, 2015IAT 2656 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); } void draw() { r1.draw(); r2.draw(); }

May 28, 2015IAT 2657 Primitive types  Primitive types are determined by machine architecture byte: 8bitsreference: (JVM Dependent) short:16bits int: 32bits long:64bits float:32bits double:64bits

May 28, 2015IAT 2658 Reference  Like a remote control  a reference is a primitive thing that points at objects  the new keyword causes the reference to point at a new instance of the object

May 28, 2015IAT 2659

May 28, 2015IAT Arrays  int[] nums = new int[7] ;

May 28, 2015IAT Array of objects  Dog[] pets = new Dog[7];  It starts as an array of null references

May 28, 2015IAT Array of objects Dog[] pets = new Dog[7] ; pets[0] = new Dog(); pets[1] = new Dog();

Objects May 28, 2015IAT 26513

Real Objects  Real-world objects have –State –Behavior  Bicycle –State selected gear, current pedal cadence, speed –Behavior Change Gear, Set Cadence, Apply Brakes May 28, 2015IAT 26514

Software Object  State int gear ; float speed ; float cadence ;  Behavior ChangeGears(int g); Brake( float level ); ChangeCadence( float c ); int GetGear(); float GetSpeed(); … May 28, 2015IAT 26515

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 26516

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 ); May 28, 2015IAT 26518

Even Arrays are objects int[] bob = new int[10] ; bob[4] = 123 ; println( bob.size() ); Bicycle[]bikes = new Bicycle[10] ; bikes[0] = new Bicycle(); May 28, 2015IAT 26519

Sets and Gets  what can you do with private data? –to set it: setVarName( varType newValue) –to get it: varType getVarName()  Why? May 28, 2015IAT 26520

Temperature object class temp // constructor not shown { private floatkelvin ; void 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 26521

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? May 28, 2015IAT 26522

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. May 28, 2015IAT 26523

Principle: Code re-use  If an object already exists, you can use that object in your program.  Specialists build, you use May 28, 2015IAT 26524

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! May 28, 2015IAT 26525

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! May 28, 2015IAT 26526

Summary of principles  Hide unnecessary details  Clearly define the interface  Allow and support code re-use  Build on the work of others May 28, 2015IAT 26527