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..............

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..............

take a look at this part of your code:

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);
        }

it's clear that h is an instance of adult, and propably the age will be correct. so, what you want to do, is print the value of h.
but, after testing for h, you print the entire collection, which means also the youngsters

I'm not on a pc where Java is installed for the moment, so forgive me if I make any errors :) but it's supposed to be a bit more like this:

hm.add(a);hm.add(b);hm.add(c);hm.add(d);
        hm.add(e);hm.add(f);
    //    Collection hs=hm;   -> hm already is a collection, you don't need this one
hm.trimToSize();
String toPrint = "";
for ( int i = 0; i < hm.size(); i++){
  Human test = (Human)hm.get(i);
  if ( test instanceof Adult){
    if toPrint.equals("")
      toPrint = test;
   else
     toPrint = ", " + test;
  }
}
System.out.println("Adults: " + toPrint);

Thanks that's perfect. However is there a way of checking the age also and comparing it to some predefined age inside the if() tag, for example:

if ( test instanceof Adult && age>=minAge)

where the age is got from the ArrayList and minAge is some int assigned for example
int minAge=18;

Thanks that's perfect. However is there a way of checking the age also and comparing it to some predefined age inside the if() tag, for example:

if ( test instanceof Adult && age>=minAge)

where the age is got from the ArrayList and minAge is some int assigned for example
int minAge=18;

that would work, but I would choose to use an Age value gotten from within Adult then.
but if you allready know that the person is an adult, why checking again using that age comparison?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.