?
Solved

Adding words to a setADT

Posted on 2004-10-21
14
Medium Priority
?
261 Views
Last Modified: 2012-06-21
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




0
Comment
Question by:pete420
  • 7
  • 6
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12373092
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
 

Author Comment

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

ta anyways

pete
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12373307
>>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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:pete420
ID: 12373340
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12373455
What collection are you going to use to hold the words?
0
 

Author Comment

by:pete420
ID: 12373476
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12373510
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
 

Author Comment

by:pete420
ID: 12373524
is what i have done so far not a set????

how do i make it no duplicates?

pete
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12373553
>>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
 

Author Comment

by:pete420
ID: 12373567
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12373636
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12375755
your add() should look like:

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

0
 

Author Comment

by:pete420
ID: 12375817
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12375827
>>,. will split the points

So what happened to the split!?
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month3 days, 11 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question