A java question. Write an application IndexMaker which reads a simplified Java s
ID: 3836742 • 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
-------------------------------------------------------------------------------------------------
Here are the Java files that are used: ProcessRectangle.java and PaintJobCalculator.java. CodeCheck will process both files.
PaintJobCalculator.java
ProcessRectangle.java
and the output should be exactly the same as follow:
Running program with substitutions
Testing ProcessRectangle.java
Please show the full code and your output, Thank you!
filename "Process Rectangle. java "PaintJobCalculator. java.Explanation / Answer
IndexMaker.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeMap;
public class IndexMaker {
public static void main(String args[]) throws IOException {
// build keyword list
HashSet<String> reservedWords = new HashSet<>();
BufferedReader reader = new BufferedReader(new FileReader("reserved.txt"));
String line;
while((line = reader.readLine()) != null) {
// assuming keywords are separated by spaces
String tokens[] = line.split(" ");
for(String keyword: tokens) {
reservedWords.add(keyword);
}
}
reader.close();
String filename = "ProcessRectangle.java";
reader = new BufferedReader(new FileReader(filename));
TreeMap<String, ArrayList<Integer>> wordIndices = new TreeMap<>();
int lineNumber = 1;
while((line = reader.readLine()) != null) {
String tokens[] = line.split("[^A-Za-z_]+");
for(String word: tokens) {
if(!word.isEmpty() && !reservedWords.contains(word)) {
addWord(wordIndices, word, lineNumber);
}
}
lineNumber++;
}
reader.close();
// Now print the words
Iterator<String> wordIterator = wordIndices.keySet().iterator();
for(;wordIterator.hasNext(); ) {
String word = wordIterator.next();
System.out.println(word + ": " + wordIndices.get(word).toString());
}
}
public static void addWord(TreeMap<String, ArrayList<Integer>> wordIndices, String word, int lineNum) {
if(wordIndices.containsKey(word)) {
ArrayList<Integer> numList = wordIndices.get(word);
if(numList.indexOf(lineNum) == -1)
numList.add(lineNum);
} else {
ArrayList<Integer> numList = new ArrayList<>();
numList.add(lineNum);
wordIndices.put(word, numList);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.