COMP2150 - Data Structure Object Oriented Programming

Download Solution Order New Solution

Assignment Task

Intoduction

You implemented a discrete-event simulation using C++ in assignment 2. In assignment 3 we’re continuing with the theme of simulations, but this time you’re going to be implementing something that more closely resembles a continuous simulation. We’re going to be simulating traffic in assignment 3. In assignment 3, you’re going to be building something that resembles a continuous simulation of traffic flow on a one-dimensional plane.

Description

Your program is going to simulate traffic moving along a one-dimensional plane. In other words: you’re going to be having vehicles driving down a single lane road where vehicles can’t pass other vehicles. Our goal is to make some observations about certain kinds of patterns that appear when traffic is flowing on a road.

Here’s a video describing the kinds of things we could make observations about when we’re simulating traffic:

Martin Treiber from the Institute for Traffic Econometrics, Modelling, and Statistics at Technische Universität Dresden has an excellent traffic simulator that you can play with to get an idea of the kinds of things we can simulate for roads.

Your simulation will be simple compared to either of the above examples. Your program will accept some inputs, but then otherwise work independently (no file input is required for this assignment).

Here’s an example of what running your program will look like:

The simulation

The simulation consists of two major parts:

1. A road

2. Vehicles on the road

Inputs and outputs

The inputs for this program are:

1. The total amount of time the simulation should run for (an integer).

2. The total number of spaces the road has (an integer).

3. The speed limit for the road (an integer)

Your simulation implementation should keep track of and print out the traffic flow: the number of vehicles that have successfully driven past the end of the road by the end of the simulation.

The road

Our hypothetical road has the following properties:

  • The road has a start and an end (the start is visually on the left, the end is visually on the right).
  • All vehicles on the road start at the start (location 0) and drive toward the end of the road (all vehicles on the road are driving in the same direction).
  • The road is made up of 1-unit spaces, where each space can either be occupied by a vehicle or be unoccupied (two vehicles cannot share a space, no crashing).
  • The road has a speed limit. Vehicles on the road cannot drive faster than the posted speed limit.

Vehicles on the road Each vehicle on the road has the following properties:

  • A vehicle is stopped when it’s added to a road.
  • A vehicle moves at each unit of time by increasing its current position on the road by the speed that it’s travelling at (e.g., a vehicle moving at a velocity of 3 starting at position 4 will move to position 7 on the road when it moves)
  • A vehicle will increase its speed when it is able to (it will not crash into the vehicle in front of it; it will not exceed the speed limit of the road).
  • A vehicle will decrease its speed when it has to (if it doesn’t slow down before moving, it will crash into the vehicle in front of it; vehicles should avoid crashing into the vehicle in front of them by continually decreasing speed until it will not crash into the vehicle in front of it, possibly coming to a full stop with a speed of 0).
  • A vehicle will not go in reverse (its current speed is never negative).
  • Every vehicle has a “model”, a single character that’s randomly selected from the lower-case letters and numbers.
  • Every vehicle has a “behaviour”. Some vehicles are “nervous” and will randomly slow down or stop. Others are “not nervous” and will never slow down randomly.
  • Whether a vehicle is nervous or not is determined when the vehicle is initially added to the road. If a vehicle is not nervous, it will never randomly slow down.
  • A nervous vehicle should have its model be drawn from symbol characters:
  •  Approximately 1 out of 10 vehicles is nervous (e.g., you generate random integers between 0 and 9, any time you generate a 0, then the vehicle is “nervous”)
  • If a vehicle is nervous, it will randomly slow down approximately 25% of the times that it moves (e.g., each time a nervous vehicle moves, you generate a random integer between 0 and 3, a nervous vehicle will slow down if the number is 0).
  • When a nervous vehicle slows down, it will slow down by a random amount up to and including stopping entirely.

Implementation requirements

The primary goal of this assignment is to evaluate your ability to use and implement interfaces in Java. A simulation runner has been provided to you. Your task is to implement the interfaces that the simulation driver needs to run.

Editing provided code You have been provided with several .java files. You are forbidden from modifying any of the provided files with one exception: You must change the methods in Time DrivenSimulationMain that return instances of the interfaces in this simulation to return instances of your implementations of those interfaces.

For example, if you have written an implementation of the Queue interface that you’ve called MyQueue, you should change TimeDrivenSimulationMain#queue from:

Animation

The output from your simulator should be “animated”. You can “animate” text output on the command line using the carriage return character and temporarily sleeping. Here’s an example of an animated text output program in Java:

To animate your road, in the method that prints the state of your simulation you should:

1. Print out a carriage return character System.out.print('\r'); (not println).

2. Print out a single character for each position on the road, representing either the vehicle at that location on the road, or an empty road space (an underscore _). You should use System.out.print (not println).

Interfaces

Here is a list of the interfaces you need to write classes to implement (you must write classes that implements these interfaces):

  • Queue: A queue!
  • Queue.QueueItem: An item that goes into the queue!
    • This is an inner interface and it does not specify any behaviour (no methods signatures are declared in this interface). The only purpose of this interface is to provide a common supertype for classes that you want to manage in your queue that implements the surrounding Queue interface.
  • Road: the place where Vehicles “drive”.
  • RoadSimulatorSettings: An interface exposing settings for simulating roads.
  •  TimeDrivenSimulation: An interface to help wrap some of the behaviours necessary to implement and test a time-driven or continuous simulation.
  • TimeUnitInstant: An abstraction on time (we don’t want to consider “seconds”, we just want to consider “units of time” in our simulation).
  • Vehicle: the actual type of thing that drives on your Road.

You are not permitted at all to change the interfaces. The grading team will replace interface files you submit with the ones provided with the assignment.

This COMP2150 - IT Computer Science has been solved by our PhD Experts at My Uni Paper.

Get It Done! Today

Country
Applicable Time Zone is AEST [Sydney, NSW] (GMT+11)
+

Every Assignment. Every Solution. Instantly. Deadline Ahead? Grab Your Sample Now.