java primer ii
play

Java Primer II Nils Weidmann Center for Comparative and - PowerPoint PPT Presentation

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,


  1. 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

  2. Today’s agenda 2 • Principles of Object-orientation • Classes and Instances – RussianRoulette – CarRace • Inheritance – SuvRace (extension of CarRace)

  3. 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

  4. 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)

  5. 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

  6. 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

  7. 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

  8. More on Instances II 8 Pointer Instance car1 car2 car3 car3 = car2;

  9. More on Instances III 9 Instance Pointer LOST! car1 = car2; car1 car2 car3

  10. 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

  11. 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); } }

  12. 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

  13. 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 } }

  14. 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 ; } }

  15. 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."); } }

  16. 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

  17. 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

  18. 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; } }

  19. 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!"); } }

  20. 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

  21. 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!"); } }

  22. 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()); } }

  23. 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

  24. 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

  25. 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

  26. 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()); } }

  27. 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; } }

  28. 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; } }

  29. 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; } }

  30. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend