ok here is my code in complete:
public class BenfordsLaw{
public static void main(String[] args){
System.out.print("Generating Benford Sequence with initial amount =" + 100);
System.out.print(", growth rate =" + .1);
System.out.println(", number of periods =" + 1000);
double[] testBenfordArray = generateBenfordNumbers(100,.1,1000);
double[] proportionTestBenfordArray = calculatingLeadingDigitProportions(testBenfordArra y);
for (int i = 0; i<10; i++) System.out.println("In calculating Leading Digit Proportions, digit " + i + " appears " + proportionTestBenfordArray[i] + " proportion of the time.");
double[] testRandomNumbersArray = BenfordSupportCode.generateRandomNumbers(1000);
double[] proportionTestRandomNumbersArray = calculatingLeadingDigitProportions(testRandomNumbe rsArray);
for (int i = 0; i<10; i++) System.out.println("In calculating Random Leading Digit Proportions, digit " + i + " appears " + proportionTestRandomNumbersArray[i] + " proportion of the time.");
double idealDistribution = calculateDistance(testBenfordArray, BenfordSupportCode.getBenfordProbabilities());
System.out.println("The ideal Benford distribution is" + idealDistribution);
double randomDistribution = calculateDistance(testRandomNumbersArray, BenfordSupportCode.getBenfordProbabilities());
System.out.println("The random distribution is" + randomDistribution);
}
public static double[] generateBenfordNumbers(double initialAmount, double growthRate, int numberPeriods){
double[] arrayBenford;
//creates number of spots in the array equal to value of numberPeriods
arrayBenford = new double[numberPeriods];
arrayBenford[0]=initialAmount;
//Adding values to the array
for(int i=1;i<numberPeriods;i++){
arrayBenford[i]=arrayBenford[i-1]*(1+growthRate);
}
return arrayBenford;
}
public static double[] calculatingLeadingDigitProportions(double[] numbers){
double[] calculatedArray;
//Creates 10 spots in the array
calculatedArray = new double[10];
for(int i=0;i<numbers.length;i++){
if((calculateLeadingDigit((int)numbers[i]))==0.0) calculatedArray[0]++;
if((calculateLeadingDigit((int)numbers[i]))==1.0) calculatedArray[1]++;
if((calculateLeadingDigit((int)numbers[i]))==2.0) calculatedArray[2]++;
if((calculateLeadingDigit((int)numbers[i]))==3.0) calculatedArray[3]++;
if((calculateLeadingDigit((int)numbers[i]))==4.0) calculatedArray[4]++;
if((calculateLeadingDigit((int)numbers[i]))==5.0) calculatedArray[5]++;
if((calculateLeadingDigit((int)numbers[i]))==6.0) calculatedArray[6]++;
if((calculateLeadingDigit((int)numbers[i]))==7.0) calculatedArray[7]++;
if((calculateLeadingDigit((int)numbers[i]))==8.0) calculatedArray[8]++;
if((calculateLeadingDigit((int)numbers[i]))==9.0) calculatedArray[9]++;
}
for(int i=0;i<calculatedArray.length;i++){
calculatedArray[i] = (calculatedArray[i]/numbers.length);
}
return calculatedArray;
}
public static int calculateLeadingDigit(int number){
if (number == 0)
return 0;
else{
double bigNumber = Math.abs(number);
while (bigNumber >= 10.0)
bigNumber = bigNumber/10.0;
int intBigNumber = (int) bigNumber; //cast bigNumber to be an int for the return statement
return intBigNumber;
}
}
public static double calculateDistance(double[] array1, double[] array2){
double euclideanDistance = 0.0;
for(int i=0;i<array1.length;i++){
euclideanDistance = euclideanDistance + Math.pow((array1[i]-array2[i]), 2);
}
euclideanDistance = Math.sqrt(euclideanDistance);
return euclideanDistance;
}
}
and another method is:
import java.util.Random;
public class BenfordSupportCode
{
//this method returns an array of random integers
public static double[] generateRandomNumbers(int count)
{
//Note FYI: if you want to get a different sequence each time, change the number 1
//below to something else. However, it will be easier to debug any issues you have
//if you leave this consistant as you can then reproduce your errors consistantly.
Random randGenerator = new Random(1);
double[] random = new double[count];
for (int i = 0; i < count; i++)
{
random[i] = randGenerator.nextInt(10000);
}
return random;
}
//this method returns an array of the benford probabilities
public static double[] getBenfordProbabilities()
{
double[] benfordDistribution = {0, .301, .176, .125, .097, .079, .067, .058, .051, .046};
return benfordDistribution;
}
}
When i attempt to run my code it runs a few things and then it hits the error: java.lang.ArrayIndexOutOfBoundsException: 10
at BenfordsLaw.calculateDistance(BenfordsLaw.java:100 )
at BenfordsLaw.main(BenfordsLaw.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
>
While I realize this is probably because i have not declared the array index for array2 i don't think it should matter, because for my assignment the specifications are that you can assume that array1 and array2 are of the same length.
Any help and or comments would be greatly appreciated.
Thanks