1

I have been asked to create two main methods, the second main method contains an ArrayList in which I need to print out. The program only calls the first main method so I think I need to call the ArrayList from there. Because the second ArrayList is contained within a main method, I cannot creae a etter method in which I could use to call that method so I am confused on how I would call that ArrayList and print it out.

Here is the relevant code:

Second main method

import java.util.ArrayList;
public class Demo {

    public static void main(String[] args) {
        ArrayList<Animal> animalGroup = new ArrayList<>();

        animalGroup.add(new Wolf("Sam", 5));
        animalGroup.add(new Parrot("George", 3));
        animalGroup.add(new Wolf("Wesley", 7));
        animalGroup.add(new Parrot("Pat", 10));

    }

}

First main method

    public class Main {

        public static void main(String[] args) {

            Demo.main(args); // populate the ArrayList in Demo Class main method 2.
    ArrayList<Animal> animalGroup = Demo.animalGroup; // Retrieve the ArrayList in Demo class.
System.out.println(animalGroup);     
        }
    }

Desired output

 Sam, 5
 George, 3
 Wesley, 7
 Pat, 10

Parrot class

  public class Parrot extends Omnivore
    {   

        private final int age;
        private final String name;

    Parrot(String name, int age)   
    {
        this.age = age;
        this.name = name;
    }     
    Parrot(int i)   
    {
        this("Polly", i);
    }  

        public String getName() 
        {
            return name;
        }
        public int getAge() 
        {
            return age;
        }

    }

Animal

abstract public class Animal implements Comparable<Animal>
{

    int age;
    String name;
    String noise;

Animal(String name, int age)   
{
    this.age = age;
    this.name = name;
} 

Animal()
{

  this("newborn", 0);
}

public String getName() {
        return name;
    }
  public int getAge()
    {
        return age;
    }

    public void setName(String newName) {
        name = newName;
    }

}

It is essential that the program is designed this way as my assignment insists upon creating the second main method whhich conains this ArrayList. Any helps on how to output the properties of the ArrayList would be greatly appreciated, thanks.

Ben
  • 153
  • 1
  • 9
  • By the way - You'll want to see this question, maybe. http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4#29140403 – OneCricketeer Nov 28 '16 at 04:44

2 Answers2

2

you can invoke a main method belonging to another class like below

ClassName.main(args);

Change your main class like below

public class Main {
    public static void main(String[] args) 
    {
        Demo.main(args);
    }
}

You can print your array list inside the main method of Demo Class or else u have to make your ArrayList available for Class main if you are going to print it from class Main

SSB
  • 144
  • 8
1

Make this kind of changes:
Second Main Method:

import java.util.ArrayList;
public class Demo {
    public static ArrayList<Animal> animalGroup; // make this an instance static variable to access in other class with main method;

    public static void main(String[] args) {
    animalGroup = new ArrayList<>();

    animalGroup.add(new Wolf("Sam", 5));
    animalGroup.add(new Parrot("George", 3));
    animalGroup.add(new Wolf("Wesley", 7));
    animalGroup.add(new Parrot("Pat", 10));

    }

}

First Main Method:

public class Main {

    public static void main(String[] args) {

    Demo.main(args); // populate the ArrayList in Demo Class main method 2.
    ArrayList<Animal> animalGroup = Demo.animalGroup; // Retrieve the ArrayList in Demo class.
    for(Animal animal : animalGroup) {
         System.out.println(animal.getName()+","+animal.getAge());
    }
}
}

I cant see your other classes but try this if it helps.

msagala25
  • 1,806
  • 2
  • 17
  • 24
  • Thanks for your help, this worked apart form one issue. When I print out each element, they appear something like `QuestionEditor.Parrot@6d06d69c` when they should appear like `Parrot, 5`. Any ideas why? – Ben Nov 28 '16 at 04:55
  • I think that you are printing the hashCode of the Parrot Class. I will edit my answer and I will make an example for you. – msagala25 Nov 28 '16 at 04:58
  • Yeah it does. I don't think I've called it though. – Ben Nov 28 '16 at 05:02
  • can you please include the Animal Class and Parrot Class. and what code you do when you iterate your results. – msagala25 Nov 28 '16 at 05:04
  • I've updated my question and included those classes, the constructors within the Animal and Parrot class probably aren't relevant but I've included them so you know what the whole class looks like. – Ben Nov 28 '16 at 05:13
  • @Ben The best way to do is Override the toString method in Animal class like below ` public String toString() { return name + "," + age ; }` – SSB Nov 28 '16 at 05:16