Link to home
Start Free TrialLog in
Avatar of pete420
pete420

asked on

Adding words to a setADT

trying to write an application which used setADT as a spelling checker

how do you add words into the set?
then i want to read in some text and check to see which words are not in the 'known words' list.

all of my source code is located at

http://www.newwavesound.co.uk/java

even if some of the theory behind this is explained i mite be able to manage the coding myself.

any help is appreciated.

thanks pete




Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What about something like:

import java.util.*;

public class WordSet implements SetADT {
      Set words;
      
      public WordSet() {
            words = new HashSet();
      }
      
      public add(Object o) {
            words.add(o);
      }
}
Avatar of pete420
pete420

ASKER

while im sure that would work, i have not learnt hash tables yet so couldn't use one.

ta anyways

pete
>>yet so couldn't use one.

You need a Set. That's not a hash table. The only alternative would be to write your own - why give yourself such a difficult task?
Avatar of pete420

ASKER

sorry, i should have wrote at the top that this was for a uni,

i cant use things i havent learnt yet for that reason,

at the minute i just hard coded the words in like:

            String word1 = "test";
            Words.add (word1);
            String word2 = "that";
            Words.add (word2);            
            String word3 = "this";
            Words.add (word3);            
            String word4 = "is";
            Words.add (word4);            
            String word5 = "working";
            Words.add (word5);

not the best way to do it im sure but it is inputing the words into the set,


ta pete
What collection are you going to use to hold the words?
Avatar of pete420

ASKER

im using a Set ADT using arrays, i think thats what you want to know


my source is available at:
http://www.newwavesound.co.uk/java


if i havent answered ur question,


pete
OK, although in Java that is not a Set, as a Set cannot included duplicates. Where are we now - i'm not sure where you need help?
Avatar of pete420

ASKER

is what i have done so far not a set????

how do i make it no duplicates?

pete
>>is what i have done so far not a set????

No, not in Java terms, as your ArraySey.add does not exclude duplicates. You'd have to check for dups before adding
Avatar of pete420

ASKER

so in add, i should say something like:

if word already exists throw and error or exception telling the user the word is already in the set


pete
Not necessarily an error. You could even replace the old one by the new one, but here's one way of approaching it:


            public void add(Object element) {
                  boolean alreadyAdded = false;
                  for (int i = 0; (i < contents.length) && (alreadyAdded == false); i++) {
                        if (contents[i].equals(element)) {
                              alreadyAdded = true;
                        }
                  }
                  if (alreadyAdded == false) {
                        if (size() == contents.length) {
                              expandCapacity();
                        }
                        contents[count] = element;
                        count++;
                  }
            }
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pete420

ASKER

cheers, i will try it out..


it appears im lookin for answers i cant get here. I will have to start from the start and learn the whole thing from scratch.

the help was appreciated tho,. will split the points cuz u's were decent enough to reply :)

pete
>>,. will split the points

So what happened to the split!?