Download presentation
Presentation is loading. Please wait.
Published byJeremy Baker Modified over 9 years ago
1
Introduction to Computational Modeling of Social Systems Nils Weidmann Center for Comparative and International Studies (CIS) Seilergraben 49, Room E.3, weidmann@icr.gess.ethz.chweidmann@icr.gess.ethz.ch Prof. Lars-Erik Cederman, CIS Room G.2, lcederman@ethz.chlcederman@ethz.ch http://www.icr.ethz.ch/teaching/compmodels Lecture, November 16, 2004 Java Primer II
2
2 Today’s agenda Principles of Object-orientation Classes and Instances –RussianRoulette –CarRace Inheritance –SuvRace (extension of CarRace)
3
3 Introduction to objects User only has to be familiar with the interface of an object, not its implementation Objects hide their behavior and data Simula-67 first object-oriented language SmallTalk another important one
4
4 Object-Oriented Programming instance variables methods An object object = data + algorithms program = objects + messages speed = 45.7; gear = 3; brake() changeGears(g) messages An object has state, behavior, and identity (Booch) A program
5
5 Three principles of OOP Encapsulation –Objects hide their functions (methods) and data (instance variables) Inheritance –Each subclass inherits all variables of its superclass Polymorphism –Same interface despite different datatypes Car Auto- matic Manual Super class Subclasses draw()
6
6 Classes and instances Class car1 car2 Car Instances Pointers Classes are "object factories". Objects are created as instances by allocating memory for them. Pointers are used to keep track of the objects. new Car() car3
7
7 More on Instances car1 InstancesPointers Car car1 = new Car(1, 65.0); car2 Car car2 = new Car(2, 80.0); car3 Car car3;
8
8 More on Instances II car1 Instances Pointers car2 car3 car3 = car2;
9
9 More on Instances III car1 Instances Pointers car2 car3 car1 = car2; LOST!
10
10 Russian Roulette revisited 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
11
11 Russian Roulette w/o objects public class RussianRoulette { public static void main(String[] args) { int n = 1000000; //the number of replications int sum = 0; //the number of replications where player 0 dies Random rand = new Random(); //the random number generator for (int replications = 0; replications < n; replications++) { int player = 1; int i = 0; boolean shot = false; do { i++; player = Math.abs(player-1); shot = rand.nextDouble() < (double) i / 6.0; } while (!shot); if (player == 0) sum++; } double survivalProb0 = 1 - (double) sum / n; System.out.println("Player 0's probability of surviving is "+survivalProb0); }
12
12 Russian Roulette with objects Player int numDeaths boolean shot Player() executeTry(Revolver revolver) reset() Revolver int numBullets void addBullet() boolean trigger() RussianRouletteWithObjects static void main(String[] args) classes objects revolver player0 player1 reference!
13
13 Russian Roulette: revolver class public class Revolver { private int numBullets = 0; public boolean trigger() { return (Math.random() < (double) numBullets/6); } public void addBullet() { if (numBullets < 6) numBullets++; } instance variable methods Static Method
14
14 Russian Roulette: player class public class Player { public int numDeaths; public boolean shot; public Player() { numDeaths = 0; } public void executeTry(Revolver revolver) { if (revolver.trigger()) { shot = true; numDeaths++; } public void reset() { shot = false; } instance variables methods constructor
15
15 Russian Roulette: main model public class RussianRouletteWithObjects { public static void main(String[] args) { int numReplications = 1000000; Revolver revolver; Player player0 = new Player(); Player player1 = new Player(); for (int i=0; i<numReplications; i++) { revolver = new Revolver(); player0.reset(); player1.reset(); Player currPlayer = player1; do { revolver.addBullet(); if (currPlayer == player0) currPlayer = player1; else currPlayer = player0; currPlayer.executeTry(revolver); } while (!currPlayer.shot); } System.out.println("Player 0 died in " + player0.numDeaths + " out of " + numReplications + " replications."); } Constructor call creates new revolver without bullets!
16
16 CarRace and SpeedTrap car 1 car 2 cop Distance per time period drawn from uniform distribution (0,maxSpeed) Total distance traveled is the sum of all trips A speed trap with probability probCatch reduces the distance per time period to zero for any car traveling faster than the speedLimit = 65
17
17 CarRace Car int id double distance, speed, maxSpeed void drive() double getSpeed() double getDistance() void setDistance(double d) CarRace static void main(String[] args) classes objects car1 car2
18
18 Class declaration: Car public class Car { int id; double distance, maxSpeed, speed; public Car(int i, double s) { id = i; maxSpeed = s; distance = 0.0; } public void drive () { speed = Math.random() * maxSpeed; distance = distance + speed; } public double getSpeed() { return speed; } public double getDistance() { return distance; } public void setDistance(double d) { distance = d; } instance variables constructor methods Static Method
19
19 public class CarRace { public static void main(String[] args) { int n = 10; Car car1 = new Car(1,65.0); Car car2 = new Car(2,80.0); for (int i=0; i<n; i++) { car1.drive(); car2.drive(); System.out.println(i+" "+car1.getDistance()+" "+car2.getDistance()); } if (car1.getDistance() > car2.getDistance()) System.out.println("Car 1 won!"); else System.out.println("Car 2 won!"); } CarRace without speed trap creating objects calling objects
20
20 SpeedTrap Car int id double distance, speed, maxSpeed void drive() double getSpeed() double getDistance() void setDistance(double d) SpeedTrap static void main(String[] args) classes objects car1car2 CopCar double speedLimit, probCatch CopCar(double s, double p) intercept(Car car) cop
21
21 public class SpeedTrap { public static void main(String[] args) { int n = 1000; Car car1 = new Car(1,65.0); Car car2 = new Car(2,80.0); CopCar cop = new CopCar(65.0, 0.7); for (int i=0; i<n; i++) { car1.drive(); car2.drive(); cop.intercept(car1); cop.intercept(car2); System.out.println(i+" "+car1.getDistance()+" "+car2.getDistance()); } System.out.print(car1.getDistance()+" "+car2.getDistance()+“ ==> "); if (car1.getDistance() > car2.getDistance()) System.out.println("Car 1 won!"); else System.out.println("Car 2 won!"); } Adding the speed trap new object created! new object calls!
22
22 Class declaration: CopCar public class CopCar { double speedLimit,probCatch; public CopCar(double s, double p) { speedLimit = s; probCatch = p; } public void intercept(Car car) { if (car.getSpeed() > speedLimit) if (Math.random() < probCatch) car.setDistance(car.getDistance() - car.getSpeed()); }
23
23 Inheritance speed; gear brake() changeGears(g) speed brake() speed brake() accelerate() Car ManualAutomatic Car’s speed and brake() are copied to the subclasses Subclasses can add own variables and methods
24
24 SuvRace sedan Extension of CarRace Distance per time period drawn from uniform distribution (0,maxSpeed) Total distance traveled is the sum of all trips The performance is measured as the person-miles traveled roadster SUV
25
25 SuvRac e SUV sedan roadster Car Car(double s) Sedan Sedan(double s) Roadster Roadster(double s) Suv double probBlowOut Suv(double s,double p) void drive() Vehicle double distance double maxSpeed int passengers void drive() double getPersonMile() inheritance!
26
26 Model public class Model { public static void main(String[] args) { int n = 100; Vehicle sedan, roadster, suv; sedan = new Sedan(65.0); roadster = new Roadster(100.0); suv = new Suv(60.0,0.01); for (int i=0; i<n; i++) { sedan.drive(); roadster.drive(); suv.drive(); } System.out.println((int)sedan.getPersonMile() + " " + (int)roadster.getPersonMile() + " " + (int)suv.getPersonMile()); }
27
27 Vehicle & Car classes public class Vehicle { double distance = 0.0; double maxSpeed = 0.0; int passengers = 0; public void drive() { distance = distance + Math.random()*maxSpeed; } public double getPersonMile() { return distance * (double) passengers; } public class Car extends Vehicle { public Car(double s) { distance = 0.0; maxSpeed = s; }
28
28 Suv class public class Suv extends Vehicle { double probBlowOut; public Suv(double s, double p) { maxSpeed = s; probBlowOut = p; distance = 0.0; passengers = 7; } public void drive() { super.drive(); if (Math.random() < probBlowOut) distance = 0.0; }
29
29 Sedan and Roadster classes public class Sedan extends Car { public Sedan(double s) { super(s); passengers = 4; } public class Roadster extends Car { public Roadster(double s) { super(s); passengers = 2; }
30
30 ElevatorProject 0 1 2 Tenant Elevator Classes Objects Two tenants live in an apartment complex with 1 occupying the first floor and 2 the second The tenants move in and out of the building using the elevator Calculate the expected waiting time for both tenants
31
31 Calculating expected waiting time W(i,j) waiting time for tenant i in position j W(i,j|k) waiting time conditional on k's being last user EW(1,0) = 0.5 EW(1,0|1) + 0.5 EW(1,0|2) = 0.5 x 0 + 0.5(0.5 x 0 + 0.5 x 2) = 0.5 EW(2,0) = 0.5 x 0 + 0.5 (0.5 x 0 + 0.5 x 1) = 0.25 EW(1,1) = 0.5 x 0 + 0.5 (0.5 x 1 + 0.5 x 1) = 0.5 EW(2,2) = 0.5 x 0 + 0.5 (0.5 x 1 + 0.5 x 2) = 0.75 EW = {EW(1,0) + EW(2,0) + EW(1,1) + EW(2,2)}/4 = 0.5
32
32 public class Model { public static void main(String[] args) { int n = 1000; Tenant tenant1 = new Tenant(1); Tenant tenant2 = new Tenant(2); Elevator elevator = new Elevator(); for (int i=0; i<n; i++) { if (Math.random()<0.5) tenant1.move(elevator); else tenant2.move(elevator); } System.out.println("Waiting time = " + elevator.averageWaitingTime()); } Elevator Project: Code
33
33 Tenant class public class Tenant { int home,floor; public Tenant(int f) { home = f; if (Math.random()< 0.5) floor = 0; else floor = f; } public boolean atHome() { return floor == home; } public void move(Elevator elevator) { elevator.callToFloor(floor); if (atHome()) elevator.takeToFloor(this,0); else elevator.takeToFloor(this,home); }
34
34 Elevator class public class Elevator { int floor, waitingTime, numTrips; public Elevator() { if (Math.random()< 0.5) floor = 0; else floor = (int)(2.0*Math.random())+1; waitingTime = 0; numTrips = 0; } public void callToFloor(int f) { waitingTime = waitingTime + Math.abs(f-floor); numTrips++; floor = f; } public void takeToFloor(Tenant tenant,int f) { floor = f; tenant.floor = f; } public double averageWaitingTime() { return (double)waitingTime/(double)numTrips; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.