Question
/* I want to track my indexes for the students class to make my algorithm more efficient. I was able to set the indexes and
/* I want to track my indexes for the students class to make my algorithm more efficient. I was able to set the indexes and get them, but how do I test where my indexes are located? please help/explain how do I test those indexes in my test program. Thanks*/
import java.util.ArrayList; import java.util.Collection;
public class MaxHeap { private ArrayList
public MaxHeap(int capacity) { students = new ArrayList
//compare with parent node, if larger moveUp private void moveUp(int i) { while( i > 0 && students.get(i).compareTo(students.get(parent(i))) > 0){ swap(i, parent(i)); i = parent(i); } } }
public class Student implements Comparable
private String name;
private double gpa = 0;
private int units = 0;
private int index = 0;
public Student(String name) {
this.name = name;
}
public Student(int index, String name, int units, double gpa) {
this.name = name;
this.units = units;
this.gpa = gpa;
this.index = index;
}
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 index() {
return index;
}
public int getIndex() {
return index;
}
public void setIndex(int newIndex) {
index = newIndex;
}
public int compareTo(Student other)
{
double difference = gpa - other.gpa;
if(difference == 0) return 0;
if(difference > 0) return 12;
return -14;
}
}
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Test;
public class MaxHeapTest { private MaxHeap heap;
@Before public void setUp() throws Exception { heap = new MaxHeap(10); Student Annie = new Student(, "Annie", 0, 0.0); Student Brandon = new Student(0, "Brandon", 120, 3.6); Student Cathy = new Student("Cathy", 110, 2.3); Student Dana = new Student("Dana", 40, 3.3); Student Emily = new Student("Emily"); Student Foxy = new Student("Foxy", 70, 3.0); heap.insert(Annie); heap.insert(Brandon); heap.insert(Cathy); heap.insert(Dana); heap.insert(Emily); heap.insert(Foxy);
heap.changeKey(Foxy, 3.4); heap.changeKey(Cathy, 4.0); heap.changeKey(Dana, 3.3); Emily.setUnits(115); Emily.setGPA(3.8); }
@Test public void test() throws Exception { assertEquals(3.6, heap.extractMax().gpa(), 0.00001); assertEquals(4.0, heap.extractMax().gpa(), 0.00001); assertEquals("Emily", heap.extractMax().getName()); assertEquals(70, heap.extractMax().units(), 0.00001); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started