Question: You are to write a Java program to read a file name from a command line argument (Use any text file). Its a plain text

  1. You are to write a Java program to read a file name from a command line argument (Use any text file). Its a plain text file that contains lines separated by new line character.
  2. Write an Indexer class which does the following:
    1. Reads a file passed to it in the constructor
    2. Reads each line and splits it into words
    3. Adds each word into a list
    4. For each word found in the file, adds the line number that the word was found to the list of line numbers that are maintained by that word
    5. For example, from Romeo-full.txt, its output for the words capulet and orchard, are as follows:
      • capulet: [4, 58]
      • orchard: [4, 111]
      • in the first example, it tells you that the word capulet occurs on lines 4 and 58
      • and the word orchard occurs on lines 4, and 111
    6. Generates the output above for all the words in the file
    7. Provides a find functionality that searches the index (list of words) that it generates and returns an on output like in 2.5 above, e.g.
      • Find(PEACE) or find(peace) will give: [385, 386] which tells that the word peace occurs on lines 385 & 386.
  3. You need to create two major classes and a test driver program (test harness) to test your methods and classes:
    1. public class Indexer{}
    2. data members:
      • private List index; // index structure to maintain a list of words read from the file
      • private String fn; // file name to read from
    3. methods:
      • public Indexer(String filename) // constructor to store a file name
        • the constructor needs also to initialize the index data member
      • public Boolean index()
        • calls the private method indexFile()
        • returns true (for now)
      • private void lineSplitter(String line, int lineno)
        • line is the line you pass to this method
        • lineno is the line number of this line
        • it will split each line as follows: line.split("\\W+")this is a basic word splitter, you dont need more than this.
        • for each word in the generated array (see java online docs for how split() method works in the String class) create a new Word (see Word class description below), checks the index and if the word is not in the index, add lineno to the list of line numbers maintained in the Word, and then add the word constructed to the index
        • if the word is in the index, you need to get the word and update its line number list by adding lineno.
      • private indexfile()
        • opens file and read one line at a time
        • skips lines with 0-length (a new line character on its own in a text file)
        • calls the lineSplitte(line, above)
        • keeps track of linenoYOU NEED TO START numbering from 1 and not 0.
        • Make sure to capture any exceptions (check text in appendix A.10 for examples)
      • public void dumpList()
        • generates the output required (see sample output below)
      • public List find(String w)searches the index and generates the an out as in the sample output below or nothing if word is not in the index.

  1. public class Word{}
  2. data members:
    • private String w; // the actual word
    • private List lines; // maintain a list of line numbers that the word occurs. If a word occurs multiple times on the same line, make sure you only store the lineno once, i.e. no duplicates.
  3. Methods:
    • public Word(String word)
      • constructor that stores the word. Make sure to store words in lower case letter, otherwise The and the will be stored as two different words.
      • Initialize lines list
    • public boolean addLine(int lineno)
      • stores the lineno of the word occurrence in the listMake sure no duplicates are allowed.
      • Return true (for now)
    • Setters and getters
      • public String getWord()
      • public List getLines()
    • you need to override the equals(object o) methodotherwise, when you search for words in the index will not work.
  4. public class PA2 {}
    • this is your test driver program that will pass the file name user provides to the Index class:

public class PA2 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Indexer idx = new Indexer(args[0]);

boolean rc = idx.index();

idx.dumpList();

// test find

System.out.println("*****************");

List x = idx.find("PEACE");

if ( x!= null)

System.out.println(x);

}

}

  1. Sample Output

romeo: [1, 6, 8, 42, 55, 60, 73, 75, 79, 83, 90, 102, 104, 115, 126, 136, 147, 165, 180, 191, 202, 218, 226, 235, 254, 264, 290, 299, 311, 316, 318, 326, 328, 337, 346, 355, 369, 383]

and: [1, 14, 15, 16, 19, 20, 33, 51, 56, 58, 76, 82, 102, 110, 111, 112, 119, 130, 139, 150, 163, 168, 171, 177, 200, 215, 233, 241, 242, 264, 268, 269, 270, 286, 287, 313, 315, 357, 363, 366, 386, 388]

juliet: [1, 11, 14, 38, 53, 64, 85, 98, 108, 122, 132, 143, 155, 185, 195, 206, 222, 230, 239, 260, 262, 276, 284, 294, 307, 309, 324, 332, 341, 350, 360, 373]

act: [2]

2: [2]

scene: [2, 4]

ii: [4]

capulet: [4, 58]

s: [4, 68, 71, 101, 117, 138, 176, 204, 213, 228, 311, 316, 363, 387]

orchard: [4, 111]

etc. ...

***************** output sample for find(peace)

[385, 386]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!