# Testing a main method

• 10-20-2012, 10:06 PM
yashvi.c.shah
Testing a main method
Hello all!

I want to test my main method to make sure it is calling all of the right headers but I don't know how. Can someone please explain? I've posted my main code here.

public class BenfordsLaw{

public static void main (String args []) {
double [] BenfordNumbers = generateBenfordNumbers(100, 0.01, 1000);
System.out.println("Generating the Benford sequence with an initial amount of 100 dollars, 0.01 growth per period, and 1000 periods");

double [] randomNumbers = BenfordSupportCode.generateRandomNumbers(1000);
for (int i = 0; i<10; i++) {
System.out.println("For Benford, the digit is " + i + " and the proportion is " + BenfordLeadingDigit[i]);
}
for (int i = 0; i<10; i++) { //in this case, the i is a counter. separate from variable
System.out.println("Using random generation, the digit is " + i + " and the proportion is " + randomLeadingDigit[i]);
}

double exponentialdifference;
System.out.println("The distance of the exponential growth group is" + exponentialdifference);
double randomdifference;
System.out.println("The distance of the random growth group is " + randomdifference);
}
• 10-21-2012, 03:42 AM
killutch
Re: Testing a main method
what do you mean by headers?

you will probably have to import BenfordSupportCode

also several methods in here arn't defined and use the
[code]
[\code] to display code
• 10-21-2012, 05:54 AM
yashvi.c.shah
Re: Testing a main method
Hi Killuch!

I've shared my whole code here. I want to call each method within my code from the main method. My professor creates tests for all the other blocks of code except my main method and I want to make sure I've written it correctly.

Code:

```public class BenfordsLaw{         public static void main (String args []) {                 double [] BenfordNumbers = generateBenfordNumbers(100, 0.01, 1000);                     System.out.println("Generating the Benford sequence with an initial amount of 100 dollars, 0.01 growth per period, and 1000 periods");                                     double [] randomNumbers = BenfordSupportCode.generateRandomNumbers(1000);                     double [] BenfordLeadingDigit = calculateLeadingDigitProportions(BenfordNumbers);                             for (int i = 0; i<10; i++) {                               System.out.println("For Benford, the digit is " + i + " and the proportion is " + BenfordLeadingDigit(i));                                                       }                     double [] randomLeadingDigit = calculateLeadingDigitProportions(randomNumbers);                             for (int i = 0; i<10; i++) {                               System.out.println("Using random generation, the digit is " + i + " and the proportion is " + randomLeadingDigit[i]);                                                       }                             double exponentialdifference = calculateDistance(BenfordLeadingDigit, BenfordSupportCode.getBenfordProbabilities());                     System.out.println("The distance of the exponential growth group is" + exponentialdifference);                     double randomdifference = calculateDistance(randomLeadingDigit, BenfordSupportCode.getBenfordProbabilities());                     System.out.println("The distance of the random growth group is " + randomdifference);                                                   }                             public static double [] generateBenfordNumbers (double initialAmount, double growthRate, int numberPeriods) {                     double [] amountOfMoney = new double [numberPeriods];                             for (int i = 0; i<numberPeriods; i++) {                               double nextNumber = initialAmount * Math.pow(1+growthRate, i);                               amountOfMoney [i] = nextNumber;                                                                   }                             return amountOfMoney;                                                                                                                         }           public static int calculateLeadingDigit(int number) {                       if (number == 0)                 return 0;                       double positiveNumber = Math.abs(number);                       while (positiveNumber>=10) {                 positiveNumber = positiveNumber/10.0;                                                 }                 int IntpositiveNumber = (int) positiveNumber;                     return IntpositiveNumber;                                                                   }           public static double [] calculateLeadingDigitProportions (double [] number) {                     double [] result = new double [10];                     for (int i = 0; i<number.length; i++) {                     int leadingDigit = calculateLeadingDigit ((int) number [i]);                       result[leadingDigit]++;                                                           }                     for (int i = 0; i<result.length; i++) {                       result [i] = (result [i]/number.length);                                                           }                     return result;                                                                                       }           public static double calculateDistance(double [] distance1, double [] distance2) {                     double [] array1 = new double [distance1.length];                     double [] array2 = new double [distance2.length];                     for (int i = 0; i<distance1.length; i++) {                       array1[i] = distance1 [i];                       array2[i]= distance2 [i];                                                             }                     double [] differenceOfArray = new double [distance1.length];                     for (int i = 0; i<distance1.length; i++) {                     differenceOfArray [i] = Math.pow((array1[i]) - (array2[i]),2);                                                           }                       double euclideanDistance = 0;                       for (int i = 0; i<distance1.length; i++) {                 euclideanDistance += (differenceOfArray[i]);                                                               }                       double distance = Math.sqrt(euclideanDistance);                       return distance;                                                                                           }                       }```
• 10-21-2012, 06:26 AM
DarrylBurke
Re: Testing a main method
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum