Title: Java Primer II
1Java Primer II
2Todays agenda
- Principles of Object-orientation
- Classes and Instances
- RussianRoulette
- CarRace
- Inheritance
- SuvRace (extension of CarRace)
3Introduction to objects
Simula-67 first object-oriented
language SmallTalk another important one
User only has to be familiar with the interface
of an object, not its implementation
Objects hide their behavior and data
4Object-Oriented Programming
An object
A program
methods
instance variables
messages
brake()
speed 45.7 gear 3
changeGears(g)
An object has state, behavior, and identity
(Booch)
object data algorithms program objects
messages
5Three 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
Super class
Auto- matic
Manual
Subclasses
draw()
draw()
6Classes and instances
Classes are "object factories". Objects are
created as instances by allocating memory for
them. Pointers are used to keep track of the
objects.
Class
Instances
Car
new Car()
car1
car2
Pointers
car3
7More on Instances
Instances
Pointers
Car car1 new Car(1, 65.0)
car1
Car car2 new Car(2, 80.0)
car2
Car car3
car3
8More on Instances II
Pointers
Instances
car1
car2
car3
car3 car2
9More on Instances III
Instances
Pointers
LOST!
car1 car2
car1
car2
car3
10Russian Roulette revisited
5/6
1/6
4/6
2/6
3/6
3/6
2/6
4/6
1/6
5/6
B
11Russian 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 lt n
replications) - int player 1
- int i 0
- boolean shot false
- do
- i
- player Math.abs(player-1)
- shot rand.nextDouble() lt (double) i / 6.0
-
- while (!shot)
12Russian Roulette with objects
RussianRouletteWithObjects
reference!
static void main(String args)
revolver
player1
player0
objects
classes
13Russian Roulette revolver class
- public class Revolver
- private int numBullets 0
- public boolean trigger()
- return (Math.random() lt (double) numBullets/6)
-
- public void addBullet()
- if (numBullets lt 6) numBullets
-
instance variable
methods
Static Method
14Russian 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
constructor
methods
15Russian 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 i0 iltnumReplications 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)
-
Constructor call creates new revolver without
bullets!
16CarRace and SpeedTrap
- 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
cop
car 1
car 2
17CarRace
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
18Class 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
-
instance variables
constructor
Static Method
methods
19CarRace without speed trap
- 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 i0 iltn i)
- car1.drive()
- car2.drive()
- System.out.println(i" "car1.getDistance()
" "car2.getDistance()) -
-
- if (car1.getDistance() gt car2.getDistance())
- System.out.println("Car 1 won!")
- else
- System.out.println("Car 2 won!")
-
creating objects
calling objects
20SpeedTrap
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
21Adding the speed trap
- 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 i0 iltn 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() gt ") - if (car1.getDistance() gt car2.getDistance())
- System.out.println("Car 1 won!")
- else
new object created!
new object calls!
22Class 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() gt speedLimit)
- if (Math.random() lt probCatch)
- car.setDistance(car.getDistance() -
car.getSpeed()) -
-
-
23Inheritance
Car
Cars speed and brake() are copied to the
subclasses
Subclasses can add own variables and methods
Manual
Automatic
24SuvRace
- 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
SUV
25SuvRace
inheritance!
Sedan
Roadster
Sedan(double s)
Roadster(double s)
26Model
- 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 i0 iltn i)
- sedan.drive()
- roadster.drive()
- suv.drive()
-
- System.out.println((int)sedan.getPersonMile()
" " - (int)roadster.getPersonMile
() " " - (int)suv.getPersonMile())
-
27Vehicle 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
28Suv 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() lt probBlowOut)
- distance 0.0
-
-
-
29Sedan 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
-
-
30ElevatorProject
- 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
2
1
0
Classes Objects
Tenant
Elevator
31Calculating expected waiting time
- W(i,j) waiting time for tenant i in position j
- W(i,jk) waiting time conditional on k's being
last user - EW(1,0) 0.5 EW(1,01) 0.5 EW(1,02)
- 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
32Elevator Project Code
- 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 i0 iltn i)
- if (Math.random()lt0.5)
- tenant1.move(elevator)
- else
- tenant2.move(elevator)
-
-
- System.out.println("Waiting time "
elevator.averageWaitingTime()) -
33Tenant class
- public class Tenant
- int home,floor
- public Tenant(int f)
- home f
- if (Math.random()lt 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)
34Elevator class
- public class Elevator
- int floor, waitingTime, numTrips
- public Elevator()
- if (Math.random()lt 0.5)
- floor 0
- else
- floor (int)(2.0Math.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)