Results 1 to 4 of 4
  1. #1
    yashvi.c.shah is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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);
    }

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

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

  3. #3
    yashvi.c.shah is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Testing a main method

    Guide For New Members
    BB Code List - Java Programming Forum

    After going through them, please edit your posts accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Testing a method
    By CoryBrown in forum New To Java
    Replies: 5
    Last Post: 06-27-2012, 08:10 AM
  2. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:08 AM
  3. Counting Method Execution, Testing Tool
    By PataRican in forum Advanced Java
    Replies: 3
    Last Post: 05-01-2011, 11:24 AM
  4. Testing boolean method
    By SteroidalPsycho in forum New To Java
    Replies: 7
    Last Post: 10-23-2009, 04:12 AM
  5. Array Method Testing
    By Suzanne1187 in forum Java Applets
    Replies: 1
    Last Post: 04-15-2009, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •