cornercorner
FeaturesPluginsDocs & SupportCommunityPartners

Welcome

Congratulations! You've just landed a new job at Ye Olde Digital Toy Shoppe, a firm that produces entertaining and educational Java applications for kids. Along with your new computer you've been given the company's IDE: NetBeans.

Task 1:

Feel free to spend a few moments looking through the IDE. Please remember to think aloud while you browse.

Task 2:

Time to get going. Please create and run a simple "Hello World" application. The end result should be a "Hello World" message printed to the console.

Task 3:

Your manager sent you mail that you'll be working on part of an Anagram game. The changes you need to make are with an application already set up by another team member. You will find the project file here:

c:\anagrams\anagram-lib\src

It is a library used for an Anagram game.

Open the project and make sure that it compiles without any errors.

Task 4:

Now you need to make changes to the code. The wordlibrary class has scrambled and unscrambled words hard coded into it. Your manager left you a list of changes she'd like made to the library:

  1. Create a text file of unscrambled words -use the words from the class.
  2. Have the word list library read in the text file and scramble the words. To read in the file use this code:
        private void readList() throws IOException {
            URL url = getClass().getResource("wordlist.txt");
            if (url != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
                String line;
                ArrayList words = new ArrayList();
                ArrayList scrambled = new ArrayList();
    
                while (null != (line = br.readLine())) {
                    words.add(line);
                    scrambled.add(scramble(line));
                }
    
                wordList = (String []) words.toArray(new String [words.size()]);
                scrambledWordList = (String []) scrambled.toArray(new String [scrambled.size()]);
    
                br.close();
            } else {
                throw new IOException("File not found!");
            }
        }	
    	

    To scramble the words use:

        private String scramble(String word) {
            int size = word.length();
            StringBuffer buffer = new StringBuffer();
            
            for (int i = 0; i + 1 < size; i += 2) {
                buffer.append(word.charAt(i + 1));
                buffer.append(word.charAt(i));
            }
    
            if (buffer.length() < word.length()) {
                buffer.append(word.charAt(word.length() - 1));
            }
            
            return buffer.toString();
        }	
    
    Finally, you will need to add this line to the constructor:

        readList();

  3. Create a new package called test and add a main class to it that prints the scrambled and unscrambed word pairs to help you easily test that the new library reads in the list of words and scrambles them.
        WordLibrary lib = new WordLibrary();
       
        for (int i = 0; i < lib.getSize(); i++) {
            System.out.println(lib.getWord(i) + " -> " + lib.getScrambledWord(i));
        }
    
    Run the class when finished.

Task 5:

You realize that it would be nice to have another type of test to interactively run an Anagram game in the consol -this will let you more quickly verify that it is working the way you want. In the test class, add an argument "-interactive" that will run a consol Anagram game. Use this code:

    if (Arrays.asList(args).contains("-interactive")) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int wordIdx = 0;
        String line;
        WordLibrary lib = new WordLibrary();
        
        while (true) {
            System.out.println("Scrambled word: " + lib.getScrambledWord(wordIdx));
            System.out.print("Your guess (type 'end' + ENTER to quit, ENTER for next word):");
            
            line = br.readLine();
            
            if (line == null || line.equals("end")) {
                break;
            } else if (line.length() != 0) {
                if (lib.isCorrect(wordIdx, line)) {
                    System.out.println("Correct!");
                    wordIdx = (wordIdx + 1) % lib.getSize();
                } else {
                    System.out.println("Incorrect!");
                }
            } else {
                wordIdx = (wordIdx + 1) % lib.getSize();
            }
            
            System.out.println("-----------------------------------------------");
        }
        
        br.close();
    } else {
        WordLibrary lib = new WordLibrary();

        for (int i = 0; i < lib.getSize(); i++) {
            System.out.println(lib.getWord(i) + " -> " + lib.getScrambledWord(i));
        }
    }

Now you'll need to execute the test class passing it the '-interactive' argurment.

Task 6:

You ask your manager where the UI for the Anagram game is because you would like to test your library. Create a new project for the Anagram game UI. The sources can be found here:

c:\anagram\anagram-ui\src

You will not need to make any changes to the UI code.

You notice that the Anagram game depends on having the most up to date wordlibrary possible. You'll want to make sure that everytime you build your Anagram game the wordlibrary is automatically built as well.

Run the code to see that everything is working.

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems