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




pete420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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);
      }
}
0
pete420Author Commented:
while im sure that would work, i have not learnt hash tables yet so couldn't use one.

ta anyways

pete
0
CEHJCommented:
>>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?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

pete420Author Commented:
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
0
CEHJCommented:
What collection are you going to use to hold the words?
0
pete420Author Commented:
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
0
CEHJCommented:
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?
0
pete420Author Commented:
is what i have done so far not a set????

how do i make it no duplicates?

pete
0
CEHJCommented:
>>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
0
pete420Author Commented:
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
0
CEHJCommented:
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++;
                  }
            }
0
objectsCommented:
your add() should look like:

   public void add (Object element)
   {
       if (!contains(element))
       {
           if (size() == contents.length)
      expandCapacity();
            
           contents[count] = element;
           count++;
       }
   }

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pete420Author Commented:
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
0
CEHJCommented:
>>,. will split the points

So what happened to the split!?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.