Note that there are some explanatory texts on larger screens.

plurals
  1. POThe extended class doesn't seem to be inheriting some stats
    primarykey
    data
    text
    <p>I have three classes, <code>Organism</code>, <code>Creature</code> and <code>Plant</code>. <code>Creature</code> and <code>Plant</code> both extend <code>Organism</code>. Now, this code worked when everything was bundled into <code>Organism</code>, so I assume the GUI class isn't at fault. Basically, what is happening is only <code>Creature</code> specific stats are being generated, whereas the <code>Organism</code> stats are being ignored, returning <code>0</code>. I cannot find anything wrong with it, so I thought you guys could help. (Ignore the mess, I am still playing around.)</p> <h2>Creature Class</h2> <pre><code>import java.awt.Color; import java.util.Random; public class Creature extends Organism { private char gender; private int strength, speed, diet, aggression; private int mated, maxMated; public Creature() { super(); Random rand = new Random(); if(rand.nextInt(2) == 0) { gender = 'f'; } else { gender = 'm'; } mated = 0; setStats(); } public Creature(String gen[]) { super(gen); Random rand = new Random(); if(rand.nextInt(2) == 0) { gender = 'f'; } else { gender = 'm'; } x = rand.nextInt(maxX); y = rand.nextInt(maxY); setStats(); } public Creature(String gen[], int newX, int newY) { super(gen, newX, newY); this.gene = gen; Random rand = new Random(); if(rand.nextInt(2) == 0) { gender = 'f'; } else { gender = 'm'; } isAlive = true; x = newX; y = newY; setStats(); } public int getAggro() { return aggression; } public int getDiet() { return diet; } public int getSpeed() { return speed; } public void setStats() { strength = (gene[1].charAt(0)-48) + 1 + ((gene[1].charAt(1)-48) * (gene[1].charAt(2)-48)); speed = (strength + (gene[2].charAt(0)-48) - (gene[2].charAt(1)-48) + (gene[2].charAt(2)-48))/(size + 1); diet = (gene[7].charAt(0)-48) + 1 + ((gene[7].charAt(1)-48) * (gene[7].charAt(2)-48)*(3/4)); aggression = ((strength + size)/2) / (gene[8].charAt(0)-48 + gene[8].charAt(1)-48 + gene[8].charAt(2)-48 + 1); maxHealth = 64 + size + (strength / 2); maxHunger = 100 + (size - speed - aggression); health = maxHealth; hunger = maxHunger; } public Creature breed(Creature mate) { Random rand = new Random(); int x = rand.nextInt(gene.length); int y = rand.nextInt(gene[x].length()); String geneY[] = new String[16]; int i; for(i = 0; i &lt; x; i++) { geneY[i] = gene[i]; } geneY[x] = gene[x].substring(0,y); geneY[x] = geneY[x] + mate.gene[x].substring(y); for(i = x + 1; i &lt; 16; i++) { geneY[i] = mate.gene[i]; } char newGender; if(rand.nextInt(2) == 0) { newGender = 'f'; } else { newGender = 'm'; } hunger = hunger /2; mate.hunger = mate.hunger /2; mated = mated + 1; mate.mated = mate.mated + 1; Creature temp = new Creature(geneY,this.getX(),this.getY()); temp.mutate(); return temp; } public void eat(Organism b) //A eats B { b.isAlive = false; this.hunger = this.hunger + b.size; } public boolean isCompatible(Creature org) { int differences = 0; for(int i = 0; i &lt; this.gene.length; i++) { if(!this.gene[i].equals(org.gene[i])) { differences = differences + 1; } } if(differences &gt; 1 || this.gender == org.gender || mated == maxMated || org.mated == org.maxMated) { return false; } return true; } public void moveTo(Organism org) { int vectX, vectY, moveX = 0, moveY = 0; double angle; vectX = this.x - org.x; vectY = this.y - org.y; if(vectX == 0) { moveY = this.speed; } if(vectY == 0) { moveX = this.speed; } if(vectX == 0 &amp;&amp; vectY == 0) { moveX = 0; moveY = 0; } if(vectX != 0 &amp;&amp; vectY != 0) { angle = ((Math.atan((vectY)/(vectX)))/(2*Math.PI))*360; if(angle &lt; 0) { angle = angle * - 1; } moveX = (int)(Math.sin(angle)*this.speed); moveY = (int)(Math.cos(angle)*this.speed); } if(Math.sqrt((vectX*vectX)+(vectY*vectY)) &lt; speed) { if(vectX &gt; 0) { this.x = this.x - vectX; } else { this.x = this.x + vectX; } if(vectY &gt; 0) { this.y = this.y - vectY; } else { this.y = this.y + vectY; } } else { if(vectX &gt; 0) { this.x = this.x - moveX; } else { this.x = this.x + moveX; } if(vectY &gt; 0) { this.y = this.y - moveY; } else { this.y = this.y + moveY; } } } } </code></pre> <h2>Organism Class</h2> <pre><code>import java.awt.Color; import java.util.Random; public class Organism { protected String gene[] = new String[16]; protected boolean isAlive; protected int x; protected int y; protected int size, sense, fertility, scent; protected int health, hunger, maxHealth, maxHunger; protected int maxX = 1000; protected int maxY = 1000; private Color color = new Color(255,0,0); public Organism() { Random rand = new Random(); for(int i = 0; i &lt; 16; i++) { gene[i] = ""+ rand.nextInt(4) + rand.nextInt(4) + rand.nextInt(4); } isAlive = true; x = rand.nextInt(maxX); y = rand.nextInt(maxY); setStats(); } public Organism(String gen[]) { Random rand = new Random(); this.gene = gen; isAlive = true; x = rand.nextInt(maxX); y = rand.nextInt(maxY); setStats(); } public Organism(String gen[], int newX, int newY) { this.gene = gen; isAlive = true; x = newX; y = newY; setStats(); } public Color getColor() { return color; } public int getX() { return x; } public void setX(int tempX) { this.x = tempX; } public int getY() { return y; } public void setY(int tempY) { this.y = tempY; } public int getHunger() { return hunger; } public void setHunger(int hun) { this.hunger = hun; } public int getHealth() { return health; } public void setHealth(int heal) { this.health = heal; } public void setStats() { size = (gene[0].charAt(0)-48) + 1 + ((gene[0].charAt(1)-48) * (gene[0].charAt(2)-48)); sense = (gene[5].charAt(2)-48) + 1 + ((gene[5].charAt(1)-48) * (gene[5].charAt(2)-48)); fertility = 22 - size + (gene[6].charAt(0)-48) + 1 + ((gene[6].charAt(1)-48) * (gene[6].charAt(2)-48)); scent = (gene[8].charAt(0)-48 + gene[8].charAt(1)-48 + gene[8].charAt(2)-48); } public int getSize() { return size; } public int getSense() { return sense; } public boolean getAlive() { return isAlive; } public void setAlive(boolean live) { this.isAlive = live; } public String getInfo() { String info; info = "Health: " + this.health + ", Hunger: "+ this.hunger + ", Status: " + this.isAlive; return info; } /*public String getStats() { String info = "Size: " + this.size + ", Strength: " + this.strength + ", Speed: " + this.speed + ", Sight: " + this.sight + ", Smell: " + this.smell + ", Hearing: " + this.hearing + ", Fertility: " + this.fertility + ", Diet: " + this.diet + ", Aggression: " + this.aggression + ", Scent: " + this.scent; return info; }*/ public String displayGene() { String geneP = "|"; for(int i = 0; i &lt; gene.length; i++) { for(int j = 0; j &lt; gene[i].length(); j++) { switch (gene[i].charAt(j)) { case '0': geneP = geneP + 'A'; break; case '1': geneP = geneP + 'T'; break; case '2': geneP = geneP + 'C'; break; case '3': geneP = geneP + 'G'; break; } } geneP = geneP + "|"; } return geneP; } public void mutate() { Random rand = new Random(); int i = rand.nextInt(10000) + 1; int affected; if(i &gt; 9999) { affected = rand.nextInt(gene.length); i = rand.nextInt(gene[affected].length()); int j = rand.nextInt(4); gene[affected] = gene[affected].substring(0,i)+j+gene[affected].substring(i+1); } } public void hungerCheck() { hunger = hunger - (size / 10 + 1); if(hunger &lt;= 0) { health = health - 1; } if(hunger &gt; (maxHunger * 3)/4) { health = health + 1; } } public void validate() { if(x &gt; maxX) { x = 100; } if(x &lt; 0) { x = 0; } if(y &gt; maxY) { y = 100; } if(y &lt; 0) { y = 0; } if(hunger &gt; maxHunger) { hunger = maxHunger; } if(hunger &lt;= 0) { hunger = 0; } if(health &lt;= 0) { isAlive = false; } if(health &gt; maxHealth) { health = maxHealth; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload