Programming for Social Scientists Lecture 3 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Object Oriented Programming
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Chapter 1 Inheritance University Of Ha’il.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.
Programming for Social Scientists Lecture 4 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Programming for Social Scientists Lecture 9 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Programming for Social Scientists Lecture 5 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz.
Programming for Social Scientists Lecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
C++ fundamentals.
More on Classes Inheritance Copyright © 2012 by Yong-Gu Lee
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
chap13 Chapter 13 Programming in the Large.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.
C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983.
Swarm Computing & Routing Algorithms Dr. Mikhail Nesterenko Presented By Ibrahim Motiwala.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Objective-C Drew Cheng CS420. Overview True superset of ANSI C Object-Oriented methods from SmallTalk There is no formal written standard for the language.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Introduction to Objective-C Spring Goals An introduction to Objective-C As implemented by the Apple LLVM Compiler 4.0 (a.k.a. Clang) Only the basics…
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright 2005, The Ohio State University CSE – Introduction to C++ Name: Shirish Tatikonda Time: T 3:30 PM Room: BE 394
Classes - Intermediate
Introduction to Computational Modeling of Social Systems Nils Weidmann Center for Comparative and International Studies (CIS) Seilergraben 49, Room E.3,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Module 9. Dealing with Generalization Course: Refactoring.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Web Design & Development Lecture 9
Classes (Part 1) Lecture 3
Agenda Warmup AP Exam Review: Litvin A2
C++ Classes & Object Oriented Programming
CS 2304 Function Pointers & Lambda Functions
Object Oriented Analysis and Design
Interfaces.
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
COP 3330 Object-oriented Programming in C++
Inheritance and Polymorphism
By Rajanikanth B OOP Concepts By Rajanikanth B
C++ Programming CLASS This pointer Static Class Friend Class
CPS120: Introduction to Computer Science
Data Structures and ADTs
SPL – PS3 C++ Classes.
Presentation transcript:

Programming for Social Scientists Lecture 3 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson

POL SCI Cederman / Stenfansson 2 Prisoner's Dilemma Game Player 2 C D C 3,30,5 Player 1 D 5,01,1

POL SCI Cederman / Stenfansson 3 Chicken Game Player 2 C D C 3,31,5 Player 1 D 5,10,0

POL SCI Cederman / Stenfansson 4 Exercise 4a: Chicken game #include int main(int argc, const char ** argv) { int m[2][2] = {{3,1},{5,0}}; int equil; int r, c; for (r=0; r<2; r++) { for (c=0; c<2; c++) { equil = (m[r][c]>m[1-r][c]) && (m[c][r]>m[1-c][r]); printf("%d ",equil); } printf("\n"); } return 0; }

POL SCI Cederman / Stenfansson 5 Exercise 4b: Fancy Output | | | | | * |

POL SCI Cederman / Stenfansson 6 Exercise 4b: Fancy Output for (r=0; r<2; r++) { printf(" \n"); printf("|"); for (c=0; c<2; c++) { equil=(m[r][c]>m[!r][c]) && (m[c][r]>m[!c][r]); if (equil) printf(" * |"); else printf(" |"); } printf("\n"); } printf(" "); return 0; }

