Hi! I'm new to JavaScript and I'm working on a project for computer science class.
I'm making a MolecularBondingSimulator, which basically means that I create two or more "atoms" with defined properties and set them in a field together, then get an output dependent on the properties of the original atoms.
For example, I could create an atom with 6 protons and 4 valence electrons (Carbon12), and four atoms with one proton and one valence (Hydrogen). The output for this would be something like:
1 Carbon atom is connected with 4 Hydrogen atoms through covalent bonding. This molecule is very stable.
That's the basics of the program. Eventually, I'll be able to create "custom" atoms with varying numbers of protons, neutrons, and electrons, but for now I'm sticking with the basics.
First, I made a class Atom that held what information was needed in the Atom:
Code:
public Atom(int protons, String name, int neutrons, int electrons, double eN)
{
myProtons = protons;
myNeutrons = neutrons;
myElectrons = electrons;
myEN = eN;
atomicNumber = protons;
atomicMass = protons + neutrons;
}
I made an ArrayList in a class called AtomList that listed the basic properties of the atoms, like so:
Code:
ArrayList<Atom> atomList = new ArrayList<Atom>();
atomList.add(new Atom(1, "hydrogen", 0, 1, 2.2));
atomList.add(new Atom(2, "helium", 2, 0, 0.0));
atomList.add(new Atom(3, "lithium", 4, 1, .98));
atomList.add(new Atom(4, "beryllium", 5, 2, 1.57));
atomList.add(new Atom(5, "boron", 6, 3, 2.04));
atomList.add(new Atom(6, "carbon", 6, 4, 2.55));
... and so on.
In a separate class called Molecule, I created a method called createAtoms that would utilize the information from Atom and AtomList to create Atoms based solely on the number of protons that was inputed. This is where my problem is. I don't know how to write code so that you can input just the number of protons (or, alternatively, the name of the atom) and have the program access the ArrayList and be able to use all the information available there for the various functions that it goes through later to get the desired output.
Here's what I tried to do:
Code:
public void createAtoms(int protons)
{
new Atom = atomA
atomA.protons = protons;
}
but that pretty much failed.
Any ideas?
Thanks a bunch!