Question: Please answer this is java. I already have the heap method, and a method which declares the variables and gets the names. But I cannot

Please answer this is java. I already have the heap method, and a method which declares the variables and gets the names. But I cannot figure out a tester method. Please make one which will work with my methods already posted. I have posted the full assignment below as well. Thank You

Objective:

You need to organize sheep in a heap. Fat sheep should go on the bottom so they dont crush the skinny sheep.

Sheep have:

o Name

o Weight

The heap (HINT: min-heap) should have the following methods:

o addSheep: inserts a new instance of a sheep. The sheep should be added to the bottom of the heap and it is expected to climb up the heap until its on top of a heavier sheep but below a light sheep.

o climbUp: used by addSheep to allow the sheep to climb the heap and get in the right place.

o removeSheep: removes the sheep from atop the heap. Then the sheep on the bottom of the heap, is put on the top and climbs down until its where it needs to be.

o climbDown: Used by remove sheep to move the sheep down to the right place. Always pick the lighter of the sheep below it and swap if the current sheep is heavier than the lighter one.

o sheepRollCall: Print out the sheeps name and weight in breadth order

o sheepHeapSort: return a sorted array of sheep

Finally write a test file that demonstrates your sheep heaping abilities.

o It should add 15 sheep

o Remove at least 5

o Demonstrate that these work by calling the sheep roll call

o Then show the sheep heap sort works

//My Heap

public class Sheep > { public static final int DEFAULT_SIZE=100; private T[] heap; private int size;//always assumed to look at the last open element public Sheep() { heap=(T[])(new Comparable[DEFAULT_SIZE]); size=0; } public Sheep(int length) { if(length>0) { heap=(T[])(new Comparable[length]); size=0; } } public void addSheep(T value) { if(size>=heap.length) { System.out.println("Filled up"); return; } heap[size]=value; //bubble up size++; } private void climbUp() { int index=this.size; while(index>0) { int parentIndex=index%2!=0?(index-1)/2:(index-1)/2; if(parentIndex>=0 && heap[index].compareTo(heap[parentIndex])>0) { //out of order, so swap T temp=heap[parentIndex]; heap[parentIndex]=heap[index]; heap[index]=temp; } else { break; } index=parentIndex; } } public T peek() { if(heap==null) return null; return heap[0]; } public T removeSheep() { T retVal=peek(); heap[0]=heap[size-1]; heap[size-1]=null; size--; //bubble down climbDown(); return retVal; } private void climbDown() { int index=0; while(index*2+1=0;i++) { System.out.print(tempHeap.removeSheep().toString()+" "); System.out.println(); } } }

//Declaring weight and name

public class FrontEnd { private String name; private double weight; public FrontEnd(String name, double weight) { this.name = name; this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }

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 Databases Questions!