Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Thanked 0 Times in 0 Posts

    Smile Creating an Object with Properties Defined in an Array List?

    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:

    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:

    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:

    public void createAtoms(int protons)
          new Atom = atomA
          atomA.protons = protons;
    but that pretty much failed.

    Any ideas?

    Thanks a bunch!

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    This is Java, not Javascript. Moving to Java forum.
    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.
    PHP Code:
    public void createAtoms(int protonsArrayList<Atomatoms// not sure if this signature is right.  This is already an instance, so I don't know what createAtom's purpose is since it doesn't return a result
    Atom oChosen null;
        for (
    Atom a atoms)
            if (
    a.myProtons == protons// not sure if this is right either since you don't have Atom class declaration here.
    oChosen a;
    // now I don't know what you want to do with it.

    An overall way around this is to declare the arraylist as static somewhere where it is within scope that molecules can access it. I'd probably just declare it and pass it into the constructor.

    BTW, one other way to get around this is to make the "collection" of possible atoms as a static property in the Atom class itself. Make the constructor private and use a static createInstance to handle it. If it already has it in the list, just pull that instead of creating a new instance. This way it will always be the same instance of an Atom that is equatable instead of multiple instances of Atoms with the same data.
    Last edited by Fou-Lu; 08-28-2012 at 03:59 AM.

  3. Users who have thanked Fou-Lu for this post:

    PeaceLoveLogic (08-28-2012)


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts