我正在写一个类,它读取Wikipedia文本并比较.txt文件中不同字符出现的频率。
为此,我创建了几个私有实例
public class Frequencies {
private double[] characterInstance = new double[26];
private String input;
private String name;
private double numberOfCharacters;
public Frequencies(String string)
{
name = string;
}
此外,我编写了一种方法来计算.txt文件中的字符并将其放入characterInstance数组中,并且该方法通过将characterInstance的元素除以字符总数来计算频率。
我的问题 我现在想写一个方法
public double sameOrNot(Frequencies different)
{
}
返回类我的频率中两个对象的字符频率之间的绝对差。也就是说,如果我初始化
Frequencies type1 = new Frequencies("type1") //And I give type1 some text to analyze
Frequencies type2 = new Frequencies("type2") //Also I give this some text to analyze
double result = type1.sameOrNot(type2)
我该如何编写方法sameOrNot,使其能够将新对象type2与旧对象type1进行比较?
答案 0 :(得分:0)
尝试一下(我没有尝试过,所以我不知道它是否有效;我什至没有尝试编译它)。
public double sameOrNot(Frequencies other)
{
// Initialize result
double result = 0.0;
// Loop over all characters
int counter;
double this_freq;
double other_freq;
for (counter = 0; counter < characterInstance.length; counter++)
{
// Determine frequency of this character in both collections
this_freq = this.characterInstance[counter] / this.numberOfCharacters;
other_freq = other.characterInstance[counter] / other.numberOfCharacters;
// Adjust final result
result += Math.abs(this_freq - other_freq);
} // for all characters
// Done
return (result);
} // sameOrNot