Hi, I have a method that checks whether Objects in a Collection are Human and whether they are of certain age. I have three different classes for Human, Adult and Child. the code for the method is in a different class called Cinema class. the code is as:
//THIS METHOD IS IN A class Cinema
public void testHuman()
{
Human h = new Adult();
Human a = new Adult("Joe",43);
Human b = new Adult("Sue",39);
Human c = new Adult("Tracy",20);
Human d = new Child("Sammy",17);
Human e = new Child("Julie",12);
Human f = new Child("Mona",6);
ArrayList<Human> hm = new ArrayList();
hm.add(a);hm.add(b);hm.add(c);hm.add(d);
hm.add(e);hm.add(f);
Collection hs=hm;
/*mrating.getAdult is from a class called MovieRating
* h.getAge() is from a class Human
*/
if(h instanceof Adult && mrating.getAdult()>=h.getAge())
{
System.out.println("Adults: "+hs);
}
}
when i run my application I get this:
Adults: [Name: Joe, Age: 43, Name: Sue, Age: 39, Name: Tracy, Age: 20, Name: Sammy, Age: 17, Name: Julie, Age: 12, Name: Mona, Age: 6]
But I would like it to print out only the names of Adults aged 18 and above instead of everybody.
Some Help please..............