Question: How would you do this without the comparator and only using the minheap instead of linked list or array lists. import java.io . File; import

How would you do this without the comparator and only using the minheap instead of linked list or array lists.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
public class SheepScheduling {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("Enter the file name: ");
String name = sc.nextLine();
Scanner file = new Scanner(new File(name));
List sheeps = new LinkedList<>();
MinHeap waitingSheeps = new MinHeap<>();
while (file.hasNextLine()){
String line = file.nextLine();
String[] sheep = line.split("\\s+");
sheeps.add(new Sheep(sheep[0], Integer.parseInt(sheep[1]), Integer.parseInt(sheep[2])));
}
sheeps.sort(Comparator.comparing(Sheep::getArrivalTime));
String output ="";
int arrivalTime =0;
System.out.println("Schedule from the Provided File:");
while (!sheeps.isEmpty()){
Sheep temp = sheeps.remove(0);
arrivalTime += temp.getShearingTime();
while (!sheeps.isEmpty()){
if (sheeps.get(0).getArrivalTime()<= arrivalTime)
waitingSheeps.insert(sheeps.remove(0));
else
break;
}
System.out.println(temp);
while (!waitingSheeps.isEmpty()){
Sheep k = waitingSheeps.remove();
arrivalTime += k.getShearingTime();
System.out.println(k);
while (!sheeps.isEmpty()){
if (sheeps.get(0).getArrivalTime()<= arrivalTime)
waitingSheeps.insert(sheeps.remove(0));
else
break;
}
}
}
System.out.println("Do you want to run again?(y/n): ");
String choice = sc.nextLine();
if (choice.equalsIgnoreCase("n")){
break;
}
}
}
}
public class Sheep implements Comparable {
private String name;
private int shearingTime;
private int arrivalTime;
public Sheep(String name, int shearingTime, int arrivalTime){
super();
this.name = name;
this.shearingTime = shearingTime;
this.arrivalTime = arrivalTime;
}
public String getName(){
return name;
}
public int getShearingTime(){
return shearingTime;
}
public int getArrivalTime(){
return arrivalTime;
}
@Override
public int compareTo(Sheep o){
int res = this.shearingTime - o.shearingTime;
if (res ==0){
return this.name.compareTo(o.name);
} else {
return res;
}
}
@Override
public String toString(){
return "Name: "+ name +", Sheer Time: "+ shearingTime +", Arrival Time: "+ arrivalTime;
}
}
public class MinHeap>{
private E[] items;
private int size;
public MinHeap(){
this.size =0;
items =(E[]) new Comparable[100];
}
public int size(){
return size;
}
public void insert(E e){
if (size >= items.length){
return;
}
items[size]= e;
bubbleUp(size);
size++;
}
public E remove(){
if (size >0){
E popped = items[0];
items[0]= items[--size];
bubbleDown(0);
return popped;
}
return null;
}
public E peek(){
if (size >0){
return items[0];
}
throw null;
}
public void update(E item){
int i =0;
E temp = null;
for (i =0; i < size; i++){
if (items[i].compareTo(item)==0){
temp = items[i];
items[i]= item;
break;
}
}
if (temp.compareTo(item)<0){
bubbleDown(i);
} else {
bubbleUp(i);
}
}
public boolean isEmpty(){
return size ==0;
}
private void bubbleUp(int current){
while (items[current].compareTo(items[parentIndex(current)])<0){
swap(current, parentIndex(current));
current = parentIndex(current);
}
}
private void bubbleDown(int i){
if (!isItLeafIndex(i)){
if ((items[i].compareTo(items[leftChildIndex(i)])>0)||(items[i].compareTo(items[rightChild(i)]))>0){
if (items[leftChildIndex(i)].compareTo(items[rightChild(i)])<0){
swap(i, leftChildIndex(i));
bubbleDown(leftChildIndex(i));
} else {
swap(i, rightChild(i));
bubbleDown(rightChild(i));
}
}
}
}
private void swap(int x, int y){
E tmp;
tmp = items[x];
items[x]= items[y];
items[y]= tmp;
}
private int parentIndex(int i){
return (i -1)/2;
}
private int leftChildIndex(int i){
return (i *2)+1;
}
private int rightChild(int i){
return (i *2)+2;
}
private boolean isItLeafIndex(int i){
if (rightChild(i)>= size || leftChildIndex(i)>= size){
return true;
}
return false;
}
public String toString(){
String result ="{";
for (int i =0; i < size -1; i++){
result = result + items[i].toString()+",";
}
if

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!