Question: Write a program that computes the count for each keyword used in a Java class. The program starts by prompting the user for a Java

Write a program that computes the count for each keyword used in a Java class. The program starts by prompting the user for a Java file to read, reads the file once given a file name, and prints keywords and their counts in the file. For the example below, the keywords class, static, and void each appear once and public appears twice.
public class Main { public static void main ( String [] args ){}}
To make this program easy, you can assume that there's a blank space between every token in the code. A token is a sequence of one or more characters without a space. Some tokens such as parenthesis or semicolon do not need spaces but for most tokens, spaces separated them. For example, this line of code, int x =30 ;, has space between int and x, between x and =, between = and 30, and between 30 and ;. You can also assume that there's a space between every dot (.). For example, System . out . println ( "hello" ) ; Java doesn't care about the spacing between tokens as long as you do not insert space into keywords and identifiers. Identifiers are names provided by the programmer and they include package name, class name, method name, labels, and variable names.
Code template
You are provided with a Java template with an array of sorted keywords.
The logic of the program can be something similar as below:
open the file to read using a Scanner
read one line at a time
split the line into tokens
for each token found, update the count, and append line number
closed the file when done
for each token with count >0, print the token, count, and lines.
Your solution should be around 50 lines.
String Comparison
You have seen binary search with int. Since int is a primitive type, you are able to compare using all the relational operators. Strings are objects and the only operator we can use is == which is true if two variables reference the same object. How do we compare the relative value of the strings to see if one is less or greater than the other? The String class provides a method String.compareTo(string) for comparing the relative ordering of two strings. String objects are compared using alphabetical ordering (the same way words are ordered in a dictionary). For example, "john" comes before "steven" in alphabetical ordering. The method returns an integer that describes the ordering of the two strings.
zero if the two strings are the same
less than zero if the first string comes before the second string in alphabetical ordering.
greater than zero if the first string comes after the second string in alphabetical ordering.
You can think of the return value of zero as two strings are equal (== or equals()), less than zero as less than () and greater than zero as greater than (>).
You can read more about the method here. Note that not all objects provide the compareTo() method and it's a decision made by the author of the class of the objects as some objects have no ordering. If you want to be able to sort the objects of a class you write, you must provide the compareTo() method using the Comparable interface. An interface describes what a class must provides in order for objects of that class to work. This will be covered in ICS 240.
Below is an example of how to compare two strings.
String s1= "abc", s2="bc";
int result = s1.compareTo(s2);
if (result ==0)
System.out.printf("%s ==%s
", s1, s2);
else if (result 0)
System.out.printf("%s %s
", s1, s2);
else
System.out.printf("%s >%s
", s1, s2);
String.split() method
You can use the split method of the String class to extract individual token from a string. The argument to the method is a pattern called regular expression. For our case, we split by one or more blanks. The method an array of strings resulted from the split. Below is an example that splits a line containing verbs into an array of verbs and then print each verb to the screen.
String line = "run jump walk";
//split by one or white spaces
String[] verbs= line.split("+");
for(String verb: verbs) System.out.println(verb);
Keyword count and line numbers.
You will need an int array where each entry represents the count for the keyword of the same index. You will also need an array of string to store the line numbers for each keyword. For example, count[index] would be the count for keywords[index]and lines[index] would be a string of number separated by commas where each number is a line number. What connects the count, lines and the keyword is the index. Below is an example of the content of the three arrays. The count for abstract is 0 and the count for the keyword while is 3. The while keyboard appears on line 4 and line 15.
Write a program that computes the count for each

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 Programming Questions!