Results 1 to 4 of 4
Thread: Testing a main method
- 10-20-2012, 10:06 PM #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);
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;
randomLeadingDigit = calculateLeadingDigitProportions(randomNumbers);
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;
exponentialdifference = calculateDistance(BenfordLeadingDigit, BenfordSupportCode.getBenfordProbabilities());
System.out.println("The distance of the exponential growth group is" + exponentialdifference);
double randomdifference;
randomdifference = calculateDistance(randomLeadingDigit, BenfordSupportCode.getBenfordProbabilities());
System.out.println("The distance of the random growth group is " + randomdifference);
}
- 10-21-2012, 03:42 AM #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 codeLast edited by killutch; 10-21-2012 at 03:45 AM.
- 10-21-2012, 05:54 AM #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); 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; } }Last edited by yashvi.c.shah; 10-21-2012 at 07:16 AM.
- 10-21-2012, 06:26 AM #4
Re: Testing a main method
Guide For New Members
BB Code List - Java Programming Forum
After going through them, please edit your posts accordingly.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Testing a method
By CoryBrown in forum New To JavaReplies: 5Last Post: 06-27-2012, 08:10 AM -
Junit testing, testing list<e> interface
By mackavelirip in forum New To JavaReplies: 0Last Post: 10-05-2011, 06:08 AM -
Counting Method Execution, Testing Tool
By PataRican in forum Advanced JavaReplies: 3Last Post: 05-01-2011, 11:24 AM -
Testing boolean method
By SteroidalPsycho in forum New To JavaReplies: 7Last Post: 10-23-2009, 04:12 AM -
Array Method Testing
By Suzanne1187 in forum Java AppletsReplies: 1Last Post: 04-15-2009, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks