JAVA: using NEW variable value from different class

257
April 21, 2017, at 00:54 AM

Dears, I´m new in java and I was wondering if I can write a code that is going to get exact value of a new variable from different class. I would like to let the int calculate variable monsters.

ORG int //private static int monsterexp = elliador.monsters.getExp(monsters.exp);/ I would like to let it count by specific monster: private static int monsterexp = elliador.monsters.getExp(monsters.zombie.exp); If you would also share a technique how to identify the "variable" variable into the code it would be awesome (i mean that I wouldn´t have to write zombie in it, but it would choose zombie or hydra depending on the other void that will choose the monster). Probably I´m mixing alltogether... Thank you very much for your support.

package elliador;
import elliador.monsters;

class source {
private String name;
private static int experience;
private static int attack;
private static int defense;
private static int magic;

private static int monsterexp = elliador.monsters.getExp(monsters.exp);
private static int countofcreatures = 25;
private static int earnexp = countofcreatures * monsterexp;

public source(String name,int experience, int attack, int defense, int magic) {
    this.name = name;
    this.experience = experience;
    this.attack = attack;
    this.defense = defense;
    this.magic = magic;
}
public void myhero() {
    System.out.println("Name: " + getName() + " Experience: " + getExperience() + " Attack: " + getAttack() + " Defense: " + getDefense() + " Magic: " + getMagic());
}
public String getName() {
    return name;
}
public void setName() {
    this.name = name;
}
public int getExperience() {
    return experience;
}
public void setExperience() {
    this.experience = experience + earnexp;
}
public int getAttack() {
    return attack;
}
public void setAttack() {
    this.attack = attack + 1;
}
public int getMagic() {
    return magic;
}
public void setMagic() {
    this.magic = magic + 1;
}
public int getDefense() {
    return defense;
}
public void setDefense() {
    this.defense = defense + 1;
}
}

class monsters

package elliador;
import elliador.heroes;
class monsters {
private String name;
private static int attack;
private static int defense;
private static int magic;
public static int exp;
public monsters(String name, int attack, int defense, int magic, int exp) {
    this.name = name;
    this.attack = attack;
    this.defense = defense;
    this.magic = magic;
    this.exp = exp;
}
//*how to use specific monster in the int mentioned in description?*/
public static int getExp (int exp) {
monsters zombie = new monsters("zombie",1,0,0,1);
monsters hydra = new monsters("Hydra",20,12,3,2);
return monsters.exp;
}

public static int getAttack (int attack) {
monsters zombie = new monsters("zombie",1,0,0,1);

//*monsters hydra = new monsters("Hydra",20,12,3,1);
return monsters.attack;
}
}

class heroes

package elliador;
import elliador.monsters;
public class heroes {
  public static void main (String[] args) throws InterruptedException {
    source Olsen;
    Olsen = new source("Olsen",0,1,1,1);

    Integer i = new Integer(5);
    Integer i2 = new Integer (5);
    System.out.println(i.equals(i2));
    Olsen.myhero();
    Olsen.setExperience();
    Olsen.myhero();
    Olsen.setExperience();
    Olsen.myhero();
    Olsen.setAttack();
    Olsen.setMagic();
    Olsen.myhero();       
    }
    }
Answer 1

Create a class representing a single monster.

package elliador;
class Monster {
private String name;
private int attack;
private int defense;
private int magic;
private int exp;
public Monster(String name, int attack, int defense, int magic, int exp) {
    this.name = name;
    this.attack = attack;
    this.defense = defense;
    this.magic = magic;
    this.exp = exp;
}
public int getExp ( {  
return exp;   
}

public int getAttack () {
return attack;
}
}

Read on encapsulation, and add the rest of the getters.

then create a Collection with all monsters.

List<Monster> monsters = new ArrayList<>();

Add monsters

monsters.add(new Monster("Godzilla", 999, 999, 999, 999));

Get specific monster by index

monsters.get(0);

Or create a class extending collection and override get method to get it by name etc

Or create a map with a key being a name and stats being a list of attributes.

Same with heroes, then you can create a class called Game and do what you need.

PS. By java conventions, class names start with capitals.

READ ALSO
Let app be forced on top of screen for a period of time

Let app be forced on top of screen for a period of time

I am an inexperienced programmer mostly focusing on the business side of ITBut now I am busy building an app that locks your phone for a period of time

227
2-Way Handshaking (mutual authentication ) in Java Webservice for SOAP Request raising handshake_failure

2-Way Handshaking (mutual authentication ) in Java Webservice for SOAP Request raising handshake_failure

I need to implement the 2-Way Handshaking(mutual authentication) for SOAP request

349
getting a weird id in my mongodb after creating my own ObjectId

getting a weird id in my mongodb after creating my own ObjectId

in my case class im creating a new mongo _id:

260
Why collection types check happen only at compile, and Array types are checked at compile time and Runtime

Why collection types check happen only at compile, and Array types are checked at compile time and Runtime

Why collection types check happen only at compile time, and Array types are checked at compile time and Runtime

228