Question: Please help me write a Java class SelfAware that implements the interface Language. Write an implementation of the occurrences() method that counts the occurrences of
Please help me write a Java class SelfAware that implements the interface Language.
Write an implementation of the occurrences() method that counts the occurrences of all Java keywords in a source file. Also, write an implementation of the append() method that can append a String to an existing file.
Count the number of Java-Keyword occurrences in your source file and append a String like this " //Keyword occurrences: " + sa.occurrences(code) to your source file.
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
public interface Language {
// Java reserved words are keywords that are reserved by Java
functions or other uses that cannot be used
// as identifiers (e.g., variable names, function names, class names).
String[] ReservedWords = {"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"default",
"do",
"double",
"else",
"enum",
"extends",
"false",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"void",
"volatile",
"while",
"continue"};
// sorts the ReservedWords string array, longest reserved word 1st.
static void sort() {
Arrays.sort(ReservedWords, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o2.length() - o1.length();
}
});
}
/**
* Counts the total number of occurrences of all Java keyword in the
file.
*
* @param sourceFile {@link String} path to a java source file
* @return {@link int} number of times java keyword occur in the
source file.
* @throws Exception not a java file or no file maybe ...
*/
int occurrences(String sourceFile) throws Exception;
/**
* Appends the provided file with the provided message
*
* @param sourceFile {@link String} path to a java source file
* @param message {@link String} message to be appended
* @throws IOException things didn't go too well ...
*/
void append(String sourceFile, String message) throws IOException;
}
********NOT SURE IF BELOW CODE IS NEEDED*********
public class SequenceSearchImpl extends SequenceSearch {
public SequenceSearchImpl(final String content, final String start,
final String end) {
super(content, start, end);
}
@Override
public String[] getAllTaggedSequences() {
String[] sa = new String[0];
int x0;
int x1 = -endTag.length(); // we have already processed the
sequence all the way to x1
while (-1 != (x0 = content.indexOf(startTag, x1 +
endTag.length()))) { // x0 : location of 1st letter of a startTag
x1 = content.indexOf(endTag, x0 + startTag.length()); // x1 :
location of 1st letter of an endTag
sa = adds(sa, content.substring(x0 + startTag.length(), x1));
}
return sa;
}
@Override
public String getLongestTaggedSequence() {
final String[] sa = getAllTaggedSequences();
int k = 0;
for (int i = 1; i < sa.length; i++) {
if (sa[k].length() <= sa[i].length()) {
k = i;
}
}
return sa.length > 0 ? sa[k] : null;
}
@Override
public String displayStringArray() {
String result = "";
for (final String s : getAllTaggedSequences()) {
result += String.format("%s : %d ", s, s.length());
}
return result;
}
/**
* @return {@link String} the content string with the all tags
removed.
*/
@Override
public String toString() {
return content.replace(startTag, "").replace(endTag, "");
}
}
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class HowMany{
private static char x = 'z';
public static void main(String[] args) throws Exception{
final String code = System.getProperty("user.dir") + File.separator +
"src" + File.separator + "main" + File.separator + "java" + File.separator +
HowMany.class.getName().replace(".", File.separator) + ".java";
final HowMany hm = new HowMany();
hm.append(code, String.format(" //Character %s was found %d times", x, + hm.occurrences(code,x)));
}
public void append(final String sourceFile, final String message) throws IOException{
Files.write(Paths.get(sourceFile), message.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
}
public int occurrences(final String sourceFile, final char x) throws Exception {
final String s = new String(Files.readAllBytes(Paths.get(sourceFile)));
final String[] sa = s.split("\\b");
for (String a : sa) {
if (0<(a=a.trim()).length()) {
System.out.println(a);
}
}
int n = 0; // occurrences counter
int k=-1; // progressed through k
while(-1 != (k=s.indexOf(x,k+1))) {
n++;
}
return n;
}
}
//Character z was found 1 times
//Character z was found 2 times
//Character z was found 3 times
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
