My heap is suppposed to print out in breadth order and sort the sheep from lightest to heaviest (min-heap). My test file should add 15 sheep and remove at least 5. I havent attempted the remove part yet because i'm getting an array index out of bounds error. Any help is appreciated.
Here are my classes:
public class Sheep {
private double weight;
private String name;
public Sheep()
{
weight = 0.0;
name = "null";
}
public Sheep(double aWeight, String aName)
{
this.weight = aWeight;
this.name = aName;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//may not be necessary...
@Override
public String toString() {
return "Sheep [weight=" + weight + ", name=" + name + "]";
}
//this may not be necessary...
public int compareTo(Sheep sheep)
{
if (this.weight>sheep.weight)
return 1;
else if(this.weight<sheep.weight)
return -1;
else
return 0;
}
}
//****************************************
public class SheepHeapMain {
private Sheep[]heap;
private static int size;
private static final int FIRST=1; //possibly needed, how do you do this
public SheepHeapMain()
{
heap = (Sheep[])(new Comparable[100]);
size = 0;
}
public SheepHeapMain(int aSize)
{
heap = new Sheep[aSize];
}
public void addSheep(Sheep val)
{
if(size >= heap.length)
{
System.out.println("Max size of heap has been reached");
return;
}
heap[size] = val;
climbUp();
size++;
}
public void climbUp()
{
int index = this.size;
while(index>0 &&//It has a parent
heap[index/2].compareTo(heap[index])<0)//And the value of the child is greater than the parent
{
//SWAP
Sheep temp = heap[index/2];
heap[index/2] = heap[index];
heap[index] = temp;
index = index/2;
}
}
public Sheep peek()
{
if(heap == null)
return null;
return heap[0];
}
public Sheep removeSheep()
{
Sheep returnVal = peek();
heap[0] = heap[size-1]; //index Out of bonunds error
heap[size-1] = null;
size--;
climbDown();
return returnVal;
}
public void climbDown()
{
int index = 0;
while(index*2+1 < size)//While there is a left child
{
//Find smallest child
int bigIndex = index*2+1;
if(index*2+2 < size &&
heap[index*2+1].compareTo(heap[index*2+2])>0) //Right was bigger than left so change it
{
bigIndex = index*2+2;
}
if(heap[index].compareTo(heap[bigIndex])<0)//If current index is greater than smaller index
{
//SWAP
Sheep temp = heap[index];
heap[index] = heap[bigIndex];
heap[bigIndex] = temp;
}
else
{
break;//We're done!
}
index = bigIndex;//Update the index to continue
}
}
public void sheepRollCall()
{
for(Sheep thing : heap)
{
if(thing == null)
break;
System.out.println(thing.toString());
System.out.println("");
}
}
public static void sheepHeapSort(SheepHeapMain heap)
{
SheepHeapMain tempheap = heap;
for(int i=size;i>0;i++)
{
System.out.println(tempheap.removeSheep()+" ");
System.out.println();
}
}
}
//****************************************************************
public class SheepHeapTester {
public static void main(String[] args) {
final int heapSize = 15;
SheepHeapMain heap = new SheepHeapMain(heapSize);
heap.addSheep(new Sheep(55,"Jimmy"));
heap.addSheep(new Sheep(43,"Ricky"));
heap.addSheep(new Sheep(77,"Larry"));
heap.addSheep(new Sheep(12,"Suzie Q"));
heap.addSheep(new Sheep(91,"Curly"));
heap.addSheep(new Sheep(85,"Bubba"));
heap.addSheep(new Sheep(189,"MEGA SHEEP"));
heap.addSheep(new Sheep(46,"Bo Peep"));
heap.addSheep(new Sheep(27,"Queenie"));
heap.addSheep(new Sheep(19,"Fluffy"));
System.out.println("current thing is");
heap.sheepRollCall();
System.out.println();
System.out.println("Sorted Sheepies :)");
SheepHeapMain.sheepHeapSort(heap);
}
}