Question

In: Computer Science

Write a program that reads a Java source file and produces an index of all identifiers...

Write a program that reads a Java source file and produces an index of all identifiers in the file. For each identifier, print all lines in which it occurs. For simplicity, we will consider each string consisting only of letters, numbers, and underscores an identifer. Declare a Scanner in for reading from the source file and call in.useDelimiter("[^AZa-z0-9_]+"). Then each call to next returns an identifier.

Java. Explain logic used lease.

Solutions

Expert Solution

IdentifierIndex.java

/**
A class to read in a Java source file and
produce an index of all identifiers in the file.
*/

. . .

public class IdentifierIndex
{
. . .

/**
Reads all identifiers from the given file
@param filename the file name
*/
public void read(String filename) throws FileNotFoundException
{
. . .
}

/**
A set of all identifiers that occur in the file, in sorted order.
*/
public Set<String> getIdentifiers()
{
. . .
}

/**
Gets all line numbers on which the given identifier occurs.
@param identifier an identifier
@return all line numbers on which the identifier was found, in increasing
order; an empty set (not null) if the identifier was never found
*/
public Set<Integer> getLines(String identifier)
{
. . .
}
}

IndexDemo.java

import java.io.FileNotFoundException;
import java.util.Set;

/**
   A program to read in a Java source file and
   produce an index of all identifiers in the file.
*/
public class IndexDemo
{  
   public static void main(String[] args)
      throws FileNotFoundException
   {  
      IdentifierIndex index = new IdentifierIndex();
      index.read("IndexDemo.java"); // reads this file
      Set<String> idents = index.getIdentifiers();
      for (String ident : idents)
      {
         Set<Integer> lines = index.getLines(ident);
         System.out.println(ident + ": " + lines);
      }
   }
}

IndexTester.java

import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Set;

public class IndexTester
{  
   public static void main(String[] args)
      throws FileNotFoundException
   {  
      IdentifierIndex index = new IdentifierIndex();
      index.read("IndexTester.java"); // reads this file
      Set<String> idents = index.getIdentifiers();
      String last = "";
      for (String s : idents) { last = s; }
      System.out.println(last); // The last identifier
      System.out.println("Expected: void");
      Set<Integer> lines = index.getLines("void");
      System.out.println(lines);
      System.out.println("Expected: [7, 16, 17]");
      System.out.println(index.getLines("f" + "oo"));
      System.out.println("Expected: []");
   }
}

Related Solutions

Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
In java, Write a program that reads a data file containing final exam scores for a...
In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined. The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT