im trying to make a program that will calculate the probability of you having two sons, two daughters, or a boy and a girl. the program is supposed to import a file that has random things like bb, gg, and bg, which it uses to calculate the probability. this is an assignment for my school that i dont quite understand. so far i have
i dont know where to really go after this. i do believe we are supposed to use loops also. any help is much appreciated.
this looks like you are jumping in and writing code before you have thought out what you want to do. That is almost ALWAYS a sure fire recipe for disaster.
My suggestion would be to throw all this away. Then, write down what your program should do - in English (or whatever your natural language of choice is). Think very broadly. This is what's called pseudo-code. Something like
open the file
read each line
add one to the appropriate counter (BB, BG, GB, or GG)
repeat until end of file
compute and print percentages
Once you are done with this, see if you can write the code for each step. if not, refine it. the last one might need to be broken down into something like
get the total number of lines read
compute the percentage for BB
compute the percentage for BG
When you do start writing code, write as little as possible before you compile, debug, test and fix. EVERY time i write a java program, I do my first compile after this much code:
I then add one or two lines, and re-compile. So after this, I might write code to open the file, and close it. Then i'd re-compile and test.
Then i'd try printing out each line of the file (which requires me to read each line), and re-compile/test.
Note that at each step, I put in just enough additional code to verify what i've done works. I usually then comment it out (so I can easily restore it if something breaks) until i'm satisfied it ALL works, and clean it up at the end - possibly even saving a version with all the extra as a draft in case I need to go back and re-work part of it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
my biggest problem is that im not really familiar with all the new code that i need to do the things like open the file and how to deal with the file after it is imported. we just started this type of assignment today and im not at all familiar with it, and i dont know where to find good examples that will help me to figure out exactly how the code works.