0

My personal mini project was to learn arrays here, doing a slightly big jump by making an Array of Objects. What I wanted to do was a mini RPG system where I create a class called monster and give it a couple parameters, and create an array of objects of the Monster class. So far I believe I created that Monster class and the Object of Arrays inside the main method class (Exec_Monster) listed below.

It took me a while initially, but I finally got to a point where I can create the array of Monsters and access them inside the Main class. But is there a way for me to create this Array of Objects and access each object from another class (and their individual values)? For Example, I would create a "Battle" class and then I would pull the "Health" value from an object of Monster.

I'm new to Arrays but I have had some experience with classes for the past two weeks here.

Class:

public class Monster
{
    public int hp;
    public String n;
    public Monster(String name,int health){
        n=name;
        hp=health;        
    }
    public int returnHealth(){
        return hp;
    }
    public String returnName(){
        return n;
    }
}

Exec_Monster:

public class Exec_Monster{
public static void main(String args[])
    {//Define Monsters
        Monster[] monsterid=new Monster[]{
        new Monster("Goblin",10),
        new Monster("Elf", 8),
        new Monster("Ant", 3),
        new Monster("Worm", 2),
        new Monster("Black Widow",6)};

        Random chooser;
        int chosenmonster=(chooser.nextInt()*5);
    //Start
        //while (Battle.wonloss==true) {            
          //  Battle.battle();
        }
    }
MrOats
  • 3
  • 3

1 Answers1

0

You'd need to pass the monsters into the Battle object somehow (or into another object that you pass into the Battle object). You could pass it as an argument to a method, but in an Object Oriented world, if the monsters really belong to a battle, you could pass them in the constructor and make them available in all the methods of the Battle class.

Example:

public class Battle {
    private Monster[] monsters;
    private boolean wonloss;

    public Battle(Monster[] monsters) {
        this.monsters = monsters;
    }

    public boolean isWonloss() {
        return wonloss;
    }

    public void battle() {
        // Do something with monsters,

        // and then check if there is any life left in the monsters
        int totalHp = 0;
        for (Monster monster : monsters) {
            totalHp += monster.hp;
        }
        if (totalHp == 0) {
            wonloss = false;
        }
    }
}

The "battle" part of your main method would then look like:

// Start
Battle battle = new Battle(monsterid);
while (battle.isWonloss()) {
    battle.battle();
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Interesting, I'm trying to follow along here, I'm new to this part: `for (Monster monster : monsters) {` Can you explain what is happening inside the parenthesis? Also was creating a private array of objects in another class was the way to pass the list of monsters to the other class? – MrOats Jan 23 '16 at 03:39
  • @MrOats That's a for-each loop, basically it means "for each `Monster monster` in the array `monsters`". See this post for detail on the for-each loop: http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work – Erwin Bolwidt Jan 23 '16 at 03:44
  • Sorry I had issues with my comment, I just fixed it. – MrOats Jan 23 '16 at 03:44
  • @MrOats Yes in this example I pass the monsters array to the constructor, and the constructor stores it in a private field so that it remains available for all instance methods in the Battle class – Erwin Bolwidt Jan 23 '16 at 03:46