A java question. Write an application IndexMaker which reads a simplified Java s
ID: 3835932 • Letter: A
Question
A java question. Write an application IndexMaker which reads a simplified Java source file and produces an index containing identifiers . Print all the identifiers and all the line numbers on which each identifier occurs. But do not print reserved keywords. When you find an identifier, compare it against a list of reserved keywords and skip keywords. Be smart about this and do not use 50 if statements. Put the keywords in a data structure and check each identifier to see if it is in the collection. The file of reserved keywords is as follow. Download it and put in your project.
---------------------------------------------------------------------------
Here is a file of reserved keywords:
reserved.txt
---------------------------------------------------------------------------
Print the identifiers in lexicographical order. Display the line numbers in numeric order.
We will assume:
1. every string consisting of only letters, numbers, and underscores is either an identifier or reserved keyword. But we are ignoring keywords
2. there are no comments to confuse things
You will need to set the delimiter: ...useDelimiter("[^A-Za-z_]+"); For simplicity, there will not be any identifiers that contain numbers.
Your output should be in this format:
ProcessRectangle: [4]
Rectangle: [2, 9, 11]
String: [7]
System: [13, 16]
args: [7]
You can simply declare that the main method throws the FileNotFoundException. You do not need to catch it.
Use this line in your code for the file name
String filename = "ProcessRectangle.java"; //SUB "PaintJobCalculator.java"
Call the reserved keyword file reserved.txt
Please show the full code, Thank you!
-------------------------------------------------------------------------------------------------
Here are the Java files that are used: ProcessRectangle.java and PaintJobCalculator.java. CodeCheck will process both files.
PaintJobCalculator.java
ProcessRectangle.java
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class IndexMaker {
public static String KEYWORDFILENAME = ""; // provide your keywords file path
public static String JAVAFILEPATH = ""; // provide your java filepath
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader bufferedReader = null;
BufferedReader filebufferedReader = null;
String currentLine = "";
int lineNo = 0;
List<String> keyWordsList = new ArrayList();
/* As treeMap is sorted, here we use treeMap for lexicographical order.*/
TreeMap<String, String> identifierIndexes = new TreeMap<String, String>();
bufferedReader = new BufferedReader(new FileReader(KEYWORDFILENAME));
filebufferedReader = new BufferedReader(new FileReader(JAVAFILEPATH));
while ((currentLine = bufferedReader.readLine()) != null) {
keyWordsList.add(currentLine);
}
while ((currentLine = filebufferedReader.readLine()) != null) {
lineNo++;
String[] split = currentLine.split("[^A-Za-z_]+");
for (int i = 0; i < split.length; i++) {
if (!"".equalsIgnoreCase(split[i]) && !keyWordsList.contains(split[i])) {
if (identifierIndexes.containsKey(split[i])) {
identifierIndexes.put(split[i], identifierIndexes.get(split[i]) + lineNo + ",");
} else {
identifierIndexes.put(split[i], lineNo + ",");
}
}
}
}
identifierIndexes.keySet().stream().forEach((String name) -> { // lamda expressions for iterating treeMap
String identifier = name;
String temp = identifierIndexes.get(name);
String occrrences = "[" + temp.substring(0, temp.lastIndexOf(",")) + "]";
System.out.println(identifier + " : " + occrrences);
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.