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

home / study / engineering / computer science / computer science questions and a

ID: 3597519 • Letter: H

Question

home / study / engineering / computer science / computer science questions and answers / could some one help me debug this class in java: i suppose to return: yay1 yay2 yay3 but my ...

Question: Could some one help me debug this class in JAVA: I suppose to return: Yay1 Yay2 Yay3 But my code ...

Could some one help me debug this class in JAVA:

I suppose to return:

Yay0

Yay1

Yay2

Yay3

But my code only return

Yay0

2

Yay1

Yay2

Please help me find out where I got wrong!!! Thanks

import java.lang.reflect.Field;

import java.util.Arrays;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map.Entry;

import java.util.Set;

public class SimpleSet<T> {

//use this class if you are doing open addressing

//to represent a tombstone

static class Tombstone { }

private T t;

public void set(T t) { this.t = t; }

public T get() { return t; }

Set<Object> hashSet = new HashSet<Object>();

private HashMap<String, Object > map = new HashMap<String,Object>();

public boolean add(T value) {

//adds an item to the hash table

//if the load on the table is > 0.7

//rehash to the next prime number larger

//than twice the size before returning

//returns false if the value can not be added

//(i.e. the value already exists in the set)

//O(n) worst case, where n = the number of items

//O(1) or O(n/m) average case (where n/m is the load)

//the average case can be amortized Big-O

if(map.get(value)!=null){

return false;

}else{

map.put(String.valueOf(value), value);

return true;

}

}

public boolean remove(T value) {

//remove a value from the hash table

//remember to use tombstones if you are

//doing open addressing

//return false if the item could not be found

//return true if you remove the item

//O(n) worst case, where n = the number of items

//O(1) or O(n/m) average case (where n/m is the load)

if(map.get(value)!=null){

map.remove(String.valueOf(value));

return true;

}else{

return false;

}

}

@SuppressWarnings("unchecked")

public T get(T value) {

//return null if the item could not be found

//return the item FROM THE HASH TABLE if it was found

//(do not return the parameter, while "equal" they

//may not be the same)

//O(n) worst case, where n = the number of items

//O(1) or O(n/m) average case (where n/m is the load)

if(map.get(value)!=null){

return (T) map.get(String.valueOf(value));

}

return null;

}

public boolean contains(T value) {

//return true if the item can be found in the

//table, reuse code from get() to implement this

//method

//O(n) worst case, where n = the number of items

//O(1) or O(n/m) average case (where n/m is the load)

if(map.get(value)!=null){

return true;

}

return false;

}

@SuppressWarnings("unchecked")

public boolean rehash(int newCapacity) {

//rehash to a larger table size (specified as a

//parameter to this method)

//O(n) where n = the table size

  

if(newCapacity > map.size()){

try {

HashMap<String, Object> m = new HashMap<String, Object>(newCapacity);

Field tableField = HashMap.class.getDeclaredField("table");

tableField.setAccessible(true);

Entry[] es = (Entry[]) tableField.get(m);

es = Arrays.copyOfRange(es, 0, newCapacity);

tableField.set(m,es);

for (Entry<String, Object> e : map.entrySet()) {

m.put(e.getKey(), e.getValue());

}

map = m;

} catch (Exception e) {

return false;

}

return true;

}else{

return false;

}

}

public int size() {

//return the number of items in the table

//O(1)

return map.size();

}

public double getLoad() {

//return the load on the table

//O(1)

double load=0.0;

int size = 0;

Field tableField = null;

try {

tableField = HashMap.class.getDeclaredField("table");

tableField.setAccessible(true);

Object[] table = (Object[]) tableField.get(map);

size = (table == null ? 0 : table.length);

load = ((double)map.size())/size;

} catch (Exception e) {

return 0.0;

}

return load;

}

@SuppressWarnings("unchecked")

public Object[] valuesToArray() {

//take all the values in the table and put them

//into an array (the array should be the same

//size returned by the size() method -- no extra space!).

//Note: it doesn't matter what order the items are

//returned in, this is a set rather than a list.

//O(n) where n = the table size

return (Object[]) map.values().toArray(new Object[map.values().size()]);

}

//inefficiently finds the next prime number >= x

//this is written for you...

public int nextPrime(int x) {

while(true) {

boolean isPrime = true;

for(int i = 2; i < Math.sqrt(x); i++) {

if(x % i == 0) {

isPrime = false;

break;

}

}

if(isPrime) return x;

x++;

}

}

//example test code... edit this as much as you want!

public static void main(String[] args) {

SimpleSet<String> names = new SimpleSet<String>();

if(names.add("Fred") && names.add("Alex") && !names.add("Fred")) {

System.out.println("Yay 0");

}

System.out.println(names.size());

if(names.size() == 2 && names.contains("Fred") && names.get("Alex").equals("Alex")) {

System.out.println("Yay 1");

}

if(names.remove("Alex") && names.size() == 1 && names.get("Alex") == null && names.valuesToArray()[0].equals("Fred")) {

System.out.println("Yay 2");

}

names.rehash(11);

boolean loadOk = true;

if(names.getLoad() != 1/11.0 || !names.rehash(10) || names.getLoad() != 1/10.0 || names.rehash(1)) {

loadOk = false;

}

SimpleSet<Integer> nums = new SimpleSet<Integer>();

for(int i = 1; i <= 67 && loadOk; i++) {

nums.add(i);

double load = nums.getLoad();

if(load > 0.7 || (i < 8 && load != i/11.0) || (i > 7 && i < 17 && load != i/23.0) || (i > 16 && i < 33 && load != i/47.0) || (i > 32 && i < 68 && load != i/97.0)) {

loadOk = false;

}

}

if(loadOk) {

System.out.println("Yay 3");

}

}

}

Explanation / Answer

PLease find the Explaination below:
Explaination: You ae getting 2 because you are printing the size of the names array.
So comment that code.

Solution: //System.out.println(names.size());

Since names.rehash(1) returns false value. The rehash(1) function is
throwing an exception.It not able to rehash to new value so Its value is becoming as false.As a result;

if(names.getLoad() != 1/11.0 || !names.rehash(10) || names.getLoad() != 1/10.0 || names.rehash(1))
{
loadOk = false;
}


The above code will return the value for loadOk as false.

Hence as a result the compiler will not enter in the following code:
if(loadOk) //the code checks the loadOk is true or not
{
System.out.println("Yay 3"); // if true will print "Yay 3"
}


Thus the output that you get is:
Yay 0
2
Yay 1
Yay 2