Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You MUST use this templet to solve the Java question below... Files to test the

ID: 3770378 • Letter: Y

Question

You MUST use this templet to solve the Java question below...

Files to test the run can be located here:

http://unixlab.sfsu.edu/~lmillik/P10/words6752

http://unixlab.sfsu.edu/~lmillik/P10/words10683

There is one part in this programming project. You may work by yourself, or in groups of two. Both partners in each group are expected to contribute equally to each part, and will get equal scores.

To make telephone numbers easier to remember, we usually map each digit to a letter of the alphabet, with a choice of 3 or 4 letters:

            2          a, b, c

            3          d, e, f

            4          g, h, i

            5          j, k, l

            6          m, n, o

            7          p, q, r, s

            8          t, u, v

            9          w, x, y, z

For example, 2232327 spells “bad bear”.

For this project, we’ll make these simplifying restrictions when we map a 7-digit telephone number to words: the first word always has three letters, the second word always has four letters, and there are no 0’s or 1’s in the telephone number.

Obviously, the “words” are only interesting if they are real words, i.e., they can be found in a dictionary. For this project, your code will read a dictionary file, and match its entries with a telephone number.

You will need to set up String arrays to store words from the dictionary file. See Chapter 9 Slide 13 for arrays of Strings.

The project is divided into two parts, to help you organize your work. You must follow the design and the instructions laid out in the handout, or points will be deducted. If you prefer to use a different design or organization for your program, you must first get permission from the instructor.

Part a: due 5pm Monday 5/14/2012

(40/100 correctness + 10/100 documentation)

Solution will be posted Monday night, no late submissions

What your program should do:

            Prompt the user to enter the name of the dictionary file to use (a text file)

            Set up two arrays of Strings, w3[] and w4[]

            Read all words from the file

                        store all 3-letter words from the file into w3[]

                        store all 4-letter words from the file into w4[]

            Write w3[] into a file called shorts3

            Write w4[] into a file called shorts4

You must create and fill the arrays w3[] and w4[] correctly; otherwise points will be deducted.

You will need to know how to read and write a text file; see Chapter 9 Slides 36-46 for details. (Hint: this is very similar to the exercise on Slide 46.) The dictionary file should be in the same directory as your executable. You should use the files on unixlab ~whsu/csc210/Projects/P10/words6752 or ~whsu/csc210/Projects/P10/words10683 as your dictionary files. (There’s also a very short file, ~whsu/csc210/Projects/P10/words5, for initial testing.) All these files are also accessible through your browser at

            http://userwww.sfsu.edu/~whsu/csc210/Projects/P10

A sample run:

unixlab% ls

ProcFile.class    words10683 words5      words6752

unixlab% cat words5

cat

intolerable

curd

daft

dog

unixlab% java ProcFile

Enter name of dictionary file: words5

unixlab% ls

procFile    shorts3     shorts4     words10683 words5      words6752

unixlab% cat shorts3

cat

dog

unixlab% cat shorts4

curd

daft

unixlab%

In more detail, suppose words5 is used to test ProcFile. ProcFile starts reading words5, line by line.

The first word read is cat. This is stored in w3[0].

The next word read is intolerable. This is not stored.

The next word read is dog. This is stored in w3[1].

The next word read is curd. This is stored in w4[0]. Finally, daft is read and stored in w4[1].

The contents of w3[] are written to the file shorts3. The contents of w4[] are written to the file shorts4.

Part b: due 5pm Friday 5/18/2012

(40/100 correctness + 10/100 documentation)

no late submissions

You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run:

unixlab% java MapNumbers

Enter name of dictionary file: words10683

Enter a test word (3 letters): cat

Test word maps to 228

Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273

Options for first 3 digits:

act

cat

bat

Options for next 4 digits:

base

card

care

case

bare

Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2232243

Options for first 3 digits:

bad

ace

Options for next 4 digits:

acid

cage

Enter telephone number (7 digits, no 0's or 1's, negative to quit): -1

unixlab%

