# Thread: Testing a main method

1. Member Join Date
Oct 2012
Posts
2
Rep Power
0

## 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);
}  Reply With Quote

2. Member Join Date
Sep 2012
Posts
70
Rep Power
0

## 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
Last edited by killutch; 10-21-2012 at 03:45 AM.  Reply With Quote

3. Member Join Date
Oct 2012
Posts
2
Rep Power
0

## 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.

Java 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);
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++) {
System.out.println("Using random generation, the digit is " + i + " and the proportion is " + randomLeadingDigit[i]);
}

System.out.println("The distance of the exponential growth group is" + exponentialdifference);
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 ;
for (int i = 0; i<number.length; i++) {
}
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;
}
}```
Last edited by yashvi.c.shah; 10-21-2012 at 07:16 AM.  Reply With Quote

4. ## Re: Testing a main method

http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum  Reply With Quote