• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Java program to make a word worth $1.00

Need help write a Java program which will answer the question describ below.

The word has to be a 100 point word...like A is worth 1 point b=2, c=3 etc.
I need make a 100 point word. ( A word can find in dictionary.)

Thanks.
0
SarahDeng
Asked:
SarahDeng
  • 13
  • 6
1 Solution
 
for_yanCommented:
Is it anacademic assignement?
If it is, please write your version and we'll help with debugging/correcting.
If it is not then please state it clearly in your reply, as EE policy does not allow
to do academic assignements for the askers.
0
 
for_yanCommented:
Also please explain what means  "A word can find in dictionary" ?
Do you have a list of words among whhci it should be found?
0
 
SarahDengAuthor Commented:
This is not an academic assignment. It is a game for kids. Actually I know the answer.(such as posting, attitute, etc.) Just thinking how to do this through a program. I have not found an argorithm or logic to come up the word.

I mean it should be an English word.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
for_yanCommented:
You can write a method which will calculate the worth for each word and then have sufficiently good number of
words and run them through this method - I'm sure some will turn up to be 100 points worth.


I thijk this  is a static method that would calculate
the worth:

  public static int getWordValue(String s){
        int nsum = 0;
         s = s.toLowerCase();

        for(int j=0; j<s.length(); j++){
            nsum += (s.charAt(j) - 'a' + 1);

        }

        return nsum;


    }

Open in new window



0
 
SarahDengAuthor Commented:
for_yan,

 This is not what I mean. I really want it another way around, which the program comes up the list of word that is worth 100 point. Not input the word to check if it is 100.

BTW. Your program works as you plan to do.
0
 
for_yanCommented:
But the program cannot do it beacuse how would it know what is the real word - what is not?
0
 
for_yanCommented:
Look, I wrote this program and fed in the text in the attached file

public class FindWords {

    public static int getWordValue(String s){
        int nsum = 0;
         s = s.toLowerCase();

        for(int j=0; j<s.length(); j++){
            nsum += (s.charAt(j) - 'a' + 1);

        }

        return nsum;


    }


    public static void main(String[] args) {

          try{
        BufferedReader br1 = new BufferedReader(new FileReader("austen.txt"));
              String line = null;
              while((line = br1.readLine()) != null){
                  String [] ss = line.split("\\s+");
                  for(String s0: ss){
                    if(getWordValue(s0) == 100)System.out.println(s0);

                  }



              }
              br1.close();


          }catch(Exception ex){

              ex.printStackTrace();
          }

}
}        

Open in new window


that's waht it found
Output:

variety
applying
excellent
towards

Open in new window

austen.txt
0
 
for_yanCommented:
If i take the whole novel I'll probably find many dozens of such words
0
 
SarahDengAuthor Commented:
That is a value point. Unless you can check each word against dictionary. I take this out of the requirement. I'm thinking if there is a way JUST come up a combination of letters ( that are not a word at all) that is worth 100 point.
0
 
for_yanCommented:
This would find one word:
public class FindWord {
  public static int getWordValue(String s){
        int nsum = 0;
         s = s.toLowerCase();

        for(int j=0; j<s.length(); j++){
            nsum += (s.charAt(j) - 'a' + 1);

        }

        return nsum;


    }

    public static String get100Words() {
          String alph = "abcdefghijklmnopqrstuvwxyz";
        Random rr = new Random();
        String sum = "";

        while(true){
            sum += alph.charAt(rr.nextInt(26));
            int tot = getWordValue(sum);
           if(tot == 100)return sum;
            else if  (tot > 100){
               sum = "";continue;
           }

        }




    }



    public static void main(String[] args) {

        System.out.println(get100Words());
}

}

Open in new window


qhraexnkb

Open in new window



0
 
for_yanCommented:

This would find as many words as you require:
public class FindmanyWords {
    public static int getWordValue(String s){
        int nsum = 0;
         s = s.toLowerCase();

        for(int j=0; j<s.length(); j++){
            nsum += (s.charAt(j) - 'a' + 1);

        }

        return nsum;


    }

    public static ArrayList <String> get100Words(int num) {
        ArrayList<String> ar = new ArrayList<String>();
          String alph = "abcdefghijklmnopqrstuvwxyz";
        Random rr = new Random();
        String sum = "";

        while(true){
            sum += alph.charAt(rr.nextInt(26));
            int tot = getWordValue(sum);
           if(tot == 100){ar.add(sum); if (ar.size() == num) return ar;}
            else if  (tot > 100){
               sum = "";continue;
           }

        }




    }



