Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.

Slides:



Advertisements
Similar presentations
Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Objective-C and Xcode (Part 1) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
C++ fundamentals.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Eclipse – making OOP Easy
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Objective-C and Xcode (Part 3) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
SNPL1 Woochang Lim What (Variable) + How (Function) = Object Objects are the physical and conceptual things we find in the universe around us. Object-Oriented.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to Java Java Translation Program Structure
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programing (OOP)
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
More Objective-C Details FA 172 Intro to Mobile App Development.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Introduction to Objective-C and Xcode (Part 4) FA 175 Intro to Mobile App Development.
What About Alice? In Alice, we have seen that we have:  Objects (skater, snowman, snowwoman, camera)  Methods (move, turn, roll, move toward)  Properties.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Lecture 3 John Woodward.
Classes and OOP.
Creating and Using Classes
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Object-Oriented Programming
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Object-Oriented Programming
JAVA CLASSES.
Object-Oriented Programming
By Rajanikanth B OOP Concepts By Rajanikanth B
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Agenda Object-oriented programming OOP in Objective-C – Classes – Instances and objects – Properties and methods

Definition of a program, revisited Traditional definition of a program: sequence of instructions to be executed on a computer Under the object-oriented programming (OOP) paradigm: a program, when it executes, is a collection of interacting objects Programming in the OOP paradigm means specifying what data are in these objects and how these objects behave

So… what is an object? An object is a thing that has type, identity, state, and behavior – Type: it belongs to a class of similar things – Identity: it is an instance distinct from other objects – State: it has a set of properties that take on values – Behavior: it can act or carry out methods

Object examples Light Bulb -state? -behavior ? Bank Account -state? -behavior? Car -state? -behavior ?

Class: Light Bulb State – lit or not (on or off) Behavior – turn on – turn off – check whether lit

Class: Bank Account State – balance Behavior – deposit – withdraw – inquire balance

Class: Car State – distance travelled – gas left Behavior – drive – load gas – check gas level – check odometer

Objective-C: interface versus implementation.h file contains an interface declaring properties and methods of a class – the interface is the public façade of an object.m file contains an implementation of the class – code for the methods plus other “private” data – the implementation contains details encapsulated within the object, hidden from users To create files in Xcode while project is open, File->New->File, then choose Obective-C class

Interface for the Car class (Car.h) Car : double int distanceTravelled; -(void) driveDistance:(int) dist; -(void) loadGas:(double)

Implementation (Car.m) #import Car -(void) driveDistance:(int) dist { self.distanceTravelled = self.distanceTravelled + dist; double gasUsed = dist/8.5; self.gasLeft = self.gasLeft - gasUsed; } -(void) loadGas:(double) gas { self.gasLeft = self.gasLeft + gas;

Using the Car class #import "Car.h"... code for a car object"); Car *myCar = [Car alloc]; [myCar loadGas:50.0]; [myCar driveDistance:10]; %6.2f, distance: %d", myCar.gasLeft, myCar.distanceTravelled); [myCar driveDistance:94]; [myCar loadGas:10.0]; %6.2f, distance: %d", myCar.gasLeft, myCar.distanceTravelled);

Encapsulation and direct data update Oftentimes, it is appropriate for properties to be “readonly” and updated only through appropriate methods Example: – gasLeft property should be updated only as a result of driveDistance or loadGas – Try adding: myCar.gasLeft = 100.0; at the end of the code, and then print myCar.gasLeft

Solution: specify property attributes in.h and.m files In.h file, replace property declarations with: double int distanceTravelled; In.m file, add the following after #import line: double int Notice that direct update of properties are no longer allowed

Syntax Property declaration ; Method declaration (no arguments) – -( ) ; Method declaration (one argument) – -( ) :( ) ;

Syntax Object creation/instantiation – = [ alloc]; Referring to a property of an object – From within the class: self. – For an object variable:. Invoking methods: – [ ]; – [ : ];

Naming conventions Variable names and method names – Camel case: begin with small letter, capitalize first letters of succeeding words – Examples: distanceTravelled, myCar, loadGas Class names – Capitalize first letters of all words within the name – Examples: BankAccount, Car, LightBulb

void The methods loadGas and driveDistance have the following signatures – -(void)driveDistance:(int) dist – -(void)loadGas:(double) gas Here, void means “no return value” Some methods return a value Example: add a method to the Car class with the following signature – -(double) distanceTravelledInMiles

Returning a value from a method In Car.h: – -(double) distanceTravelledInMiles; In Car.m – -(double) distanceTravelledInMiles { double miles = self.distanceTravelled*0.62; return miles; } In your test code, – car travelled %6.2 miles”, [myCar distanceTravelledInMiles]);

Initialization It is common to provide initialization code intended for a newly created object – Set initial values for properties By convention, these methods should begin with the word init Also by convention (and for reasons too technical to discuss at this point), use _ (e.g., _gasLeft) instead of self. (e.g. self.gasLeft), when referring to the properties

init method examples -(id) init { self = [super init]; _gasLeft = 0; _distanceTravelled = 0; return self; } -(id) initWithGas:(double) amt { self = [super init]; _gasLeft = amt; _distanceTravelled = 0; return self; }

Implementing initialization methods Make sure to place method declarations of init and initWithGas in Car.h Method implementations should be in Car.m In your test code, add the following: – Car *car2 = [[Car alloc] init]; Car *car3 = [[Car alloc] initWithGas:20.0]; [car3 driveDistance:15]; // some code to print gas levels of car2 & car3

Summary Introduction to Object-Oriented Programming Objective-C class creation in Xcode (Car class: Car.h, Car.m) OOP concepts tackled – classes, objects, properties, methods, initialization Naming conventions