Creating an Object with Properties Defined in an Array List?
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:
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:
Thanks a bunch!
Short answer is you cannot. ArrayList is an instance and as such will be scoped to where it is declared. So unless ArrayList is declared within Molecule (make it static if its shared), then you'll need to pass ArrayList<Atom> as a parameter to any method that uses it.
As for your createAtoms method itself, you cannot do that at all. new Atom cannot be assigned the value of atomA (wherever that is declared). What you want is to fetch from it, but to do so you need to iterate it.
|All times are GMT +1. The time now is 05:35 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.