Csc 205 programming ii
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

CSC 205 Programming II PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on
  • Presentation posted in: General

CSC 205 Programming II. Lecture 22 Carwash Simulation. Recap: Queue. A queue is an ordered, linear data structure New items are added at the rear end Items are removed from the front end It’s a “ first-in, first-out ” (FIFO) structure Queue operations

Download Presentation

CSC 205 Programming II

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Csc 205 programming ii

CSC 205Programming II

Lecture 22

Carwash Simulation


Recap queue

Recap: Queue

  • A queue is an ordered, linear data structure

    • New items are added at the rear end

    • Items are removed from the front end

    • It’s a “first-in, first-out” (FIFO) structure

  • Queue operations

    • Enqueue – add an item to the rear end

    • Dequeue – remove the front item

    • Peek – get the content of the top item


Carwash simulation

Carwash Simulation


The problem

The Problem

  • A carwash station can wash one car at a time

    • Time needed to wash a car is measured in seconds

  • Cars may arrive at any given second

    • A probability can be assumed

  • Cars arrived when the washer is busy have to wait in line

    • First-come, first-served

  • The goal: find out the average waiting time for a given period of time (in seconds)


Program specification

Program Specification

  • Input

    • Time needed to wash one car

    • The probability that a new customer arrives at any given second

    • The total length of time to be simulated

  • Output

    • Car arrival and leaving history

    • Number of customers serviced

    • Average time that a customer spent in line during the simulation


Design

Design

  • Objects relevant to the carwash problem

    • A collection of cars: CarQueue

    • Car

    • Washer

  • Other objects used in the simulation

    • Random arrival time generator: BooleanSource

    • Average time calculator: Averager

  • The application driver class: CarWashApp


Detailed design

Detailed Design

  • Key design concept

    • Determine which properties of a real-world object are relevant to the problem at hand

  • Car

    • Arrival timestamp

    • Sequence number

  • CarQueue

    • A linked list object is used as a queue

    • Three operations

      • enqueue, dequeue, isEmpty


Detailed design continued

Detailed Design – continued

  • Washer

    • Variables

      • Time (in seconds) needed to wash a car

      • Time left to finish washing the current car

    • Operations

      • Start washing

        • set washTimeLeft to secondsForWash

      • Reduce remaining time

        • Decrement washTimeLeft if it is not zero

      • Test if the washer is busy

        • It’s busy if washTimeLeft is greater than zero


Detailed design continued1

Detailed Design – continued

  • BooleanSource

    • Variable: probability

    • Operation: query

      • returns true only if Math.random() < probability

  • Averager

    • Variables

      • A count and a sum

    • Operations

      • addNumber: update both count and sum

      • howManyNumbers: return count

      • average: return sum/count


Car wash application

Car Wash Application

  • carWashSimulate

    • Three parameters

      • Time needed to wash a car

      • Probability

      • Total time

    • Echo input parameters

    • Validate parameters

    • Processing car washing with a loop

    • Output results


Car wash application1

Car Wash Application

  • Within the loop

    • Check if a new car arrives

    • Check whether we can start washing another car

      • That is if washer is not busy, and

      • The queue is not empty

    • Reduce the remaining time during washing a car


Sample simulation

Sample Simulation

C:\courses\CSC205\labs\carwash>java CarWashApp2

Seconds to wash one car: 1000

Probability of customer arrival during a second: 0.0010

Total simulation seconds: 5000

Car Arrived Left Waited

1 470 470 0

2 1412 1470 58

3 1580 2470 890

4 2900 3470 570

5 3559 4470 911

Customers served: 5

Average wait: 485.8 sec


  • Login