For my class we are making a simulator far a game, I'm just entirely confused on how to use TreeMaps, I understand there is a key and a value but how to go about all of this is a blur to me. I'm not looking for a cheat sheet or whatever, just some kind guidance would be appreciated!
For the first part we have to call (in public void printAllScheduledTasks(PrintStream out))
protected void print(Task task,long scheduledTime, PrintStream out).
--This method must print out all the Tasks that are scheduled for execution
I have some code for this part but have no idea if it is correct, and I'm still trying things as I type.
///////////////////
For the second part Implement Vector<Task> getTasks(long simulationTime) so that
public void schedule(Task task, long simulationTime) will work correctly. Vector<Task>
getTasks(long simulationTime) must return the collection of Tasks which is associated (in
scheduledTasks) with the specied simulation time. If no collection of Tasks is associated with
the specied simulation time, a new collection needs to be created and then (before it is returned) inserted into scheduledTasks with the specied simulation time.
--For this part I don't even know where to start, am I using treeMap, or vectors or wtf? All my classmates are stumped too so at least I don't feel alone. AGAIN I don't want the solution but help would be so much appreciated, Thank you.
import java.io.PrintStream; import java.util.Iterator; import java.util.Vector; /** * A Simulator performs scheduled Tasks at the specified simulation times. * * */ public class Simulator implements Iterable<Task> { /** * The current time. */ protected long currentSimulationTime = 0; /** * The scheduled tasks. */ protected java.util.TreeMap<Long, Vector<Task>> scheduledTasks = new java.util.TreeMap<Long, Vector<Task>>(); /** * The periodic Tasks and the periods of these Tasks (used for the scheduleAtFixedRate methods). */ protected java.util.Map<Task, Long> periodicTasks = new java.util.HashMap<Task, Long>(); /** * A flag that represents whether or not the simulation will be synchronized with the wall clock time. */ protected boolean realTimeSync; /** * The wall clock time when the scheduler's start() method was invoked. */ protected long startWallClockTime = -1; /** * Constructs a Simulator. */ public Simulator() { this(true); } /** * Constructs a Simulator. * @param realTimeSync a flag to indicate whether or not to synchronize the Simulator with the actual wall-clock time. */ public Simulator(boolean realTimeSync) { currentSimulationTime = 0; this.realTimeSync = realTimeSync; } /** * Prints out all the Tasks that this Simulator has scheduled for execution. * @param out the print stream. */ public void printAllScheduledTasks(PrintStream out) { // TODO updated this method (HW5. Problem 1, 20 points) print(this.iterator().next(), getCurrentSimulationTime(), out); } /** * Prints the specified Task. * @param scheduledTime the time when the Task will be executed. * @param task the task to print * @param out the print stream. */ protected void print(Task task, long scheduledTime, PrintStream out) { out.println("simulation time " + scheduledTime + ": " + task + (periodicTasks.containsKey(task)?" - periodic":"")); } /** * Returns the current simulation time. * @return the current simulation time. */ public long getCurrentSimulationTime() { return currentSimulationTime; } /** * Schedules the specified Task. * @param task the Task to be scheduled. * @param simulationTime the simulation time at which the Task to be executed. * @throws InvalidSimulationTimeException if the specified simulation time is earlier than the current simulation time. */ public void schedule(Task task, long simulationTime) throws InvalidSimulationTimeException { if (simulationTime < currentSimulationTime) throw new InvalidSimulationTimeException(); // finds the collection associated with the specified simulation time. getTasks(simulationTime).add(task); } /** * Returns the collection of Tasks associated with the specified simulation time (if no such collection, a new collection is created). * @param simulationTime the simulation time of interest. * @return the collection of Tasks associated with the specified simulation time. */ protected Vector<Task> getTasks(long simulationTime) { // TODO updated this method (HW5. Problem 2, 20 points) return null; } /** * Schedules the specified Task for repeated fixed-rate execution, beginning * at the specified simulation time. Subsequent executions take place at regular * intervals, separated by the specified period. * * @param task the Task to be scheduled. * @param simulationTime the first simulation time at which Task is to be executed * @param period the simulation time between successive executions. * @throws InvalidSimulationTimeException if the specified simulation time is earlier than the current simulation time. */ public void scheduleAtFixedRate(Task task, long simulationTime, long period) throws InvalidSimulationTimeException { // TODO updated this method (HW5. Problem 3, 20 points) } /** * Runs this Simulator. * @param duration the duration of the simulation. */ public void run(long duration) { while (currentSimulationTime < duration) { Task nextTask = nextTask(); // get the next Task. run(nextTask); // run the Task. // if the task is a periodic Task, reschedule the Task try { Long period = periodicTasks.get(nextTask); if (period != null) { schedule(nextTask, currentSimulationTime + period.longValue()); } } catch (Exception e) { e.printStackTrace(); } } } /** * Returns the next Task to run (if there is no scheduled Task, null is returned). * @return the next Task to run; null if there is no scheduled Task. */ protected Task nextTask() { // TODO updated this method (HW5. Problem 4, 20 points) return null; // no Task left; } /** * Executes the specified Task. * @param task the Task to execute. */ protected void run(Task task) { if (realTimeSync) { // if needs to be synchronized with the wall-clock time waitUntil(startWallClockTime + currentSimulationTime); } if (task != null) { task.set(this); task.run(); // execute the current task } } /** * Waits until the specified wall-clock time * @param wallClockTime the wall-clock time when the program will resume its execution. */ protected void waitUntil(long wallClockTime) { // TODO updated this method (HW5. Problem 5, 20 points) } @Override }