More formally, your program should do this:

            Prompt the user for the name of a dictionary file

            Set up the PhoneMapper class and methods (see details below)

            Prompt the user to enter a 3-letter test word (mostly for testing early versions)

            Display the number that maps to that word

            Prompt the user to enter a 7-digit telephone number

                        (quit when negative number is read)

            Use the PhoneMapper class to map 3-letter and 4-letter words to the number

            Display all 3-letter words that map to the first 3 digits of the number

            Display all 4-letter words that map to the last 4 digits of the number

            Prompt the user to enter another 7-digit telephone number

                        (quit when negative number is read)

A skeleton program is provided on unixlab in

~whsu/csc210/Projects/P10/MapNumbersSkeleton.java or

http://userwww.sfsu.edu/~whsu/csc210/Projects/P10/MapNumbersSkeleton.java

Let’s look at the skeleton file. Note the PhoneMapper class that does most of the useful work. The main method of the public class prompts the user for the name of the dictionary file, and telephone numbers. A PhoneMapper object myMap3 (for 3-digit numbers) is created. For each telephone number entered by the user, the method myMap3.findWords() is called; it returns an array of Strings, with all the words that map to the first three digits of the number.

Right now, the PhoneMapper class and methods don’t do very much. Follow the steps to fill out the design. If you wish to make significant changes to the overall program, you must check with the instructor first; otherwise points will be deducted.

Step 1: Fill in the constructor for the PhoneMapper class. There’s only one constructor:

PhoneMapper(String fileName, int wordLength) throws Exception

filename is the name of the dictionary file to be used, wordLength is the length of the words to be extracted from the dictionary file. All words of length wordLength are added to the wordList[] array of Strings. (Note: this part uses code from Part A!)

An example: suppose we instantiate a PhoneMapper object:

     PhoneMapper smallMap = new PhoneMapper(“words5”, 3);

words5 is the short test file we looked at in Part A. After the PhoneMapper constructor returns, smallMap.wordList[0] = “cat”, smallMap.wordList[1] = “dog”, and smallMap.numWords = 2.

Step 2: Fill in the method findTelNum():

int findTelNum(String inStr)

findTelNum() maps each character of inStr to a numeric digit according to the rule described earlier for telephone numbers, assembles the digits into a 3 or 4-digit integer, and returns the integer corresponding to the string.

An example:

     String str = “bad”;

     int x = findTelNum(str); // x = 223

Note that there’s some code in the main method of the public class for testing findTelNum().

Step 3: Fill in the method findWords():

     String [] findWords(int num)

num is the 3 or 4-digit integer from the telephone number. findWords() checks every word in wordList[], by calling findTelNum(); if a word in wordList[] maps to num, that word is added to a list of mapped words. After all words in wordList[] that map to num have been copied to the list of mapped words, the list of mapped words is returned. Make sure the array returned has the correct length. (Hint: add the mapped words to a very large array first. After you are done adding mapped words, copy the very large array to a smaller array, with the correct length.)

An example: suppose we use words10683 as the dictionary file. The number 223 maps to two words in the file: bad and ace. When we run these lines of code:

     PhoneMapper bigMap = new PhoneMapper(“words10683”, 3);

     String [] s = bigMap.findWords(223);

s[0] = “bad”, s[1] = “ace”, and s.length = 2.

Step 4: Fill in the rest of the main loop in the main method in the public class, to find and report the 4-letter words that map to the last 4 digits of the telephone number entered by the user.

Submission: Submit your programs (the .java files only) using the iLearn submission links.

Explanation / Answer

What your program should do:            Prompt the user to enter the name of the dictionary file to use (a text file)

            Set up two arrays of Strings, w3[] and w4[]

            Read all words from the file

                        store all 3-letter words from the file into w3[]

                        store all 4-letter words from the file into w4[]

            Write w3[] into a file called shorts3

            Write w4[] into a file called shorts4

You must create and fill the arrays w3[] and w4[] correctly; otherwise points will be deducted.


