Introduction to Computational Modeling of Social Systems Java Primer II Nils Weidmann Center for Comparative and International Studies (CIS) Seilergraben 49, Room E.3, weidmann@icr.gess.ethz.ch Prof. Lars-Erik Cederman, CIS Room G.2, lcederman@ethz.ch http://www.icr.ethz.ch/teaching/compmodels Lecture, November 16, 2004
Today’s agenda 2 • Principles of Object-orientation • Classes and Instances – RussianRoulette – CarRace • Inheritance – SuvRace (extension of CarRace)
Introduction to objects 3 Simula-67 first object-oriented language SmallTalk another important one User only has to be familiar Objects hide their with the interface of an object, behavior and data not its implementation
Object-Oriented Programming 4 An object A program methods instance variables messages brake() speed = 45.7; gear = 3; changeGears(g) An object has state, object = data + algorithms behavior, and identity program = objects + (Booch)
Three principles of OOP 5 • Encapsulation – Objects hide their functions ( methods ) and data ( instance variables ) • Inheritance Super class Car – Each subclass inherits all variables of its Auto- Manual Subclasses matic superclass • Polymorphism draw() draw() – Same interface despite different datatypes
Classes and instances 6 Classes are "object factories". Class Instances Objects are created as instances by allocating memory for them. Car Pointers are used to keep track of new Car() the objects. car1 car2 Pointer car3
More on Instances 7 Pointer Instance Car car1 = new Car(1, 65.0); car1 Car car2 = new Car(2, 80.0); car2 Car car3; car3
More on Instances II 8 Pointer Instance car1 car2 car3 car3 = car2;
More on Instances III 9 Instance Pointer LOST! car1 = car2; car1 car2 car3
Russian Roulette revisited 10 A 5/6 1/6 B 4/6 2/6 A 3/6 3/6 B 2/6 4/6 A 1/6 5/6 B
Russian Roulette w/o objects 11 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); } }
Russian Roulette with objects 12 RussianRouletteWithObjects reference! static void main(String[] args) Revolver int numBullets void addBullet() boolean trigger() Player revolver int numDeaths boolean shot Player() executeTry(Revolver revolver) reset() player1 player0 objects classes
Russian Roulette: revolver class 13 public class Revolver { instance variable private int numBullets = 0; public boolean trigger() { return (Math.random() < ( double ) numBullets/6); } methods public void addBullet() { Static if (numBullets < 6) numBullets++; Method } }
Russian Roulette: player class 14 public class Player { instance variables public int numDeaths; public boolean shot; constructor public Player() { numDeaths = 0; } public void executeTry(Revolver revolver) { if (revolver.trigger()) { shot = true ; numDeaths++; methods } } public void reset() { shot = false ; } }
Russian Roulette: main model 15 public class RussianRouletteWithObjects { public static void main(String[] args) { int numReplications = 1000000; Revolver revolver; Player player0 = new Player(); Constructor Player player1 = new Player(); call creates for ( int i=0; i<numReplications; i++) { new revolver revolver = new Revolver(); without player0.reset(); bullets! 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."); } }
CarRace and SpeedTrap 16 •Distance per time period drawn from uniform distribution cop (0,maxSpeed) car 1 •Total distance traveled is the sum of all trips •A speed trap with probability probCatch car 2 reduces the distance per time period to zero for any car traveling faster than the speedLimit = 65
CarRace 17 CarRace static void main(String[] args) Car int id double distance, speed, maxSpeed void drive() double getSpeed() double getDistance() void setDistance( double d) car1 car2 objects classes
Class declaration: Car 18 public class Car { int id; instance variables double distance, maxSpeed, speed; public Car( int i, double s) { id = i; constructor maxSpeed = s; Static distance = 0.0; Method } public void drive () { speed = Math.random() * maxSpeed; distance = distance + speed; } public double getSpeed() { return speed; methods } public double getDistance() { return distance; } public void setDistance( double d) { distance = d; } }
CarRace without speed trap 19 public class CarRace { public static void main (String [] args) { int n = 10; Car car1 = new Car(1,65.0); creating objects Car car2 = new Car(2,80.0); for ( int i=0; i<n; i++) { calling objects 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!"); } }
SpeedTrap 20 SpeedTrap static void main(String[] args) Car int id double distance, speed, maxSpeed void drive() double getSpeed() double getDistance() void setDistance( double d) car1 car2 CopCar double speedLimit, probCatch CopCar( double s, double p) intercept(Car car) cop objects classes
Adding the speed trap 21 public class SpeedTrap { public static void main (String [] args) { int n = 1000; new object Car car1 = new Car(1,65.0); created! Car car2 = new Car(2,80.0); CopCar cop = new CopCar(65.0, 0.7); for ( int i=0; i<n; i++) { car1.drive(); new object car2.drive(); calls! 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!"); } }
Class declaration: CopCar 22 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()); } }
Inheritance 23 Car brake() Car’s speed and brake() are copied speed to the subclasses brake() brake() Subclasses can add speed; own variables and speed gear methods changeGears(g) accelerate() Manual Automatic
SuvRace 24 • Extension of CarRace • Distance per time roadster period drawn from uniform distribution (0,maxSpeed) sedan • Total distance traveled is the sum of all trips • The performance is SUV measured as the person-miles traveled SUV
SuvRace 25 Vehicle inheritance! double distance double maxSpeed int passengers void drive() Car double getPersonMile() Car( double s) Suv double probBlowOut Sedan Roadster Suv( double s, double p) void drive() Sedan( double s) Roadster( double s) SUV sedan roadster
Model 26 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()); } }
Vehicle & Car classes 27 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; } }
Suv class 28 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; } }
Sedan and Roadster classes 29 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; } }
ElevatorProject 30 •Two tenants live in an 2 apartment complex with 1 occupying the first floor 1 and 2 the second 0 •The tenants move in and out of the building using the elevator Classes Objects •Calculate the expected waiting time for both Tenant tenants Elevator
Recommend
More recommend