Results 1 to 4 of 4
Thread: need help with a project
- 08-31-2009, 09:25 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
need help with a project
Hello,
i have some problems with a project for school.
Im newbie with java, so im sorry for my mistakes.
title: make an HashSet (with arrays of int),
Set with no negative elements.
loadfactor max 0.75
This is the class (implemented)
package ins;
import java.util.*;
public class HashSet implements Set {
//assign to an int a position in the table.
final private int address(int x, int l) {
int hash = (new Integer(x)).hashCode();
return (hash & 0x7FFFFFFF) % l;
}
public Node[] tabSet;
public int tabSetCap;
public int setElements;
public int firstDim;
private static double defaultRatio = 0.75;
private static int defaultDim = 3;
public HashSet() {
this(defaultDim);
}
public HashSet(int dim) {
tabSetCap = dim;
setElements = 0;
tabSet = new Node[tabSetCap];
}
public boolean put(int x) {
if(x < 0) throw new IllegalArgumentException();
int hashcode = address(x,tabSetCap);
Node l = new Node(null, tabSet[hashcode]);
tabSet[hashcode] = l;
while (l.next != null) {
if (l.next.key.intValue() == x){
tabSet[hashcode] = tabSet[hashcode].next;
return false;
}
l = l.next;
}
l.next = new Node((new Integer(x)),null);
setElement++;
tabSet[hashcode] = tabSet[hashcode].next;
if (expand())
rehash(tabSetCap*2);
return true;
}
public boolean contains(int x) {
int hashcode = address(x,tabSetCap);
Node l = tabSet[hashcode];
while (l != null) {
if (l.key.intValue() == x)
return true;
else
l = l.next;
}
return false;
}
public int[] toArray() {
int[] arraySet = new int[tabSetCap];
int i = 0;
Iterator it = iterator();
while (it.hasNext()) {
arraySet[i] = (Integer)it.next();
i++;
}
for (; i < arraySet.length; i++)
arraySet[i] = -1;
Arrays.sort(arraySet);
return arraySet;
}
public void union(Set y) {
int[] unionSet = y.toArray();
for (int i = 0; i < unionSet.length; i++) {
if (unionSet[i] >= 0)
put(unionSet[i]);
}
}
public int max() {
throw new UnsupportedOperationException();
}
public boolean remove(int x) {
throw new UnsupportedOperationException();
}
public Object clone() throws CloneNotSupportedException {
throw new UnsupportedOperationException();
}
public Iterator iterator() {
return new IteratorHashSet(this);
}
private boolean expand() {
return
((double))setElements/tabSetCap > defaultRatio;
}
private void rehash(int newdim) {
Node[] newtabSet = new Node[newdim];
for (int i = 0; i < tabSetCap; i++) {
Node l = tabSet[i];
while(l != null) {
Integer obj = (Integer) l.key;
int pos = address(obj.intValue(),newdim);
newtabSet[pos]= new Node(l.key, newtabSet[pos]);
l=l.next;
}
}
tabSetCap = newdim;
tabSet = newtabSet;
}
public void clear(){
setElements = 0;
}
public int size() {
return setElements;
}
public boolean isEmpty() {
return setElements == 0;
}
public String toString() {
int[] arr = toArray();
String out = "[";
if(setElements == 0)
out = out + "]";
else {
for(int i = 0; i < arr.length; i++) {
if(arr[i] > -1)
out = out + arr[i] + "]";
}
}
return out;
}
}
class Node {
Integer key;
Node next;
Node(Integer k, Node n) {
key = k;
next = n;
}
}
INTERFACE:
package ins;
import java.util.Iterator;
public interface Set extends Iterable, Cloneable{
/**
* make empty
*/
void clear();
/**
* is empty?
* @return true if the set is empty else return false.
*/
boolean isEmpty();
/**
* @return the number of object in the set
*/
int size();
/**
* @return max int in the set, -1 if the set is empty
*/
int max();
/**
* add int to the set
* @param x int to add
* @return true if int is already inside, else return false
* @exception IllegalArgumentException if x<0
*/
boolean put(int x);
/**
* contains
* @return true if int is already inside, else return false
*/
boolean contains(int x);
/**
* remove int from set
* @param x .
* @return true if int is already inside, else return false
*/
boolean remove(int x);
/**
* Convert to string
* return a string with elements in ascending order
* @return la stringa
*/
String toString();
/**
* convert to array
* return array of int with elements in ascending order
* @return l'array
*/
int [] toArray();
/**
* clone
* @return clone of set
* @throws CloneNotSupportedException
*/
Object clone() throws CloneNotSupportedException;
/**
* return an iterator (elements in ascending order)
* method remove not implemented
* @return the iterator
*/
Iterator iterator();
/**
* Unione
* the set will be modifie with the elements inside y
* @param y the second set
*/
void unione (Set y);
}
ITERATOR
package ins;
import java.util.*;
class IteratorHashSet implements Iterator {
private HashSet insh;
private int pos;
private Node rif;
public HashSetIterator(HashSet tabSet) {
insh = tabSet;
pos = 0;
while (pos < insh.tabSetCap && insh.tabSet[pos] == null) pos++;
rif = insh.tabSet[pos];
}
public boolean hasNext() {
return pos == insh.tabSetCap;
}
public Object next() {
Object e = rif.key;
if (rif.next != null)
rif = rif.next;
else { //rif == null
while (pos < insh.tabSetCap && insh.tabSet[pos] == null) pos++;
if (pos < insh.tabSetCap) rif = insh.tabSet[pos];
else rif = null;
}
return e;
}
public void remove() {
throw new UnsupportedOperationException("not supported");
}
}
Sorry for my english, i hope you can help me!
My test (if is necessary i will post it) give to me some errors
in particular with the methods toArray() and toString()
Thanks,
best regards.Last edited by drk; 08-31-2009 at 07:28 PM.
- 08-31-2009, 07:39 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
if you are receiving errors, it would be best to post the complete message that the compiler gives you and indicate what line of code is causing the error, as well as say what exactly that piece of code should be doing.
- 08-31-2009, 07:53 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
Sorry my fault!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at ins.HashSetIterator.<init>(HashSetIterator.java:15 )
at ins.HashSet.iterator(HashSet.java:155)
at ins.HashSet.toArray(HashSet.java:106)
at ins.HashSet.toString(HashSet.java:195)
at ins.HashTest.main(HashTest.java:14)
Set s1 = new HashSet();
System.out.println(s1.toString()); <------ line 14 of HashTest
Best regards.
- 08-31-2009, 08:03 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
err, here's some tips on reading the compiler output.
what you see here is the calling stack, so the bottom most function called the one right above it and so on and so forth. thus, your error is more specifically at line 15 of HashSetIterator.Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 // this tells your the exception thrown at ins.HashSetIterator.<init>(HashSetIterator.java:15 ) // this is the top of the stack at ins.HashSet.iterator(HashSet.java:155) at ins.HashSet.toArray(HashSet.java:106) at ins.HashSet.toString(HashSet.java:195) at ins.HashTest.main(HashTest.java:14) // this is the bottom of the stack
now, the first line tells you exactly what went wrong at the indicated line. ArrayIndexOutOfBoundsException tells you that you are trying to access an index of an array that doesn't have that index. for example, if you have an array of size 2, you only have arr[0] and arr[1]. if you try to access arr[2], then you will get this exception thrown. it's your goal to prevent these from occurring, usually using the properties of the array, most notably arr.length
Similar Threads
-
need help for a project
By megironi in forum New To JavaReplies: 4Last Post: 02-07-2009, 08:28 PM -
open existing project project ..
By itaipee in forum EclipseReplies: 1Last Post: 12-28-2008, 08:12 PM -
How to use ejb in our project
By Ragini Shukla in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 12-26-2008, 09:29 AM -
Help With Project!!!
By jackhammer in forum New To JavaReplies: 5Last Post: 12-04-2008, 05:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks