Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java program to make a word worth $1.00

Posted on 2011-10-24
20
Medium Priority
?
232 Views
Last Modified: 2012-05-12
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
Comment
Question by:SarahDeng
  • 13
  • 6
20 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37022149
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022153
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
 

Author Comment

by:SarahDeng
ID: 37022191
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Expert Comment

by:for_yan
ID: 37022204
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
 

Author Comment

by:SarahDeng
ID: 37022230
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022232
But the program cannot do it beacuse how would it know what is the real word - what is not?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37022240
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022248
If i take the whole novel I'll probably find many dozens of such words
0
 

Author Comment

by:SarahDeng
ID: 37022254
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022282
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022297

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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022331

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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37022367

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
 

Author Comment

by:SarahDeng
ID: 37022376
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022392
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022433
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
 
LVL 86

Expert Comment

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

Author Comment

by:SarahDeng
ID: 37024672
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
 

Author Closing Comment

by:SarahDeng
ID: 37024682
Excellent!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37024691
You are always welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

580 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