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

Please answer this is java. I already have the heap method, and a method which d

ID: 3684322 • Letter: P

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 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 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

//My Heap


public class Sheep <T extends Comparable<T>> {
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<size)
  {
   int bigIndex=index*2+1;
   if(index*2+2 < size && heap[index*2+1].compareTo(heap[index*2+1])<0)//assumes left is bigger than right
   {
    bigIndex=index*2+2;//nope right is bigger
   }
   if(heap[index].compareTo(heap[bigIndex])<0)
   {
    //SWAP
    T temp=heap[index];
    heap[index]=heap[bigIndex];
    heap[bigIndex]=temp;
   }
   else
   {
    break;
   }
   index=bigIndex;
  }
}
public void sheepRollCall()
{
  for(T moo : heap)
  {
   if(moo==null)
    break;
   System.out.println(moo);
  }
}
public void sheepHeapsort()
{
  Sheep tempHeap = new Sheep(heap.length);
  T[] cloneHeap=heap.clone();
  for(int i=0;i<size;i++)
  {
   tempHeap.addSheep(cloneHeap[i]);
  }
  //heap sort
  for(int i=size;i>=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;
    }
}

Explanation / Answer

Note:

The given code is used as it is given, and the required code added in this solution.

Program:

public class Sheep <T extends Comparable<T>>

{

     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<size)

          {

              int bigIndex=index*2+1;

if(index*2+2 < size && heap[index*2+1].compareTo (heap[index*2+1])<0)//assumes left is bigger than right

              {

                   bigIndex=index*2+2;//nope right is bigger

              }

              if(heap[index].compareTo(heap[bigIndex])<0)

              {

                   //SWAP

                   T temp=heap[index];

                   heap[index]=heap[bigIndex];

                   heap[bigIndex]=temp;

              }

              else

              {

                   break;

              }

              index=bigIndex;

          }

     }

     public void sheepRollCall()

     {

          for(T moo : heap)

          {

              if(moo==null)

              break;

              System.out.println(moo);

          }

     }

     public void sheepHeapsort()

     {

          Sheep tempHeap = new Sheep(heap.length);

          T[] cloneHeap=heap.clone();

          for(int i=0;i<size-1;i++)

          {

              tempHeap.addSheep(cloneHeap[i]);

          }

          //heap sort

          for(int i=size-1;i>=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;

     }

}

// Tester class

public class SheepHeapTester

{

     // main method

    public static void main(String[] args)

     {

        final int heapSize = 15;

        Sheep heap = new Sheep(heapSize);

        // add the sheeps into heap

        heap.addSheep("Mimmy");

        heap.addSheep("Jicky");

        heap.addSheep("Karry");

        heap.addSheep("Iuzie Q");

        heap.addSheep("Burly");

        heap.addSheep("Cubba");

        heap.addSheep("WEGA");

        heap.addSheep("Jo Peep");

        heap.addSheep("Wueenie");

        heap.addSheep("Jluffy");

        heap.addSheep("Gabba");

        heap.addSheep("SUGA");

        heap.addSheep("Peep Ma");

        heap.addSheep("Jaenie");

        heap.addSheep("Blufy");

         

        // print the sheep details

        System.out.println("current thing is");

        heap.sheepRollCall();

        System.out.println();

         

        // performt the heap sort

        System.out.println("Sorted Sheepies :)");

        heap.sheepHeapsort();

         

        // remove the sheep from heap

        heap.removeSheep();

         

        // print the sheep details

        heap.sheepRollCall();

    }

}

Result:

current thing is

Mimmy

Jicky

Karry

Iuzie Q

Burly

Cubba

WEGA

Jo Peep

Wueenie

Jluffy

Gabba

SUGA

Peep Ma

Jaenie

Blufy

Sorted Sheepies :)

Mimmy

Jicky

Peep Ma

SUGA

Jaenie

Jluffy

Wueenie

Iuzie Q

WEGA

Jo Peep

Gabba

Cubba

Karry

Burly