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.ioFile;
import java.ioFileNotFoundException;
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
public class SheepScheduling
public static void mainString args throws FileNotFoundException
Scanner sc new ScannerSystemin;
while true
System.out.printEnter the file name: ;
String name scnextLine;
Scanner file new Scannernew Filename;
List sheeps new LinkedList;
MinHeap waitingSheeps new MinHeap;
while filehasNextLine
String line file.nextLine;
String sheep line.splits;
sheeps.addnew Sheepsheep Integer.parseIntsheep Integer.parseIntsheep;
sheeps.sortComparatorcomparingSheep::getArrivalTime;
String output ;
int arrivalTime ;
System.out.printlnSchedule from the Provided File:";
while sheeps.isEmpty
Sheep temp sheeps.remove;
arrivalTime temp.getShearingTime;
while sheeps.isEmpty
if sheepsgetgetArrivalTime arrivalTime
waitingSheeps.insertsheepsremove;
else
break;
System.out.printlntemp;
while waitingSheeps.isEmpty
Sheep k waitingSheeps.remove;
arrivalTime kgetShearingTime;
System.out.printlnk;
while sheeps.isEmpty
if sheepsgetgetArrivalTime arrivalTime
waitingSheeps.insertsheepsremove;
else
break;
System.out.printlnDo you want to run again?yn: ;
String choice scnextLine;
if choiceequalsIgnoreCasen
break;
public class Sheep implements Comparable
private String name;
private int shearingTime;
private int arrivalTime;
public SheepString 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 compareToSheep o
int res this.shearingTime oshearingTime;
if res
return this.name.compareTooname;
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 ;
items E new Comparable;
public int size
return size;
public void insertE e
if size items.length
return;
itemssize e;
bubbleUpsize;
size;
public E remove
if size
E popped items;
items itemssize;
bubbleDown;
return popped;
return null;
public E peek
if size
return items;
throw null;
public void updateE item
int i ;
E temp null;
for i ; i size; i
if itemsicompareToitem
temp itemsi;
itemsi item;
break;
if tempcompareToitem
bubbleDowni;
else
bubbleUpi;
public boolean isEmpty
return size ;
private void bubbleUpint current
while itemscurrentcompareToitemsparentIndexcurrent
swapcurrent parentIndexcurrent;
current parentIndexcurrent;
private void bubbleDownint i
if isItLeafIndexi
if itemsicompareToitemsleftChildIndexiitemsicompareToitemsrightChildi
if itemsleftChildIndexicompareToitemsrightChildi
swapi leftChildIndexi;
bubbleDownleftChildIndexi;
else
swapi rightChildi;
bubbleDownrightChildi;
private void swapint x int y
E tmp;
tmp itemsx;
itemsx itemsy;
itemsy tmp;
private int parentIndexint i
return i ;
private int leftChildIndexint i
return i ;
private int rightChildint i
return i ;
private boolean isItLeafIndexint i
if rightChildi size leftChildIndexi size
return true;
return false;
public String toString
String result ;
for int i ; i size ; i
result result itemsitoString;
if
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
