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.