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

Lab 07 Process Heap Objective: Create a couple of classes that will simulate a p

ID: 3602745 • Letter: L

Question

Lab 07 Process Heap Objective: Create a couple of classes that will simulate a process heap. Download the driver and follow the following instructions! Create a class called Process . Instance variables: o Priority: which is an integer value that corresponds to where it will be in the heap Needs to be greater than 0 o Time: A double value that corresponds to how long the process will take to run. Needs to be greater than 0 o Name: A string value that is the name of the process Constructors: Default and Parameterized ·Accessors and mutators for each instance variable o Make sure to check for valid values Other methods: o toString: a method that prints out the name, priority, and time Create another class called ProcessHeap Instance variables: o Heap: an array of type Process which represents the heap o Size: the number of elements in the heap . Constructors: Default and parameterized .Other methods o insert: this method which returns nothing adds a new element to the heap. It always adds the next element as the next element in breadth order and then bubbles it up based on the priority of the Process o peek: this returns the head of the heap o remove: This returns the root of the heap and then removes it from the heap. It must then move the last element in the heap (via breadth order) to the root and bubble that value down until it's in the correct location. o printHeap: Prints the heap out in breadth order o isEmpty: returns a true or false value for whether or not the queue is empty o heapSort: Prints out the heap in sorted order. The idea is to remove an element print that element and then repeat that until there are no more elements.

Explanation / Answer

class Process{
   private int priority;
   private double time;
   private String name;
   public Process(){
       priority=0;
       time=0.0;
       name="";
   }
   public Process(int priority,double time,String name){
       this.priority=priority;
       this.time=time;
       this.name=name;
   }
   public int getPriority(){
       return priority;
   }
   public void setPriority(int p){
       priority=p;
   }
   public double getTime(){
       return time;
   }
   public void setTime(double t){
       time=t;
   }
   public String getName(){
       return name;
   }
   public void setName(String n){
       name=n;
   }
   public String toString(){
       String str=" Name: "+name;
       str+=" Priority: "+priority;
       str+=" Time: "+time;
       return str;
   }
}

class ProcessHeap{
   private Process[] heap;
   private   int size;
   private static int counter=0;
   public ProcessHeap(){
       heap=null;
       size=0;
   }
   public ProcessHeap(Process[] heap,int size){
       this.heap=heap;
       this.size=size;
   }
   public void insert(Process p){
       heap[counter++]=p;   //insert a process into the heap
   }
   public Process peek(){
       return heap[counter-1];   //returns the head of the heap
   }
   public Process remove(){
       Process temp=heap[0];
       for(int i=0;i<counter-1;i++){
           heap[counter]=heap[counter+1];
       }
       return temp;
   }
   public void printHeap(){
       for(int i=0;i<counter;i++){
           System.out.println(heap[i].toString());
       }
   }
   public bool isEmpty(){
       if(counter==0){
           return true;
       }else{
           return false;
       }
   }
   public void heapSort(){
       Process temp=null;
       for(int i=0;i<counter-1;i++){
           for(int j=0;j<counter-i-1;j++){
               if(heap[j].getPriority()>heap[j+1].getPriority()){   //sort the heap processes based on their priority
                   temp=heap[j];
                   heap[j]=heap[j+1];
                   heap[j+1]=temp;
               }
           }
       }
       printHeap();
   }
}