import java.util.Scanner;
public class MapNumbersSkeleton {
    public static void main(String [] args) throws Exception {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter name of dictionary file: ");
        String fName = input.nextLine();

  //Creating files short3 and short4
        PhoneMapper myMap3 = new PhoneMapper(fName, 3);
  String [] w3=myMap3.getArray();

        PhoneMapper myMap3 = new PhoneMapper(fName, 4);
  String[] w4=myMap3.getArray();
  
  FileWriter fileWriter =
                new FileWriter("short3");

            BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);
  for(int i=0;i<w3.length;i++)
  {
  bufferedWriter.write(w3[0]);
  }

   FileWriter fileWriter =
                new FileWriter("short4");

            BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);
  for(int i=0;i<w3.length;i++)
  {
  bufferedWriter.write(w4[0]);
  }

        System.out.print("Enter a test word (3 letters): ");
        String s = input.nextLine();
        int testNum = myMap3.findTelNum(s);
        System.out.println("Test word maps to " + testNum);
  

        System.out.print("Enter telephone number ");
        System.out.print("(7 digits, no 0's or 1's, negative to quit): ");

        int telNum = input.nextInt();
        while (telNum >= 0) { // process each non-negative number
            int firstPart = telNum / 10000;
           
            String [] results = myMap3.findWords(firstPart);

            System.out.println("Options for first 3 digits:");
            for (int i=0; i<results.length; i++)="" {="" system.out.println(results[i]);="" }="" extract="" last="" 4="" digits="" get="" words="" for="" and="" display="" prompt="" user="" phone="" number="" system.out.print("enter="" telephone="" ");="" system.out.print("(7="" digits,="" no="" 0's="" or="" 1's,="" negative="" to="" quit):="" telnum="input.nextInt();" end="" while="" (telnum="">=0)

    }
}

class PhoneMapper {
    final int MAXWORDS = 20000; // max number of words from dictionary
    String [] wordList = new String [MAXWORDS]; // list of dictionary words
    int numWords = 0; // number of words of correct length
                      // extracted from dictionary
int cnt=0;

    PhoneMapper(String fileName, int wordLength) throws Exception {

FileReader fileReader =
                new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader =
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
    if(line.length == wordLength)
     {
     wordList[cnt]=line;
     cnt++;
     }
    }
    }
public String[] getArray()
{
return wordList;
}

    // return array of words mapped to num
    String [] findWords(int num) {
        String [] nString = new String[0];

        return nString;
    }

    // returns telephone number that inStr maps to
    int findTelNum(String inStr)
    {
int x=0;
  for(int i=0;i<inStr.length;i++)
  {
   if(inStr[i].contains("a")||(inStr[i].contains("b")||inStr[i].contains("c"))
   {
    x=x+2;
    x=x*10;
   }
   else if(inStr[i].contains("d")||(inStr[i].contains("e")||inStr[i].contains("f"))
   {
    x=x+3;
    x=x*10;
   }
   else if(inStr[i].contains("g")||(inStr[i].contains("h")||inStr[i].contains("i"))
   {
    x=x+4;
    x=x*10;
   }
   else if(inStr[i].contains("j")||(inStr[i].contains("k")||inStr[i].contains("l"))
   {
    x=x+5;
    x=x*10;
   }
   else if(inStr[i].contains("m")||(inStr[i].contains("n")||inStr[i].contains("o"))
   {
    x=x+6;
    x=x*10;
   }
   else if(inStr[i].contains("p")||(inStr[i].contains("q")||inStr[i].contains("r")|| inStr.contains("s"))
   {
    x=x+7;
    x=x*10;
   }
   else if(inStr[i].contains("t")||(inStr[i].contains("u")||inStr[i].contains("v"))
   {
    x=x+8;
    x=x*10;
   }
   else if(inStr[i].contains("w")||(inStr[i].contains("x")||inStr[i].contains("y")|| inStr.contains("z"))
   {
    x=x+9;
    x=x*10;
   }
  }
        return x;
    }

}