Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write JUnit tests to test all cases of the MaxHeap.java. The test cases should cover all parts of the code MaxHeap.java import java.util.ArrayList; import java.util.Collection;

Write JUnit tests to test all cases of the MaxHeap.java. The test cases should cover all parts of the code

MaxHeap.java

import java.util.ArrayList; import java.util.Collection;

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) { //Please write me. students.add(elt); int s = size();

while(s >0 && students.get(s-1).compareTo(students.get(parent(s-1)))>0){ swap(s-1,parent(s-1)); s = parent(s)+1; } } public void changeKey(Student s, double newGPA) { //Please write me. int indexOfStudent = students.indexOf(s);

if(indexOfStudent != -1) {

s.setGPA(newGPA);

students.set(indexOfStudent, s);

}

}

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; } 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); } } }

Student.java

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago