Question: The instructions is just to code the insert and changeKey methods for the MaxHeap class I keep getting a 0% for problem coverage. It states:

The instructions is just to code the insert and changeKey methods for the MaxHeap class

I keep getting a 0% for problem coverage. It states: "

Problem coverage: 0% Your problem setup does not appear to be consistent with the assignment.For this assignment, the proportion of the problem that is covered by your test cases is being assessed by running a suite of reference tests against your solution, and comparing the results of the reference tests against the results produced by your tests.In this case, none of the reference tests pass on your solution, which may mean that your solution (and your tests) make incorrect assumptions about some aspect of the required behavior. This discrepancy prevented Web-CAT from properly assessing the thoroughness of your solution or your test cases.Double check that you have carefully followed all initial conditions requested in the assignment in setting up your solution

.The following hint(s) may help you locate some ways in which your solution and your testing may be improved:Timeout occurred. Please note the time in the report does not reflect the time until the timeout."

This is the code that I did:

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class MaxHeap

{

private ArrayList students;

public MaxHeap(int capacity)

{

students = new ArrayList(capacity);

}

public MaxHeap(Collection collection)

{

students = new ArrayList(collection);

for(int i = size()/2; i >= 0; i--)

{

maxHeapify(i);

}

}

public Student getMax()

{

if(size() < 1)

{

throw new IndexOutOfBoundsException("No maximum value: the heap is empty.");

}

return students.get(0);

}

public Student extractMax()

{

Student value = getMax();

students.set(0,students.get(size()-1));

students.remove(size()-1);

maxHeapify(0);

return value;

}

public void insert(Student elt)

{

students.add(elt);

int i = students.size()-1;

int parent = parent(i);

while(i>0)

{

//Swap if the current object is greater than its parent

//if(students.get(p) > students.get(i)) doesn't work

if(students.get(i).compareTo(students.get(parent)) > 0)

{

swap(i,parent);

//moves up to the next level

i=parent;

}

else

{

break;

}

}

}

public void changeKey(Student s, double newGPA)

{

s.setGPA(newGPA);

int i = students.size()-1;

int parent = parent(i);

while(i>0)

{

if(students.get(i).compareTo(students.get(parent)) > 0)

{

swap(students.indexOf(s), i);

}

}

}

private int parent(int index)

{

return (index - 1)/2;

}

private int left(int index)

{

return 2 * index + 1;

}

private int right(int index)

{

return 2 * index + 2;

}

private int size()

{

return students.size();

}

private void swap(int from, int to)

{

Student val = students.get(from);

students.set(from, students.get(to));

students.set(to, val);

}

private void maxHeapify(int index)

{

int left = left(index);

int right = right(index);

int largest = index;

if (left < size() && students.get(left).compareTo(students.get(largest)) > 0)

{

largest = left;

}

if (right < size() && students.get(right).compareTo(students.get(largest)) > 0)

{

largest = right;

}

if (largest != index)

{

swap(index, largest);

maxHeapify(largest);

}

}

}

I was given these two classes.

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Before; import org.junit.Test;

public class MaxHeapTest { private MaxHeap heap;

@Before public void setUp() throws Exception { heap = new MaxHeap(10); heap.insert(new Student("Susan", 60, 3.5)); heap.insert(new Student("Ben", 70, 3.4)); heap.insert(new Student("Reed", 120, 4.0)); heap.insert(new Student("Johnny", 50, 1.2)); }

@Test public void test() { assertEquals(4.0, heap.extractMax().gpa(), .000001); assertEquals(3.5, heap.extractMax().gpa(), .000001); }

}

And this class:

public class Student implements Comparable { private String name; private double gpa = 0; private int units = 0; public Student(String name) { this.name = name; } public Student(String name, int units, double gpa) { this.name = name; this.units = units; this.gpa = gpa;

} public String getName() { return name; } public double gpa() { return gpa; } public void setGPA(double newGPA) { gpa = newGPA; } public int units() { return units; } public void setUnits(int newUnits) { units = newUnits; } public int compareTo(Student other) { double difference = gpa - other.gpa; if(difference == 0) return 0; if(difference > 0) return 12; return -14; } }

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!