I'm a beginner coder in Java and I'm confused about how to code this one assignment I have. If I can get any help, then it would be fantastic.
I am using BlueJ.
Assignment:
Write a program to calculate the probability that a family with two children
will consist of two boys, a boy and a girl, or two girls given a text file with combinations already given (BB, BG, GG). They want me to show correct use of loops and using the Scanner method to read text files.
test1.txt
GB BG BG GG GB GB GB GB BG BG
Here's code of what I have so far:
double sampleSize = 0.0; double twoBoysCounter = 0.0; double oneBoyGirlCounter = 0.0; double twoGirlsCounter = 0.0; Scanner inFile = new Scanner(new File("test1.txt")); while (inFile.hasNext()) { String token = inFile.nextLine(); String line = token; { if (line.equalsIgnoreCase("BB")) { twoBoysCounter++; } else if (line.equalsIgnoreCase("BG") || line.equalsIgnoreCase("GB")) { oneBoyGirlCounter++; } else { twoGirlsCounter++; } } } double twoBoys = twoBoysCounter / sampleSize; double oneBoyGirl = oneBoyGirlCounter / sampleSize; double twoGirls = twoGirlsCounter / sampleSize; System.out.println("Sample Size: " + sampleSize); System.out.println("Two Boys: " + twoBoys + "%"); System.out.println("One Boy One Girl: " + oneBoyGirl + "%"); System.out.println("Two Girls: " + twoGirls + "%");