    public static void main(String[] args) {

        System.out.println(get100Words(10));
}
}

Open in new window




[iayxcgmdn, poxigxe, dimcegeyoie, jkjfqvcu, pyunpce, ydhsgjrah, ndwgetlchd, fkgxkgdwg, ccesuedpx, rboiycrj]

Open in new window

0
 
for_yanCommented:

I think this should uincrease efficiency of the method:

 public static ArrayList <String> get100Words(int num) {
        ArrayList<String> ar = new ArrayList<String>();
          String alph = "abcdefghijklmnopqrstuvwxyz";
        Random rr = new Random();
        String sum = "";
             int tot = 0;
        while(true){

            if(tot > 75 && tot <100) sum += alph.charAt(rr.nextInt(Math.min(100-tot,26)));
            else  sum += alph.charAt(rr.nextInt(26));

             tot = getWordValue(sum);
           if(tot == 100){ar.add(sum); if (ar.size() == num) return ar;}
            else if  (tot > 100){
               sum = ""; tot = 0; continue;
           }

        }




    }

Open in new window

0
 
for_yanCommented:

Actually this last change dramatically increased
the efficiency - as in the original mmethod more than 100 words were rejected inthe prcess;
whereas with this enhanced selection - only 9 were rejected becuase of > 100 (see output below)
public class FindManyWords{
  public static int getWordValue(String s){
        int nsum = 0;
         s = s.toLowerCase();

        for(int j=0; j<s.length(); j++){
            nsum += (s.charAt(j) - 'a' + 1);

        }

        return nsum;


    }

    static int count;

    public static ArrayList <String> get100Words(int num) {
        ArrayList<String> ar = new ArrayList<String>();
          String alph = "abcdefghijklmnopqrstuvwxyz";
        Random rr = new Random();
        String sum = "";
             int tot = 0;
        while(true){

            if(tot > 75 && tot <100) sum += alph.charAt(rr.nextInt(Math.min(100-tot,26)));
            else  sum += alph.charAt(rr.nextInt(26));


            // sum += alph.charAt(rr.nextInt(26));

             tot = getWordValue(sum);
           if(tot == 100){ar.add(sum); if (ar.size() == num) return ar;}
            else if  (tot > 100){
               count++;
               sum = ""; tot = 0; continue;
           }

        }




    }



    public static void main(String[] args) {

        System.out.println(get100Words(10) + " total tried: " + count);

}

}

Open in new window


[nhcppxhhba, xcnpclbseaa, wacknsaragaa, ggyumllc, zfcensudb, vfnzrjaba, vjmkrsg, hfkbcwzjib, ivvhqffia, czrcsitaa] total rejected: 9

Open in new window

0
 
SarahDengAuthor Commented:
You are quick.

 the logic should mimic the structure of dictionary. for each letter ( starting from letter A), all the 26 letters should have the chance as the second letter after the first letter. and so on. Just like the dictionary way to come up the word. Does this make sense?
If this doesn't, I take what you have for now.
0
 
for_yanCommented:
I think you can get eventaually as sophisticated as you want,
But if you don't mind the words which have same letter several times, as in real life, then you select first letter, and then
second letter can also be any one of 26 and then again, until you reach the sum of higher than 74 - at that point you want to limit
yourself only to those letters which are less than what is remained. And that is what I tried to implement in the last version.
I am not quite understanding why it still rejects some with sum more than 100 in such situation, but no matter where i choose the
border 73, 74 or 75 - it returns 9 rejected words - something I'm missing there probably,
but otherwise I think it works - and of course it works very quickly.

You can also analyze the text (like Jane Austen) and find distribution of word values and compare say Hemingway with Dickens
0
 
for_yanCommented:
These are 687 words worth 100 out of list of 58000 english words
from http://www.mieliestronk.com/wordlist.html

By the way, check one or two words manually to make sure program works correctly.
corncob-lowercase.txt
100-words.txt
0
 
CEHJCommented:
SarahDeng, where is the code you have written? So far, i've seen only one person doing your class assignment, and that person's not you ...
0
 
SarahDengAuthor Commented:
Thanks, for_yan. You are a solid programmer. Now I learn the approach for_yan has demostrated here. Again, this is not a class assignment of any sort. Just a number game kids playing. It made me wonder if this can be solved by a program. I take what for_yan has accomplished and moved it forward. It is a good way to learn program. That is all about.
0
 
SarahDengAuthor Commented:
Excellent!
0
 
for_yanCommented:
You are always welcome.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 13
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now