JAVA problem: Create a couple of classes that will simulate a process heap. Down
ID: 3807298 • Letter: J
Question
JAVA problem:
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:
Priority: which is an integer value that corresponds to where it will be in the heap. Needs to be greater than 0.
Time: A double value that corresponds to how long the process will take to run. Needs to be greater than 0.
Name: A string value that is the name of the process
Constructors: Default and Parameterized
Accessors and mutators for each instance variable
Make sure to check for valid values.
Other methods:
toString: a method that prints out the name, priority, and time
Create another class called ProcessHeap
Instance variables:
Heap: an array of type Process which represents the heap
Size: the number of elements in the heap
Constructors: Default and parameterized
Other methods:
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.
peek: this returns the head of the heap
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.
printHeap: Prints the heap out in breadth order
isEmpty: returns a true or false value for whether or not the queue is empty
Example Print Out:
The current heap is:
e 33 23.0
c 20 18.0
g 16 5.0
b 5 20.0
d 18 29.0
f 1 15.0
a 10 50.0
Starting simulation
Process: e 33 23.0 is starting
Process: e 33 23.0 has ended
Process: c 20 18.0 is starting
Process: c 20 18.0 has ended
Process: d 18 29.0 is starting
Random process added! Random Process 50 10 10.0
Process: d 18 29.0 has ended
Process: g 16 5.0 is starting
Process: g 16 5.0 has ended
Process: a 10 50.0 is starting
Random process added! Random Process 100 20 20.0
Process: a 10 50.0 has ended
Process: Random Process 100 20 20.0 is starting
Random process added! Random Process 150 30 30.0
Process: Random Process 100 20 20.0 has ended
Process: Random Process 150 30 30.0 is starting
Process: Random Process 150 30 30.0 has ended
Process: Random Process 50 10 10.0 is starting
Process: Random Process 50 10 10.0 has ended
Random process added! Random Process 200 40 40.0
Process: Random Process 200 40 40.0 is starting
Process: Random Process 200 40 40.0 has ended
Process: b 5 20.0 is starting
Simulation time has expired
Lab Report Questions:
What are the major differences between a heap and a binary search tree?
What are some problems that a heap can be used to solve?
Driver:
Explanation / Answer
//Process.java
package com.chegg.heap;
public class Process {
private int priority;
private double time;
private String name;
public Process() {
}
public Process(String name, int priority, double time) /*throws Exception */{
this.setName(name);
this.setPriority(priority);
this.setTime(time);
}
public String getName() {
return this.name;
}
public int getPriority() {
return this.priority;
}
public double getTime() {
return this.time;
}
public void setName(String name) {
this.name = name;
}
public void setPriority(int priority){
//if(priority < 0) throw new Exception();
this.priority = priority;
}
public void setTime(double time){
//if(time < 0) throw new Exception();
this.time = time;
}
public String toString() {
return String.format("Name: %s, Priority: %d, Time: %f", getName(), getPriority(), getTime());
}
}
---------------------
//ProcessHeap.java
package com.chegg.heap;
public class ProcessHeap {
private Process[] heap;
private int size = 0;
public ProcessHeap() {
}
public ProcessHeap(int size) {
heap = new Process[size];
}
public void insert(Process process) {
//System.out.println("Adding " + process);
addProcess(process);
SortProcessHeap.heapSort(getHeap(), getSize());
}
public Process peek() {
if (size > 0)
return heap[0];
return null;
}
public Process remove() {
if (size > 0) {
Process processAtRoot = heap[0];
heap[0] = heap[size-1];
heap[size-1] = null;
size--;
//System.out.println("Removing " + processAtRoot);
if (size > 1) {
SortProcessHeap.heapSort(heap, size);
}
//System.out.println("Removed " + processAtRoot);
return processAtRoot;
}
return null;
}
public void printHeap() {
for (int i = 0; i < size; i++)
System.out.println(heap[i].toString());
}
public boolean isEmpty() {
if (size <= 0)
return true;
return false;
}
public void addProcess(Process process) {
heap[size] = process;
size++;
}
public Process[] getHeap() {
return this.heap;
}
public int getSize() {
return this.size;
}
public void setHeap(Process[] heap) {
this.heap = heap;
setSize(heap.length);
}
private void setSize(int size) {
this.size = size;
}
public static class SortProcessHeap {
public static void heapSort(Process[] processArray, int size) {
int n = size;
for (int i = n / 2 - 1; i >= 0; i--) {
//System.out.println(n+ " " + i );
heapify(processArray, n, i);
}
}
private static void heapify(Process[] processArray, int size, int root) {
int indexLargestPriority = root;
int l = 2 * root + 1; // leftChild = 2*i + 1
int r = 2 * root + 2; // rightChild = 2*i + 2
//System.out.println(processArray[indexLargestPriority]);
//System.out.println(size + " " + l + " "+ r + " " + indexLargestPriority);
if (l < size &&
processArray[l].getPriority() >
processArray[indexLargestPriority].getPriority())
indexLargestPriority = l;
if (r < size && processArray[r].getPriority() > processArray[indexLargestPriority].getPriority())
indexLargestPriority = r;
if (indexLargestPriority != root) {
Process swap = processArray[root];
processArray[root] = processArray[indexLargestPriority];
processArray[indexLargestPriority] = swap;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.