Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following tasks are left for you to complete this project: Implement the Pri

ID: 3757442 • Letter: T

Question

The following tasks are left for you to complete this project: Implement the Priority Queue ADT based on the MinHeap Data Structure Understand the given code and complete the “tiny” sections of code marked with (left for you) Get it to run Modify the simulation so it uses the weighted discrete random number-generated function described on the next slide. § Select reasonable numbers of weights for the type of beer ordered by the drinkers and for the sizes of the groups of drinkers § Compare a run of the resulting program to a run of the given program (based on uniform distribution) Warning: please don’t leave things till last minute! Unlike assignment 1, I will not be available on the Saturday of the due date. Sorry, I am expecting out of town visitors this time. Project #2 Software Gurus Bar Slide 30 Weighted Probability One alternative to the use of uniform distribution is the idea of a weighted discrete probability. Suppose that we observe a real bar and note that 15% of the time they will order local beer, 60% of the time they will order imported beer, and 25% of the time they will order special beer. This is certainly a far different distribution from the uniform distribution we used above (33% of each type of beer). In order to simulate this new behavior, we can add a new method to our class random. Add a method named weightedProbability to the class SimulationFramework. This method will take as argument a vector of unsigned integer values. For example, to generate the preceding distribution, you will pass to the weightedProbability method a vector of three elements containing 15, 60, and 25. The method first sums the values in the array, resulting in a maximum value. In this case, the value would be 100. A random number between 1 and this maximum value is then generated. The method then decides in which category the number belongs. This can be discovered by “scanning” through the values. In our example, if the number is less than 15, the method should return 0; if less than or equal to 75 (15 + 60), return 1; otherwise return 2.

Explanation / Answer

CSMSoftwareGurusBar.java

public class CSMSoftwareGurusBar {
   private int freeChairs = 50;
   private double profit = 0.0;
   private SimulationFramework simulation = new SimulationFramework();
   private int[] beertype = {15, 60, 25};

   public static void main(String[] args) {
       CSMSoftwareGurusBar world = new CSMSoftwareGurusBar();
   }

   CSMSoftwareGurusBar() {
       int t = 0;
       while (t < 240) { // simulate 4 hours of bar operation
           t += randBetween(2, 5); // new group every 2-5 minutes
           if (t <= 240)
               simulation.scheduleEvent(new ArriveEvent(t, randBetween(1, 5)));
       } // group size ranges from 1 to 5
       simulation.run();
       System.out.println("Total profits " + profit);
   }

   private int randBetween(int low, int high) {
       return low + (int) ((high - low + 1) * Math.random());
   }

   public boolean canSeat(int numberOfPeople) {
       System.out.println("Group of " + numberOfPeople
               + " customers arrives at time " + simulation.time());
       if (numberOfPeople < freeChairs) {
           System.out.println("Group is seated");
           freeChairs -= numberOfPeople;
           return true;
       } else
           System.out.println("No Room, Group Leaves");
       return false;
   }

   private void order(int beerType) {
       System.out.println("Serviced order for beer type " + beerType
               + " at time " + simulation.time());
       // update profit knowing beerType (left for you)
       profit += beerType + 1;
   }

   private void leave(int numberOfPeople) {
       System.out.println("Group of size " + numberOfPeople
               + " leaves at time " + simulation.time());
       freeChairs += numberOfPeople;
   }

   private class ArriveEvent extends Event {
       private int groupSize;
      
       ArriveEvent(int time, int gs){
           super(time);
           groupSize = gs;
       }
      
       public void processEvent(){
           if (canSeat(groupSize)){
               // place an order within 2 & 10 minutes
               simulation.scheduleEvent (new OrderEvent(time + randBetween(2,10), groupSize));
           }
       }
   }

   private class OrderEvent extends Event {
       private int groupSize;

       OrderEvent(int time, int gs) {
           super(time);
           groupSize = gs;
       }

       public void processEvent() {
           // each member of the group orders a beer (type 1,2,3)
           for (int i = 0; i < groupSize; i++)
               order(1 + simulation.weightedProbability(beertype));
           // schedule a leaveEvent for this group
           // all the group leaves together (left for you)
           simulation.scheduleEvent (new LeaveEvent(time + randBetween(30,60), groupSize));
          
       }
   }

   private class LeaveEvent extends Event {
       LeaveEvent(int time, int gs) {
           super(time);
           groupSize = gs;
       }

       private int groupSize;

       public void processEvent() {
           leave(groupSize);
       }
   }      
}


SimulationFramework.java

public class SimulationFramework {
   public void scheduleEvent(Event newEvent) {
       // put or addElement “newEvent” to the “eventQueue”
       // MinHeap Priority Queue (left for you)
       eventQueue.addElement(newEvent);
   }

   public void run () {
       while (! eventQueue.isEmpty()) {
           Event nextEvent = (Event) eventQueue.removeMin();
           currentTime = nextEvent.time;
           nextEvent.processEvent();
       }
   }
  
   public int time() {
       return currentTime;
   }

   private int currentTime = 0;
   private FindMin eventQueue = new Heap(new DefaultComparator());
  
   public int weightedProbability(int[] beertype){
       int sumOfArray = 0;
       for (int i=0; i < beertype.length; i++){
           sumOfArray += beertype[i];
       }
       int probability = 1 + (int) ((sumOfArray) * Math.random());
       if (probability <= 15){
           return 0;
       }
       else if (probability <= 75){
           return 1;
       }
       else{
           return 2;
       }
   }
}


DefaultComparator.java


public class DefaultComparator implements java.util.Comparator {
  
  
   public int compare(java.lang.Object left, java.lang.Object right) {
       Event i = (Event) left;
       Event j = (Event) right;
       if (i.time > j.time){
           return 1;
       }
       else if (i.time < j.time){
           return -1;
       }
       else{
           return 0;
       }
   }

   public boolean equals(java.lang.Object obj){
       return    this.equals(obj);
      
   }
}

Event.java

public abstract class Event implements Comparable {
public final int time;
public Event (int t) { time = t; }
abstract void processEvent ();
public int compareTo (Object o) {
       Event right = (Event) o;
       if (time < right.time) return -1;
       if (time == right.time) return 0;
       return 1;
} }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote