Study the Table class and write a JUnit test class TableTest.java to test the Table class as follows. Submit the test class and screen captures showing pass of all the tests. Either JUnit4 or JUnit 5 in any IDE may be used. a. Create a table and put 10 entries of your choice (e.g., testTable.put(1, “testA”)).
Study the Table class and write a JUnit test class TableTest.java to test the Table class as
follows. Submit the test class and screen captures showing pass of all the tests. Either JUnit4
or JUnit 5 in any IDE may be used.
a. Create a table and put 10 entries of your choice (e.g., testTable.put(1, “testA”)).
Below is the code for pbulic class Table :
public class Table
{
private int manyItems;
private Object[] keys;
private Object[] data;
private boolean[] hasBeenUsed;
public Table(int capacity){
if (capacity <= 0)
throw new IllegalArgumentException("Capacity is negative");
keys=new Object[capacity];
data=new Object[capacity];
hasBeenUsed=new boolean[capacity];
}
public boolean containsKey(Object key){
return findIndex(key)!=-1;
}
public int findIndex(Object key){
int count=0;
int i=hash(key);
while (count <= data.length && hasBeenUsed[i]){
if (key.equals(keys[i]))
return i;
count++;
i=nextIndex(i);
}
return -1;
}
public Object get(Object key){
int index=findIndex(key);
if (index==-1)
return null;
else
return data[index];
}
private int hash(Object key){
return Math.abs(key.hashCode())%data.length;
}
private int nextIndex(int i){
if (i+1 == data.length)
return 0;
else
return i+1;
}
public Object put(Object key, Object element){
int index = findIndex(key);
Object answer;
if (index!=-1){ // The key is already in the table.
answer = data[index];
data[index] = element;
return answer;
}
else if (manyItems<data.length){ // The key is not yet in this Table.
index=hash(key);
while (keys[index]!=null)
index = nextIndex(index);
keys[index] = key;
data[index] = element;
hasBeenUsed[index] = true;
manyItems++;
return null;
}
else{ // The table is full.
throw new IllegalStateException("Table is full.");
}
}
public Object remove(Object key){
int index = findIndex(key);
Object answer = null;
if (index!=-1) // The key exists in the table.
{
answer=data[index];
keys[index]=null;
data[index]=null;
manyItems--;
}
return answer;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
Study the Table class and write a JUnit test class TableTest.java to test the Table class as follows. Submit the test class and screen captures showing pass of all the tests. Either JUnit4 or JUnit 5 in any IDE may be used.
>>Table class code is in previous question
a. Test findIndex() for an non-existing entry.
b. Test findIndex() for an existing entry.
c. Test put() for an non-existing entry.
d. Test put() for an existing entry
e. Test put() for a new entry putting into the full table.