Question

JAVA (advanced data structures) write a completed program using HashIntSet and HashMain

Exercises 1. Write a method in the HashIntset class called addA11 that accepts another hash set as a parameter and adds all o

include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks

HashIntSet.java

public class HashIntSet {

private static final double MAX_LOAD_FACTOR = 0.75;

private HashEntry[] elementData;

private int size;

// Constructs an empty set.

public HashIntSet() {

elementData = new HashEntry[10];

size = 0;

}

// Adds the given element to this set, if it was not already

// contained in the set.

public void add(int value) {

if (!contains(value)) {

if (loadFactor() >= MAX_LOAD_FACTOR) {

rehash();

}

// insert new value at front of list

int bucket = hashFunction(value);

elementData[bucket] = new HashEntry(value,

elementData[bucket]);

size++;

}

}

// Removes all elements from the set.

public void clear() {

for (int i = 0; i < elementData.length; i++) {

elementData[i] = null;

}

size = 0;

}

// Returns true if the given value is found in this set.

public boolean contains(int value) {

int bucket = hashFunction(value);

HashEntry current = elementData[bucket];

while (current != null) {

if (current.data == value) {

return true;

}

current = current.next;

}

return false;

}

// Returns true if there are no elements in this queue.

public boolean isEmpty() {

return size == 0;

}

// Removes the given value if it is contained in the set.

// If the set does not contain the value, has no effect.

public void remove(int value) {

int bucket = hashFunction(value);

if (elementData[bucket] != null) {

// check front of list

if (elementData[bucket].data == value) {

elementData[bucket] = elementData[bucket].next;

size--;

} else {

// check rest of list

HashEntry current = elementData[bucket];

while (current.next != null && current.next.data != value)

{

current = current.next;

}

// if the element is found, remove it

if (current.next != null && current.next.data == value) {

current.next =

current.next.next;

size--;

}

}

}

}

// Returns the number of elements in the queue.

public int size() {

return size;

}

// Returns a string representation of this queue, such as "[10, 20,

30]";

// The elements are not guaranteed to be listed in sorted order.

public String toString() {

String result = "[";

boolean first = true;

if (!isEmpty()) {

for (int i = 0; i < elementData.length; i++) {

HashEntry current = elementData[i];

while (current != null) {

if (!first) {

result += ", ";

}

result += current.data;

first = false;

current = current.next;

}

}

}

return result + "]";

}

// Returns the preferred hash bucket index for the given value.

private int hashFunction(int value) {

return Math.abs(value) % elementData.length;

}

private double loadFactor() {

return (double) size / elementData.length;

}

// Resizes the hash table to twice its former size.

private void rehash() {

// replace element data array with a larger empty version

HashEntry[] oldElementData = elementData;

elementData = new HashEntry[2 * oldElementData.length];

size = 0;

// re-add all of the old data into the new array

for (int i = 0; i < oldElementData.length; i++) {

HashEntry current = oldElementData[i];

while (current != null) {

add(current.data);

current = current.next;

}

}

}

// Represents a single value in a chain stored in one hash bucket.

private class HashEntry {

public int data;

public HashEntry next;

public HashEntry(int data) {

this(data, null);

}

public HashEntry(int data, HashEntry next) {

this.data = data;

this.next = next;

}

}

}

HashMain.java

import java.util.Arrays;

public class HashMain {

public static void main(String[] args) {

HashIntSet set = new HashIntSet();

set.add(7);

set.add(5);

set.add(1);

set.add(9);

// test adding duplicates

set.add(7);

set.add(7);

set.add(5);

// test collisions and linked lists

set.add(45);

set.add(91);

set.add(71);

// test rehashing

set.add(810); // re-hash should occur here (8/10, load = 0.8)

set.add(62);

set.add(1238);

set.add(-99);

set.add(-30);

set.add(42);

set.add(49857);

set.add(1520); // re-hash should occur here (15/20, load = 0.75)

set.add(2);

set.add(31);

set.add(11);

set.add(21);

// test removal

set.remove(7);

set.remove(9);

set.remove(1);

set.remove(1);

set.remove(21);

set.remove(71);

for (int n : new int[] {1520, 99, 2, 55, 42, 11, 45, 0, 5, -10,

810}) {

System.out.println("contains " + n + "? " +

set.contains(n));

}

System.out.println(set + ", size " + set.size());

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//1
//place this method in HasIntSet class
void addAll(HashIntSet n)
{
   if(!n.isEmpty())//if n is not empty
   {
   for(int i=0;i<n.elementData.length;i++)  
   {
       HashEntry current = n.elementData[i];
       while(current !=null)
       {
           add(current.data);//adding element to set
           current=current.next;
       }  
   }
   }
}
//to place in main
static void addAll(HashIntSet n,HashIntSet s)//adds all elements in n to s
{
   if(!n.isEmpty())//if n is not empty
   {
   for(int i=0;i<n.elementData.length;i++)  
   {
       HashEntry current = n.elementData[i];
       while(current !=null)
       {
           s.add(current.data);//adding element to set
           current=current.next;
       }  
   }
   }
}

public static void main(String[] args) {

HashIntSet set = new HashIntSet();
HashIntSet set2 = new HashIntSet();
set2.add(7);
set2.add(11);
set.add(9);
set.add(7);

set.add(5);

set.add(1);

set.add(9);

// test adding duplicates

set.add(7);

set.add(7);

set.add(5);

// test collisions and linked lists

set.add(45);

set.add(91);

set.add(71);

// test rehashing

set.add(810); // re-hash should occur here (8/10, load = 0.8)

set.add(62);

set.add(1238);

set.add(-99);

set.add(-30);

set.add(42);

set.add(49857);

set.add(1520); // re-hash should occur here (15/20, load = 0.75)

set.add(2);

set.add(31);

set.add(11);

set.add(21);

// test removal

set.remove(7);

set.remove(9);

set.remove(1);

set.remove(1);

set.remove(21);

set.remove(71);

for (int n : new int[] {1520, 99, 2, 55, 42, 11, 45, 0, 5, -10,

810}) {

System.out.println("contains " + n + "? " +

set.contains(n));

}

System.out.println(set + ", size " + set.size());


//set2 has 7 ,11, 9

set.addAll(set2);//adding all elments of set2 to set


}

Add a comment
Know the answer?
Add Answer to:
JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method in the HashIntSet class called addAll that accepts another hash set as a...

    Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...

  • JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of objects using a hash table. // The hash table uses separate chaining to resolve collisions. // Original from buildingjavaprograms.com supplements public class HashSet<E> { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry<E>[] elementData; private int size; // Constructs an empty set. @SuppressWarnings("unchecked") public HashSet() { elementData = new HashEntry[10]; size = 0; } // ADD METHODS HERE for exercise solutions: // Adds the...

  • JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public...

    JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public class ArrayIntList { private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 100; // pre : capacity >= 0 // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } // post: constructs an empty list of default capacity...

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • Identify the letters and anything associated with it. It is a hash map so please feel...

    Identify the letters and anything associated with it. It is a hash map so please feel free to point out the other important parts beside the arrows. I apologize for the order of the pictures class HashMap private: HashEntry table ; public: HashMap() table-new HashEntry * [TABLE_SIZE]; for (int 1-0; 1< TABLE SIZE: i++) table [i] = NULL; Hash Function int HashFunc(int key) return key % TABLE-SIZE; Insert Element at a key void Insert(int key, int value) int hashHashFunc(key); while...

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT