but first. Project 1: Movie Theatre Simulation Inheritance and - - PDF document

but first
SMART_READER_LITE
LIVE PREVIEW

but first. Project 1: Movie Theatre Simulation Inheritance and - - PDF document

but first. Project 1: Movie Theatre Simulation Inheritance and Polymorphism Handouts / Description now on Web site Due Dates: The Payroll App Minimum Effort Due: April 7, 2002 Full Project Due: April 14, 2002 Movie


slide-1
SLIDE 1

1

Inheritance and Polymorphism

The Payroll App

but first….

  • Project 1: Movie Theatre Simulation

– Handouts / Description now on Web site – Due Dates:

  • Minimum Effort Due: April 7, 2002
  • Full Project Due: April 14, 2002

Movie Theatre Simulation

  • You are to program a simulation of the workings
  • f a multiplex theatre complex.
  • Rules:

– A Theatre Complex has a number of individual theatres – Each theatre shows an individual movie a number of times based on a given schedule – A movie may be shown in more than one theatre – Customers can buy tickets in advance for $7 ($4 if ticket is purchased before 4pm)

Movie Theatre Simulation

  • You will be programming a simulation

– There will be a global “clock” indicating the current time of day of the simulation – You will be updating the current time as the simulation progresses through “the day” – The state of the objects of the system will change as the current time changes.

Movie Theatre Simulation

  • Major objects

– TheatreSim – main object that drives the simulation – TheatreComplex – represents a multiplex containing a multitude theatres – Theatre – an individual theatre – Movie – a movie – Schedule – The schedule that a movie will follow at a given individual theatre. Will also maintain the “state”

  • f a movie playing at a given theatre.

– Clock – Master clock for the simulation – Time – representing time of day

Movie Theatre Simulation

  • You will need to implement the following

classes:

– Movie – Schedule – Theatre – TheatreComplex

  • Specs for these classes are given via

Javadocs and UML diagrams.

slide-2
SLIDE 2

2

Movie Theatre Simulation

  • A simulation will be defined by two input files (to

TheatreSim)

– Complex Definition File –

  • Contains what theatres are playing what movies and when.

– Simulation Control file

  • List of “events” that happen during the day

– “buy” – customer buys ticket – “advance” – advance the global clock – “status” – Print status report (what’s going on “now” at each theatre) – “summary” – Print summary (how many tickets sold and $$ collected for each theatre) – “schedule” – Print s schedule

Movie Theatre Simulation

  • Minimum Effort Requirement

– At the very least, you will need to implement the Movie class. – Submission of this is due April 7th – Must submit minimum effort requirement to pass the course.

Movie Theatre Simulation

  • Submission

– All submissions (minimum effort + final code) are done using try – Minimum effort due: April 7th – Complete project due: April 14th – No late submissions!

Movie Theatre Simulation

  • Grading

– First criteria: Program must run correctly! – Deductions for

  • Unclear or bad implementation (35%)
  • Bad programming style (including non-use of RCS) (30%)

– Weighting

  • Movie

15%

  • Schedule 25%
  • Theatre

25%

  • TheatreComplex 25%
  • All classes together 10%

Movie Theatre Simulation

  • Questions?

Interface data

  • All data associated with an interface,

regardless of how they are declared are:

– Read-only – Public – Final – Static

slide-3
SLIDE 3

3

Interface data

public interface Foo { public int a = 30; public static int b = 40; public static final int c = 50; private int d = 34; // not allowed protected int e = 444; // not allowed }

A bit more on interfaces

  • The Observable/Observer design pattern

– Use this pattern when you have one class that must react to the changes in another class. – The second class(Observer) is said to observe the first class (Observable). – In the project, the Clock will be observed by the Theatre objects.

Observer/Observable (java.util)

  • The Observable

– Observable is defined as a class. – The Observable maintains a set of Observers who are interested when the Observable changes

  • Observer

– Observer is defined as an interface – Classes that implement Observer must define an update method

Observer/Observable (java.util)

public void update (Observable o, Object arg)

  • – the observable object

arg – an argument with optional details about the change.

Observer/Observable (java.util)

  • When an update is made to an Observable,

an update message is sent to all Observers currently in the Observable’s set.

  • Observable.notifyObservers(Ob

ject arg) is used for this purpose

Observer/Observable (java.util)

  • In our simulation

– All Theatre objects are registered with the global Clock as observers. – When the current time of the Clock changes (via the simulation), the Clock will notifyObservers()

  • I.e. the update method for all Theatres will be

called.

slide-4
SLIDE 4

4

Observer/Observable

  • Questions?

Back to our Payroll app

  • Class hierarchy for theatre app

Performer Actor Musician isA isA Guitarist Pianist Drummer

Back to our Payroll app

  • Instrument hierarchy

Instrument Guitar Piano Drums

Changes to the Payroll class

public class Payroll { private Performer performer[]; private int nPerf; public void addPerformer (Performer P) { if (nPerf == MAXPERF) { System.err.println ("Payroll is full!"); } else { performer[nPerf] = P; nPerf++; } }

calculateTotalPay

public double calculateTotalPay() { double sum = 0.0; for (int i=0; i < nPerf; i++) sum += performer[i].calculatePay(); return sum; }

A look at Instruments

public abstract class Instrument { private double rentalCost; protected Instrument (double cost) { rentalCost = cost; } public double getWeeklyRental () { return rentalCost; } }

slide-5
SLIDE 5

5

A look at instruments

public class Guitar extends Instrument { public Guitar () { super (200.0); } protected Guitar (double rate) { super (rate); } }

A look at Performer

public abstract class Performer implements Comparable { private String myName; private double payPerPerf; private int nPerformances; protected Performer (String name, double rate) { myName = name; payPerPerf = rate; nPerformances = 0; } }

A look at Performer

public abstract double calculatePay(); protected double getBasePay() { return nPerformances * payPerPerf; } public void perform (int n) { nPerformances = n; }

Now Actor

public class Actor extends Performer { private static final double PAYRATE = 200.0; public Actor (String name) { super (name, PAYRATE); } public double calculatePay () { return getBasePay(); } }

And Musician

public class Musician extends Performer { private Instrument myInstrument; private static final double PAYRATE = 100.0; public Musician(String name, Instrument I) { super(name, PAYRATE); myInstrument = I; } public double calculatePay() { return getBasePay() + myInstrument.getWeeklyRental(); }

Finally Musician subclasses

public class Drummer extends Musician { public Drummer (String name) { super (name, new Drums()); } }

slide-6
SLIDE 6

6

  • Any questions?
  • Tomorrow – Exceptions