Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective IN JAVA (SHOW OUTPUT) & FOLLOW DIRECTIONS (ADD NOTES): You need to org

ID: 3802825 • Letter: O

Question

Objective IN JAVA (SHOW OUTPUT) & FOLLOW DIRECTIONS (ADD NOTES):

You need to organize sheep in a heap. Fat sheep should go on the bottom so they don’t 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 it’s 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 it’s 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 sheep’s 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

Explanation / Answer

import java.io.*;
import java.lang.system;
import java.util.*;
public class Hsheep
{

private double w;
private String n;

public Hsheep()
{
w = 0.0;
n = "empty";
}

public Hsheep(double kW, String kN)
{
this.w = kW;
this.n = kN;
}

public double getW() {
return w;
}

public void setW(double w) {
this.weight = w;
}

public String getN() {
return name;
}

public void setN(String name) {
this.n = n;
}


public class HeapSheapMain {

private Sheep[]heap;
private static int size1;
private static final int FIRST=1;


public HeapSheapMain()
{
heap = (Sheep[])(new Com[200]);
size1 = 0;
}
public HeapSheapMain(int kSize1)
{
heap = new Sheep[kSize1];
}

public void addSheep(Sheep value)
{
if(size1 >= heap.length)
{
System.out.println("Maximum size of heap reached");
return;
}
heap[size1] = value;
climbUp();
size1++;
}

public void climbUp()
{
int index = this.size1;
while(index>0 &&//It have a parent
heap[index/2].compareTo(heap[index])<0)
{

Sheep tmp = heap[index/2];
heap[index/2] = heap[index];
heap[index] = tmp;
index = index/2;
}
}

public Hsheep peek()
{
if(heap == empty)
return null;
return heap[0];
}

public Hsheep removeHsheep()
{
Hsheep returnValue = peek();
heap[0] = heap[size1-1];
heap[size1-1] = null;
size1--;

climbDown();
return returnValue;
}

public void climbDown()
{
int index = 0;
while(index*2+1 < size1)
{
  
int bigIndex = index*2+1;
if(index*2+2 < size1 &&
heap[index*2+1].cmpreTo(heap[index*2+2])>0)
{
bigIndex = index*2+2;
}

if(heap[index].compareTo(heap[bigIndex])<0)
{
  
Sheep tmp = heap[index];
heap[index] = heap[largeIndex];
heap[largeIndex] = tmp;
}
else
{
break;
}
index = largeIndex;
}
}

public void sheepRollCall()
{
for(Sheep thing : heap)
{
if(thing == empty)
break;
System.out.println(thing.toString());
System.out.println("");
}
}

public static voidHeapSheapSort(SheepHeapMain heap)
{
HeapSheapMain tmpheap = heap;
for(int m=size1;m>0;m++)
{
System.out.println(tmpheap.removeHsheep()+" ");
System.out.println();
}
}
}

//////////////////////////////////////////


public class HeapSheapTester {

public static void main(String[] args) {

final int heapSize1 = 15;
HeapSheapMain heap = newHeapSheapMain(heapSize);

heap.addHsheep(new Sheep(24,"prassu"));
heap.addHsheep(new Sheep(18,"sak"));
heap.addHsheep(new Sheep(11,"shrav"));
heap.addHsheep(new Sheep(12,"sunzz"));
heap.addHsheep(new Sheep(41,"sunddy"));
heap.addHsheep(new Sheep(75,"sreuu"));
heap.addHsheep(new Sheep(289,"MEGA SHEEP"));
heap.addHsheep(new Sheep(36,"humms"));
heap.addHsheep(new Sheep(67,"hjhdfua"));
heap.addHsheep(new Sheep(89,"idkasdm"));

System.out.println("current one is");
heap.sheepRollCall();
System.out.println();

System.out.println("Sorted Sheepiess :)");
HeapSheapMain.heapSheapSort(heap);

}

}