Write a Java \"library\" class called DProb consisting entirely of static method
ID: 3859402 • Letter: W
Question
Write a Java "library" class called DProb consisting entirely of static methods (similar to a C function library). This type of class is never instanced as an object, so references to methods use the class name instead of an object name (e.g. DProb.xxx(...)). The DProb class contains functions for calculating permutations, combinations, and several discrete probability distributions. For each function, use a long datatype for all integer parameters and a double datatype for all decimal parameters. Do not use factorial functions in your algorithms (overflow problems). The return type is double for each function. Your DProb class should include the following functions:
1. Permutation(long N, long X): This function returns the number of ways X objects can be drawn from N objects in a particular order. To reduce the problem of integer overflow, your algorithm should convert (cast) all values to double while you calculate the result.
e.g. double perm = 1.0
Then in loop: perm = perm*(double)(N-i)
2. Combination(long N, long X): This function returns the number of ways X objects can be drawn from N objects ignoring the order in which the objects are drawn. To reduce the problem of integer overflow, your algorithm should:
a. For special cases X = 0 and X = N, the return value is 1.0.
b. Calculate C(N,X) or C(N,N-X) depending on whether X or N-X is smaller (same value).
e.g To calculate C(50,47), calculate C(50,3) instead.
c. Alternate multiplies and divides (in a loop).
e.g. C(25,4) = 25 / 1 * 24 / 2 * 23 / 3 * 22 / 4
d. Convert (cast) all values to double while you calculate the result.
e.g. double comb = 1.0
Then in the loop: comb = comb*(double)(N-i)/(double)(i + 1)
3. HyperGeometric(long Np, long Xp, long N, long X): This function returns the hypergeometric probability of having a total of X successes in a sample of size N, where the sample is drawn (without replacement) from a population of size Np containing Xp successes. In this probability distribution, Np, Xp, and N are fixed, and X is the random variable.
4. Binomial(double P, long N, long X): This function returns the binomial probability of having a total of X successes in N independent trials, where P is the probability of a success in each trial. In this probability distribution, P and N are fixed, and X is the random variable.
5. Poisson(double Xmean, long X): This function returns the Poisson probability of having a total of X independent occurrences in a fixed-size time interval (e.g. X arrivals in a 10-minute interval). The parameter Xmean is the expected (average) number of occurrences per time interval. In this probability distribution, Xmean is fixed, and X is the random variable. The Poisson function can be used to approximate a Binomial probability when N is large and P is small (e.g. N > 100 and P < .05). In this case, let Xmean = N*P, and use the Poisson function.
Part B
Write a test program that uses the DProb class functions to solve several counting problems and probability problems involving discrete random variables. The problems are listed below:
1. A department contains 33 employees. The manager is going to randomly draw 4 employees and give each one a prize.
a. How many ways can the 4 employees be drawn if the order in which they are drawn matters (i.e. the prizes have different values)?
b. How many ways can the 4 employees be drawn if the order in which they are drawn does not matter (i.e. all prizes have the same value)?
2. Suppose that the CS Dept at Happy Valley State College has two computer labs. Lab-A contains 50 PC's and Lab-B contains 30 PC's. Nine of the Lab-A PC's and 4 of the Lab-B PC's have been infected with spyware. A sample of PC's will be inspected to check for spyware.
a. Suppose 8 Lab-A PC's are randomly drawn. What is the probability that none of the PC's in the sample is infected?
b. Suppose 8 Lab-B PC's are randomly drawn. What is the probability that none of the PC's in the sample is infected?
c. Suppose 4 Lab-A PC's are randomly drawn, and a separate random sample of 4 Lab-B PC's is drawn. What is the probability that none of the PC's in either sample is infected?
d. Suppose that from a combined list of all 80 Lab-A and Lab-B PC's, 8 PC's are randomly drawn. What is the probability that none of the PC's in this sample is infected?
3. A munitions warehouse contains 71 bombs, of which 5 are defective. A sample of 10 bombs will be drawn and tested.
a. What is the probability that the sample will contain exactly 2 defective bombs?
b. What is the probability that the sample will contain less than 2 defective bombs?
4. Suppose that the same munitions warehouse contains a very large number of hand grenades, of which 6.9% are defective. A sample of 30 grenades will be drawn and tested.
a. What is the probability that the sample will contain exactly 3 defective grenades?
b. What is the probability that the sample will contain less than 3 defective grenades?
5. Suppose that the munitions warehouse has just received a large shipment of a new, higher-quality hand grenade. Suppose that only 2.6% of the grenades in the shipment are defective. A sample of 125 grenades will be drawn and tested.
a. What is the probability that the sample will contain exactly 4 defective grenades?
b. What is the probability that the sample will contain less than 4 defective grenades?
Solve the problems in 5a and 5b using the Binomial distribution. Then solve the same problems using the Poisson distribution as an approximation. How good is the Poisson approximation?
Explanation / Answer
Hi,
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.SerializationUtils;
public class Dprob {
private final static String characters = "abcd";
private static int length = characters.length();
public static void main(String[] args) {
List
source = new LinkedList
();
// form the source list that have all the possible positions
for (int i = 0; i < length; i++) {
source.add(i);
}
// create a target list for forming unique Dprob
List
target = new LinkedList
();
combine(source, target);
}
public static void combine(List
source, List
target) {
// break the recursion
if (target.size() == length) {
for (int i = 0; i < length; i++) {
System.out.print(characters.charAt(target.get(i)));
}
System.out.println();
return;
}
for (Integer position : source) {
//form the target combination by selecting a position from the source
List
reducedSource = (List
)SerializationUtils.clone((LinkedList
)source);
reducedSource.remove(position);
List
combinedTarget = (List
)SerializationUtils.clone((LinkedList
)target);
combinedTarget.add(position);
combine(reducedSource, combinedTarget);
}
source.clear();
target.clear();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.