#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Referring Variables from Classes

edited December 2016

Hei guys I started to write a code to kind of simulation the Atomic Fission. So I have two classes : on for the Atoms and one for the Neutrons. And I want to use the X and Y ( and a bunch of others) Variables from the Atom class in the Neutron Class. I tried to referr to them with classname.variablename (for example: atoms.atomX) but then there is a failiure code with: the global variable atomX does not exist. So my question is: how can I use a Variable from another class?

My code in the Atom class is:

``````class Atom {
float atomWid = 60;
float atomHei = atomWid;
float atomX = random((0+atomWid/2), (width-atomWid/2));
float atomY = random((0+atomHei/2), (height-atomHei));
float atomXspeed = random(-2, 2);
float atomYspeed = random(-2, 2);

void show() {
noStroke();
fill(40, 40, 40);
ellipse(atomX, atomY, atomWid, atomHei);
}

void move() {
atomX += atomXspeed;
atomY += atomYspeed;

if ((atomX-atomWid/2) < 0) {
atomXspeed *= -1;
}
if ((atomX+atomWid/2) > width) {
atomXspeed *= -1;
}
if ((atomY-atomHei/2) < 0) {
atomYspeed *= -1;
}
if ((atomY+atomHei/2) > height) {
atomYspeed *= -1;
}
}
}
``````

and my code in the Neutron class is:

``````class Neutron {

void show() {
noStroke();
fill(255, 10, 10);
ellipse(**atoms.atomX, atoms.atomY, atoms.atomWid, atoms.atomHei**);
}

void move() {
}
}
``````

and here is my setup() and draw() function (i seperated the three codes in different tabs btw):

``````Atom[] atoms = new Atom;
Neutron[] neutrons = new Neutron;

void setup() {
size(600, 600);

for (int i=0; i<atoms.length; i++) {
atoms[i] = new Atom();
}
for (int i=0; i<neutrons.length; i++) {
neutrons[i] = new Neutron();
}
}

void draw() {
background(240, 240, 240);
for (int i=0; i<atoms.length; i++) {
atoms[i].show();
atoms[i].move();
}
for ( int i=0; i<neutrons.length; i++) {
neutrons[i].show();
}
}
``````

Sry for the stupid question & bad explanation Im new here :) I really hope someone can help me ... thanks :)

Tagged:

• Btw I dont know why some parts of the code arent in order ... sry for that

• To prepare your code for the forum so it looks right, edit your post above, make sure there is a blank line before and after the code, select the code and click on the C button. Then save changes and your code will be formatted.

• Im gonna try I didnt know how to do it before sry ;)

• You've posted the same question 3 times. I'm going to delete the other two. Please don't do it again.

• ellipse(atoms.atomX, atoms.atomY, atoms.atomWid, atoms.atomHei);

The problem here is that the variable `atoms` is an array not a single `Atom` object.

In your program you are associating a neutron with a particular atom so one solution would be to

1) modify the Neutron: show method to

``````  void show(Atom atom) {
noStroke();
fill(255, 10, 10);
ellipse(atom.atomX, atom.atomY, atom.atomWid, atom.atomHei**);
}
``````

2) Now in the draw method to

``````void draw() {
background(240, 240, 240);
for (int i=0; i<atoms.length; i++) {
atoms[i].show();
atoms[i].move();
}
for ( int i=0; i<neutrons.length; i++) {
neutrons[i].show(atoms);  //  Draw the neutrons as if part of atoms
}
}
``````

This is not ideal because how do you know which neutrons are associated with which atom?

You need to step back and think about what you are representing. In nuclear fission/fusion neutrons are either part of an atom or moving unattached between atoms. In which case I would modify the neutron class to have a reference to an Atom object.

• @koogs thanks you beat me to it.

• Thanks a lot Im gonna try to do it in another way ... you really helped me there :)

• Given an Atom is made outta Proton + Neutron + Electron, you should go w/ inheritance by composition: *-:)

``````// forum.Processing.org/two/discussion/19949/
// referring-variables-from-classes#Item_8

// GoToLoop (2016-Dec-27)

static final int ATOMS = 3, W = 60, H = 60, RW = W>>1, RH = H>>1;
final Atom[] atoms = new Atom[ATOMS];

void setup() {
for (int i = 0; i < ATOMS; ++i) {
int x = (int) random(RW, width  - RW);
int y = (int) random(RH, height - RH);

int protons   = (int) random(1, 10);
int neutrons  = (int) random(1, 15);
int electrons = (int) random(5, 30);

atoms[i] = new Atom(x, y, W, H, protons, neutrons, electrons);
}

printArray(atoms);
exit();
}

class Atom {
final Proton[]   protons;
final Neutron[]  neutrons;
final Electron[] electrons;

int x, y, w, h;

Atom(int px, int py, int pw, int ph, int p, int n, int e) {
x = px;
y = py;
w = pw;
h = ph;

protons   = new Proton[p];
neutrons  = new Neutron[n];
electrons = new Electron[e];
}

@ Override String toString() {
final StringBuilder sb = new StringBuilder();

sb.append("x: ").append(x).append(", y: ").append(y);
sb.append(", w: ").append(w).append(", h: ").append(h);

sb.append("\nProtons: ").append(neutrons.length);
sb.append(", Neutrons: ").append(neutrons.length);
sb.append(", Electrons: ").append(electrons.length);

return sb.toString();
}
}

class Proton {
}

class Neutron {
}

class Electron {
}
``````
• I appreciate your help, but honestly I dont get this code ... Im just not on your level ;) But thanks anyway :)