POL SCI Cederman / Stenfansson 7 Exercise 4c: Pareto optima for (r=0; r<2; r++) { for (c=0; c<2; c++) { equil=(m[r][c]>m[!r][c]) && (m[c][r]>m[!c][r]);... better=0; for (i=0; j<2; i++) for (j=0; j<2; j++) if (!((i==r) && (j==c))) if ((m[i][j]>=m[r][c]) && (m[j][i]>=m[c][r])) better = 1; // if better==0 then (r,c) is a Pareto optimum } return 0; }

POL SCI Cederman / Stenfansson 8 Exercise 5: Russian Roulette A B A B A 1/6 5/6 2/6 4/6 3/6 4/6 B 5/6 3/6 2/6 1/6

POL SCI Cederman / Stenfansson 9 Exercise 5: Russian Roulette 0: A B 1: A B A B 2: A B 3: A B 4: A 5: A B 6: A B A B A 7: A B A B 8: A B 9: A B A B 10: A B A B A B 11: A B A 12: A B 13: A B A 14: A B 15: A B A B 16: A B A B... A's survival prob.= ,000,000 replications:

POL SCI Cederman / Stenfansson 10 Exercise 5: main.m int main(int argc, const char ** argv) { long int repl; int i,p,shot; long int sum = 0; long int n = ; for (repl=0; repl<n; repl++) { p = 0; i = 0; do { i++; shot = ((double)rand() / (double)RAND_MAX < (double)i/6.0); p = !p; } while (!shot); if (p==0) sum++;} printf("%8.6f \n", (double) sum/n); return 0; }

POL SCI Cederman / Stenfansson 11 Object Oriented Programming Variables Methods State Behavior An object A program Message

POL SCI Cederman / Stenfansson 12 OOP and agent-based modeling general advantages: triple abstraction –encapsulation hides functions and data –inheritance of classes saves work –polymorphism simplifies programming also useful metaphor: "objects as agents" –deciding, sending and receiving messages –autonomous, own data and methods

POL SCI Cederman / Stenfansson 13 Why Objective-C? History: Created by Brad Cox, most intensively used by NeXT, now owned by Apple Computer and forms basis for Rhapsody OS Main difference between C++ and Objective-C: –Easy to learn: A simple superclass of C - no new keywords –Partially interpreted –More flexible and dynamic than C++

POL SCI Cederman / Stenfansson 14 A few Objective-C Bug : SwarmObject { int xPos, yPos; int worldXSize, worldYSize; id foodSpace; } -setX: (int) x Y: (int) y; Super class Sub classes Instance Variables Methods

POL SCI Cederman / Stenfansson 15 Some basic –Declarations of instance variables and methods -message –declares a method called `message'. -message: (type) var –declares method called ‘message’ that takes one Bug:SwarmObject { int xPos, yPos; int worldXSize,worldYSize; id foodSpace; } -setX: (int) x Y: (int) y;

POL SCI Cederman / Stenfansson 16 More Objective-C syntax Defining methods - aMessage: (type) var -aMessage:(type)v1 with:(type)v2 and:(type) v3 Calling methods [obj aMessage: val] [obj aMessage: val1 with: val2 and: val3]

POL SCI Cederman / Stenfansson 17 The id variable type etc. Default variable type for object in ObjC is id Think of this as a special variable type (which is actually a pointer to a special data structure - namely the object) All objects can refer to themselves by [self...] All objects can refer to superclass by [super...]

POL SCI Cederman / Stenfansson 18 Declaring a class The header file or interface declares –Class name –It’s superclass –Instance variables interface Obj:SuperClass { vartype Ivar1 vartype Ivar2... vartype IvarN } -(vartype)aMethod -anotherMethod: (vartype) arg;

POL SCI Cederman / Stenfansson 19 Defining a class 4 #import implementation Obj -(vartype)aMethod { (body) return returnval; } -anotherMethod: (vartype) arg { (body) return self;} Based on the interface the object file contains the implementation of the class Methods are essentially C functions, same rules about return values and arguments, local vars etc. apply Method returning no value must return self

POL SCI Cederman / Stenfansson 20 Typical ObjC File structure: main.m ClassA.m ClassB.m ClassA.h ClassB.h

POL SCI Cederman / Stenfansson 21 C Functions vs. ObjC methods Objective-C method: -(type) name: (type) arg1 argName2: (type) arg2 { (body) return returnval; } C function: -(type)name((type) arg1,(type) arg2)) { (body) return returnval; } Code in body could look exactly the same in C and Objective-C

POL SCI Cederman / Stenfansson 22 SimplePD: File Structure main.m Player.m Player.h

POL SCI Cederman / Stenfansson 23 SimplePD/main.m int main (argc, const char ** argv) { id player1, player2; initSwarm(argc, argv); player1 = [Player create: globalZone]; player2 = [Player create: globalZone]; [player1 init: 1 rowPlayer: YES]; [player2 init: 2 rowPlayer: NO]; [player1 setRow: 1 Col: 1]; [player2 setRow: 1 Col: 1]; if ([player1 move] && [player2 move]) printf(“Equilibrium!\n”); else printf(“No equilibrium!\n”); return 0; }

POL SCI Cederman / Stenfansson 24 SimplePD/Player.h # Player: SwarmObject { int name; BOOL rowPlayer; int matrix[2][2]; int row, col; } -init: (int) n rowPlayer: (BOOL) rp; -setRow: (int) r Col: (int) c; -(BOOL)move; }

POL SCI Cederman / Stenfansson 25 SimplePD/Player.m #import Player -init:(int)n rowPlayer: (BOOL)rp { name = n; rowPlayer = rp; matrix[0][0]=3; matrix[1][1]=1; if (rowPlayer) { matrix[0][1]=0; matrix[1][0]=5;} else { matrix[0][1]=5; matrix[1][0]=0;} return self; } -setRow: (int)r Col: (int)c { row = r; col = c; } -(BOOL)move { BOOL moving; if (rowPlayer) moving=matrix[!row][col] > matrix[row][col]; else moving=matrix[row][!col] > matrix[row][col]; return moving;

POL SCI Cederman / Stenfansson 26 ObjRoulette: File Structure main.m Player.m Revolver.m Player.h Revolver.h

POL SCI Cederman / Stenfansson 27 ObjRoulette/main.m... int main(int argc, const char ** argv) { id playerA, playerB, revolver; long int repl; long int sum = 0; long int n = 1000; initSwarm(argc,argv); playerA = [Player create: globalZone]; playerB = [Player create: globalZone]; [playerA setOther: playerB]; [playerB setOther: playerA]; revolver = [Revolver create: globalZone]; // main simulation loop-body goes here printf("%8.6f \n", (double)sum/n); return 0; }

POL SCI Cederman / Stenfansson 28 ObjRoulette/main.m (cond'd)... for (repl=0; repl<n; repl++) { [playerA init: 0]; [playerB init: 1]; [revolver empty]; [playerA play: revolver]; if ([playerA isAlive]) sum++; }...

POL SCI Cederman / Stenfansson 29 Player: SwarmObject { int name; int alive; id other; } -init: (int) n; -setOther: o; -(BOOL)isAlive; -play:

POL SCI Cederman / Stenfansson 30 Revolver: SwarmObject { int bullets; } -empty; -load;

POL SCI Cederman / Stenfansson 31 Recursive Russian Roulette playerA playerB playerA playerB playerA [playerB play: r] playerB [playerA play: r] [playerB play: r] [playerA play: r] [playerB play: r] main [playerA play: r]