Question

import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements...

import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.ArrayList;


public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {


   private static final long serialVersionUID = -6229569372944782075L;

  
   public void add(K k, V v) {
      
if (!containsKey(k)) {

put(k, new LinkedList<V>());
}
get(k).add(v);
   }

  
   public V removeFirst(K k) {
      
       if (!containsKey(k)) {
return null;
       }

V value = get(k).removeFirst();

if (get(k).isEmpty()) {

super.remove(k);
}
return value;
   }

  
   public Pair<K, V> removeFirst() { // Problem 3
       // TODO complete this method
if (isEmpty()) {
return null;
}
K k = firstKey();
V v = removeFirst(k);
Pair<K, V> p = new Pair<K, V>(k, v);
return p;
      
   }


   public Iterator<Pair<K, V>> iterator() { // Problem 6
       // TODO complete this method
ArrayList<Pair<K, V>> list = new ArrayList<Pair<K, V>>();
for (K k : keySet()) {
for (V v : get(k)) {
list.add(new Pair<K, V>(k, v));
}
}
return list.iterator();
      
   }


   public static void main(String[] args) {
       MultiValuedTreeMap<Integer, String> m = new MultiValuedTreeMap<Integer, String>();
       m.add(1, "b");
       m.add(1, "a");
       m.add(0, "c");
       System.out.println(m);

       System.out.println(m.removeFirst(1));
       System.out.println(m);
       System.out.println(m.removeFirst());
       System.out.println(m.removeFirst());
       System.out.println(m.removeFirst());

       m.add(1, "b");
       m.add(1, "a");
       m.add(0, "c");
       System.out.println(m);
       for (Pair<Integer, String> p : m) {
           System.out.println(p);
       }
   }
}

public class Pair<F, S> {

   protected F first;
   protected S second;

  
   public Pair(F first, S second) {
       this.first = first;
       this.second = second;
   }
  
   public F first() {
       return first;
   }

   public S second() {
       return second;
   }

   @Override
   public String toString() {
       return "(" + first + ", " + second + ")";
   }

}


public class Simulator {

  
   protected MultiValuedTreeMap<Double, Task> scheduledTasks = new MultiValuedTreeMap<Double, Task>();


   public void schedule(Task task, double simulationTime) { // Problem 4
       // TODO complete this method
      
   }

  
   public void start() { // Problem 5
       // TODO complete this method
      
   }

  
   public static void main(String[] args) {
       Simulator simulator = new Simulator(); // construct a Simulator
       simulator.schedule(new Task() { // schedule a task that prints "1: " and then the simulation time 1000
           public void run(double simulationTime) {
               System.out.println("1: " + simulationTime);
           }
       }, 1000);
       simulator.schedule(new Task() { // schedule a task that prints "2: " and then the simulation time 2000
           public void run(double simulationTime) {
               System.out.println("2: " + simulationTime);
           }
       }, 2000);
       simulator.schedule(new Task() { // schedule a task that prints "3: " and then the simulation time 1000
           public void run(double simulationTime) {
               System.out.println("3: " + simulationTime);
           }
       }, 1000);
       simulator.start();
   }
}

public interface Task {

   public abstract void run(double simulationTime);

}

Problem 4. In Simulator.java, implement the schedule(Task task, double simulationTime)
method. This method must associate the specied simulationTime with the specied task using
the scheduledTasks member variable of the Simulator class. The type of scheduledTasks is
MultiValuedTreeMap so that scheduledTasks can associate each simulation time
with multiple Tasks while maintianing them in the order of increasing simulation time.

Problem 5. In Simulator.java, implement the start() method. This method
must perform the simulation while (i) extracting Tasks from scheduledTasks in the order of in-
creasing simulation time (in the insertion order in the case of Tasks associated with the same
simulation time), (ii) invoking the run(double simulationTime) method on each extracted Task,
and (iii) terminating this process when the scheduledTasks collection becomes empty. If both
the schedule(Task task, double simulationTime) and start() methods are implemented cor-
rectly, the following code:
Simulator simulator = new Simulator ( ) ;
simulator . schedule ( new Task ( ) f
public void run ( double simulationTime ) f
System . out . println ( "1: " + simulationTime ) ;
g
g , 1 0 0 0 ) ;
simulator . schedule ( new Task ( ) f
public void run ( double simulationTime ) f
System . out . println ( "2: " + simulationTime ) ;
g
g , 2 0 0 0 ) ;
simulator . schedule ( new Task ( ) f
public void run ( double simulationTime ) f
System . out . println ( "3: " + simulationTime ) ;
g
g , 1 0 0 0 ) ;
simulator . start ( ) ;
will display:
1: 1000.0
3: 1000.0
2: 2000.0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Only the Simulator class is attached as there is no changes for any other classes. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Simulator.java

public class Simulator {

      protected MultiValuedTreeMap<Double, Task> scheduledTasks = new MultiValuedTreeMap<Double, Task>();

      public void schedule(Task task, double simulationTime) { // Problem 4

            // simply adding to the scheduledTasks map, with simulationTime being

            // the key and task being the value. since MultiValuedTreeMap is a

            // TreeMap, the default ordering of elements will be based on the

            // increasing order of key value - here the simulation time, so we dont

            // need to worry about the order.

            scheduledTasks.add(simulationTime, task);

      }

      public void start() { // Problem 5

            // loops until scheduledTasks is empty

            while (!scheduledTasks.isEmpty()) {

                  // removing the first key-value pair (with least simulation time) to

                  // get a Pair object

                  Pair<Double, Task> p = scheduledTasks.removeFirst();

                  // here the p.second will be the Task and p.first will be the

                  // simulation time. we simply call run method on task and passing

                  // simulation time

                  p.second().run(p.first());

            }

      }

      public static void main(String[] args) {

            Simulator simulator = new Simulator(); // construct a Simulator

            simulator.schedule(new Task() { // schedule a task that prints "1: " and

                                                             // then the simulation time 1000

                              public void run(double simulationTime) {

                                    System.out.println("1: " + simulationTime);

                              }

                        }, 1000);

            simulator.schedule(new Task() { // schedule a task that prints "2: " and

                                                             // then the simulation time 2000

                              public void run(double simulationTime) {

                                    System.out.println("2: " + simulationTime);

                              }

                        }, 2000);

            simulator.schedule(new Task() { // schedule a task that prints "3: " and

                                                             // then the simulation time 1000

                              public void run(double simulationTime) {

                                    System.out.println("3: " + simulationTime);

                              }

                        }, 1000);

            simulator.start();

      }

}

/*OUTPUT*/

1: 1000.0

3: 1000.0

2: 2000.0

Add a comment
Know the answer?
Add Answer to:
import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT