I have to create a Binomial Class. One of the methods is called coefficients. Cr
ID: 656421 • Letter: I
Question
I have to create a Binomial Class. One of the methods is called coefficients. Create an array of length n +1, load each elemnt, k, of this array with the value returned by combinations (n,k), and return a reference to the new array. The elemnts of this array should be equal to the coefficient of a binomial. I posted both Coefficient method and combination method but I'm not sure how to load each element in the array that is from the combination method. Thanks
public static double combinations(int n, int k)
{
double logCoef;
double coef;
logCoef = gamma(n + 1) - gamma(k + 1) - gamma(n - k + 1);
coef = Math.exp(logCoef);
return coef;
}
public static double[] coefficients(int n)
{
int [] anArray;
anArray = new int[n+1];
while(n != 0)
{
anArray[n] = ?
}
}
This is my Binomial class if it helps
import java.util.Scanner;
public class Binomial extends IncompleteBeta
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Select coefficients(c) or probability(p)");
String choiceBinomial = keyboard.nextLine();
if(choiceBinomial.equals("c"))
{
System.out.println("Enter order: ");
int order = keyboard.nextInt();
System.out.println(coefficients(order));
}
else if(choiceBinomial.equals("p"))
{
System.out.println("Enter total number of trials: ");
int trials = keyboard.nextInt();
System.out.println("Enter successes or minimum successes: ");
int success = keyboard.nextInt();
System.out.println("Enter single-trial probability: ");
int probability = keyboard.nextInt();
System.out.println("Probability of " + success + " sucesses = ");
System.out.println(probability(trials, success, probability));
System.out.println("Probability of at least " + success + " successes = ");
System.out.println(cumaltiveProbability(trials, success, probability));
}
}
public static double combinations(int n, int k)
{
double logCoef;
double coef;
logCoef = gamma(n + 1) - gamma(k + 1) - gamma(n - k + 1);
coef = Math.exp(logCoef);
return coef;
}
public static double[] coefficients(int n)
{
int [] anArray;
anArray = new int[n+1];
while(n != 0)
{
anArray[n] = ;
}
}
public static double probability(int n, int k, double p)
{
if(k > n)
return 0.0;
else
return (combinations(n,k) * Math.pow(p, k) * Math.pow((1-p), (n-k)));
}
public static double cumaltiveProbability(int n, int k , double p)
{
if(k > n)
return 0.0;
else
return incompleteBeta(k, (n - k + 1), p);
}
}
Explanation / Answer
//Binomial .java
import java.util.Scanner;
public class Binomial extends IncompleteBeta
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Select coefficients(c) or probability(p)");
String choiceBinomial = keyboard.nextLine();
if(choiceBinomial.equals("c"))
{
System.out.println("Enter order: ");
int order = keyboard.nextInt();
System.out.println(coefficients(order));
}
else if(choiceBinomial.equals("p"))
{
System.out.println("Enter total number of trials: ");
int trials = keyboard.nextInt();
System.out.println("Enter successes or minimum successes: ");
int success = keyboard.nextInt();
System.out.println("Enter single-trial probability: ");
int probability = keyboard.nextInt();
System.out.println("Probability of " + success + " sucesses = ");
System.out.println(probability(trials, success, probability));
System.out.println("Probability of at least " + success + " successes = ");
System.out.println(cumaltiveProbability(trials, success, probability));
}
}
public static double combinations(int n, int k)
{
double logCoef;
double coef;
//Note : gamma method not provided in the code.
logCoef = gamma(n + 1) - gamma(k + 1) - gamma(n - k + 1);
coef = Math.exp(logCoef);
return coef;
}
//Modified method coefficients
//The method coefficients that accpets the n value as input
//argumet and creates an array anArray by calling the combinations
//method and return values are stored in the anArray at index ,k
//postions
public static double[] coefficients(int n)
{
double [] anArray;
anArray = new double[n+1];
//create an index ,k value and set to 0
int k=0;
//run while loop with condition k <n+1,size of the array
while(k<(n+1))
{
anArray[k] =combinations(n, k);
//increment the index of each k index of array anArray
k++;
}
//return the anArray which fill with value of combinations method that
//calls the with n and index value,k of anArray
return anArray;
}
public static double probability(int n, int k, double p)
{
if(k > n)
return 0.0;
else
return (combinations(n,k) * Math.pow(p, k) * Math.pow((1-p), (n-k)));
}
public static double cumaltiveProbability(int n, int k , double p)
{
if(k > n)
return 0.0;
else
return incompleteBeta(k, (n - k + 1), p);
}
}
----------------------------------------------------------
//Not provided this class code in the post.
//This is just a stub class .
//Make sure that this class is works as per your requirements
//IncompleteBeta.java
public class IncompleteBeta
{
public static double incompleteBeta(int k, int i, double p)
{
// TODO Auto-generated method stub
return 0;
}
public static int gamma(int i) {
// TODO Auto-generated method stub
return 0;
}
}
Hope this helps you