Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding words to a setADT

Posted on 2004-10-21
14
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

636 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