In a statistics class you learn about distributions Among ma
In a statistics class, you learn about distributions. Among many types of (discrete) distributions, there is one particularly useful: binomial distributions. Binomial distributions are useful because they let us deal with situations where there are only two relevant outcomes. Examples of these outcomes are: Heads or Tails; Success or Failure; Something happened or Didn\'t; Perfect or Imperfect. As you can see, the binomial distributions can be applied to many problems.
Microsoft Excel has a built-in function called BINOM.DIST to compute this type of probability. For example, BINOM.DIST can calculate the probability that two of the next three incoming customers to your shop are male.
The syntax of BINOM.DIST is:
BINOM.DIST(x, n, p, cumulative)
where
x: the number of successes in trials.
n: the number of independent trials.
p: The probability of success on each trial.
cumulative: is the distribution cumulative (we assume FALSE for this exercise)?
An example case would be:
=BINOM.DIST(2,10,0.4,FALSE)
which returns
0.120932
For this exercise, write a code that mimics Excel\'s BINOM.DIST functionality. For example,
>java Binomdist 2 10 0.4
0.12093235199999997
Include the following when you submit your assignment:
/**
* Compilation: javac Binomdist.java
* Execution: java Binomdist x n p
* Dependent libraries: List any here
*
* Reads in x, n, and p as command-line arguments and prints out
* binomial probability.
*
* Sample output
* > java Binomial 2 10 0.4
* 0.12093235199999997
Your code file (should be plain text with a .java extension) and the class file (with a .class extension). The code should have comments explaining what each method does. In the Doc comments, include a brief explanation of how to execute your class:
Any external libraries you used. You do not need to submit libraries included in the Java Development Kit.
In short, you will include 1) your .java file, 2) your .class file, and 3) any external libraries.
Solution
Step1) Explanation of BINOM.DIST
When cumulative = TRUE, the BINOMDIST(x, n, p, cumulative) function returns the probability of x or fewer successes in n independent Bernoulli trials. Each of the trials has an associated probability p of success (and probability 1-p of failure). When cumulative = FALSE, BINOMDIST returns the probability of exactly x successes.
Syntax
BINOMDIST(x, n, p, cumulative)
Parameters
x is a non-negative integer
n is a positive integer
0 < p < 1
Cumulative is a logical variable that takes on the values TRUE or FALSE
Step 2) BINOMIST.java Program
//----------Expression Calculation Engine for Java---------------
package org.boris.expr.function.excel;
import org.boris.expr.Expr;
import org.boris.expr.ExprDouble;
import org.boris.expr.ExprError;
import org.boris.expr.ExprException;
import org.boris.expr.ExprNumber;
import org.boris.expr.function.AbstractFunction;
import org.boris.expr.util.Statistics;
public class BINOMDIST extends AbstractFunction
{
public Expr evaluate(Expr[] args) throws ExprException
{
assertArgCount(args, 4);
Expr ens = evalArg(args[0]);
if (!isNumber(ens))
return ExprError.VALUE;
int nums = ((ExprNumber) ens).intValue();
if (nums < 0)
return ExprError.NUM;
Expr et = evalArg(args[1]);
if (!isNumber(et))
return ExprError.VALUE;
int trials = ((ExprNumber) et).intValue();
if (nums > trials)
return ExprError.NUM;
Expr ep = evalArg(args[2]);
if (!isNumber(ep))
return ExprError.VALUE;
double p = ((ExprNumber) ep).doubleValue();
if (p < 0 || p > 1)
return ExprError.NUM;
Expr ec = evalArg(args[3]);
if (!(ec instanceof ExprNumber))
return ExprError.VALUE;
boolean c = ((ExprNumber) ec).booleanValue();
return new ExprDouble(Statistics.binomDist(nums, trials, p, c));
}
}

