Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2012, 02:06 AM   PM User | #1
PeaceLoveLogic
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
PeaceLoveLogic is an unknown quantity at this point
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:

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!
PeaceLoveLogic is offline   Reply With Quote
Old 08-28-2012, 02:56 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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;
            break;
        }
    }
    
// 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.

Edit:
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 02:59 AM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
PeaceLoveLogic (08-28-2012)
Reply

Bookmarks

Tags
arraylist, class, javascript, method, object

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:08 PM.


Advertisement
Log in to turn off these ads.