我需要计算对象或函数的LOC。也就是说,对于java,每个对象中的方法数量。
其他要求是计算java代码的物理LOC和逻辑LOC,我已经完成了。
我的代码正在使用bufferreader读取.txt文件并计算代码。
我计算了物理,逻辑,空白和注释行。现在我需要计算方法和对象loc。
这样的预期输出;
https://s8.postimg.org/cidz14y1h/Capture.png
对象名称#of Methods Object LOC XXX 5 55 YYY 1 9 ZZZ 3 14
你可以帮忙解决这个问题吗?
public class HW2 {
public static void main(String[] args) {
String lineToRead = ""; //will be used to read the lines from .txt file
int totalNumberofCommentLines = 0; //will be used to count comment lines
int totalNumberofBlankLines = 0; //will be used to count blank lines
int totalNumberofLines = 0; //will be used to count all lines
try {
BufferedReader br = new BufferedReader(new FileReader("F:\\HW-1\\4.txt"));
try {
while ((lineToRead = br.readLine()) != null) {
totalNumberofLines++;
if (lineToRead.trim().isEmpty()) {
totalNumberofBlankLines++;
}
if (lineToRead.startsWith("//")) { //if the line starts with single line comment
totalNumberofCommentLines++; //if yes, count it
} else if (lineToRead.startsWith("/*")) { //if the line starts with multiple line comment
totalNumberofCommentLines++; //if yes, look at the content of it
do {
if (lineToRead.trim().isEmpty()) { //if there is a blank line, count it as blank
totalNumberofBlankLines++;
}
else
{ totalNumberofCommentLines++; //else, count it as comment line
totalNumberofLines++;
}
} while (!(lineToRead = br.readLine()).endsWith("*/")); //count until see multiple comment line closure
}
}
} catch (IOException ex) {
Logger.getLogger(HW2.class.getName()).log(Level.SEVERE, null, ex);
}
br.close(); //close the buffer reader to empty ram area
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Total Number of Physical Lines " + totalNumberofLines);
System.out.println("Total Number of Comment Lines " + totalNumberofCommentLines);
System.out.println("Total Number of Blank Lines " + (totalNumberofBlankLines));
System.out.println("Total Number of Logical Lines " + (totalNumberofLines - ((totalNumberofBlankLines) + totalNumberofCommentLines)));
}
}