I had a couple of moments to spare, so I solved this one for you..
you did get (or should have gotten) an error message, namely
>> Adult cannot be cast to java.lang.String
and this for every time you tried to print. you were trying to cast the element of your arrayList (which was either Adult or Child of type) to a String.
I gave you a concept to follow, not the entire sollution. I'll hereby post your entire code (as I think it is) with my changes in them.
ow yeah, I commented the dog, rabbit and all that, since I don't have those classes, and didn't think they were really relevant for this exercise
take a look at it and ask back if you don't see the difference:
//Family.java
import java.util.ArrayList;
import java.util.Collection;
import java.io.*;
public class Family
{
private Adult father;
private Adult mother;
private Child[] children= {new Child("Frank",10), new Child("Lisa",18)};;
/*
private Dog ourDog;
private Cats ourCat;
private Rabbit ourRabbit; */
private static ArrayList mGoers = new ArrayList();
// int Age;
public Family()
{
}
public Family(Adult father, Adult mother, Child[] children)
{
// setFather(father);
// setMother(mother);
// setChildren(children);
/* ourDog=new Dog("");
ourCat=new Cats("");
ourRabbit = new Rabbit("");*/
}
// private Adult getFather()
// {
// return this.father;
// }
//
// private Adult getMother()
// {
// return this.mother;
// }
//
// private Child[] getChildren()
// {
// return this.children;
// }
//
// private void setFather(Adult father)
// {
// this.father = father;
// }
//
// private void setMother(Adult mother)
// {
// this.mother = mother;
// }
//
// private void setChildren(Child[] children)
// {
// this.children = children;
// }
public Collection getMovieGoers(int rating)
{
father = new Adult("John",42);
mother = new Adult("Laura",36);
//I consider rating here to be the starting age that is allowed for the movie
if ( father.getAge() >= rating)
mGoers.add(father);
if ( mother.getAge() >= rating)
mGoers.add(mother);
for(int i = 0; i < children.length; i++)
{
if (children[i].getAge() >= rating)
mGoers.add(children[i]);
}
return mGoers;
}
public static void main(String args[])throws IOException
{
MovieRating mRt = new MovieRating();
// Scenario I : Movies rated G
try
{
Family f = new Family();
ArrayList mgGoers= (ArrayList)f.getMovieGoers(mRt.getGeneral());
String persons = "";
for ( int i = 0; i < mgGoers.size(); i++)
{
String name = "";
if ((mgGoers.get(i)) instanceof Adult){
Adult a = (Adult)mgGoers.get(i);
name = a.getName();
}
else{
Child b = (Child)mgGoers.get(i);
name = b.getName();
}
if ( persons.equals(""))
{
persons += name;
}
else
{
persons += ", " + name;
}
}
System.out.println("The next people can go watch the movie: " + persons);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
// Scenario II : Movies rated PG
try
{
Family f = new Family();
ArrayList mpgGoers= (ArrayList)f.getMovieGoers(mRt.getPGuidance());
String persons = "";
for ( int i = 0; i < mpgGoers.size(); i++)
{
String name = "";
if ((mpgGoers.get(i)) instanceof Adult){
Adult a = (Adult)mpgGoers.get(i);
name = a.getName();
}
else{
Child b = (Child)mpgGoers.get(i);
name = b.getName();
}
if ( persons.equals(""))
{
persons += name;
}
else
{
persons += ", " + name;
}
}
System.out.println("The next people can go watch the movie: " + persons);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
// Scenario III : Movies rated A
try
{
Family fam = new Family();
ArrayList maGoers= (ArrayList)fam.getMovieGoers(mRt.getAdult());
String persons = "";
for ( int i = 0; i < maGoers.size(); i++)
{
String name = "";
if ((maGoers.get(i)) instanceof Adult){
Adult a = (Adult)maGoers.get(i);
name = a.getName();
}
else{
Child b = (Child)maGoers.get(i);
name = b.getName();
}
if ( persons.equals(""))
{
persons += name;
}
else
{
persons += ", " + name;
}
}
System.out.println("The next people can go watch the movie: " + persons);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("End");
// Family fam = new Family();
// fam.ourCat.catType("Angora");
// fam.ourCat.setCatColor("Brownish white");
// fam.ourDog.dogType("African Shepherd Dog");
// fam.ourDog.setDogColor("Black");
// fam.ourRabbit.setRabbitColor("White");
// fam.ourRabbit.rabbitType("Akorino");
//
// System.out.println("We own the following pets: " +
// " " + fam.ourDog.MydogType+ " " + fam.ourDog.dog_color + " in color;" +
// " a small " + fam.ourCat.cat_color + " " +fam.ourCat.MyCatType+ " cat;"+
// " and a "+ fam.ourRabbit.rabbit_color +" "+ fam.ourRabbit.MyRabbitType +
// " rabbit.");
}
}
}
//Human.java
public class Human {
protected String name;
public Human(String name){
setName(name);
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
//Adult.java
public class Adult extends Human
{
private int aAge;
public Adult(String name, int age)
{
super(name);
setAge(age);
}
@Override
public String getName()
{
return this.name;
}
public int getAge(){
return aAge;
}
public void setAge(int age){
this.aAge = age;
}
public boolean canWatchMovie(String mR)
{
return true;
}
}
//Child.java
public class Child extends Human
{
int childAge;
MovieRating mRts = new MovieRating();
String mrating;
public Child(String name, int age)
{
super(name);
this.childAge=age;
}
/*
public boolean isObjectEmpty(Object obj)
{
boolean result = obj==null;
return result;
}
*/
public void setAge(int age)
{
this.childAge = age;
}
public int getAge()
{
return childAge;
}
@Override
public String getName()
{
return this.name;
}
public boolean canWatchMovie(String mR)
{
return mRts.getStatus(this.mrating , this.childAge);
}
}
//MovieRating.java
import java.util.*;
public class MovieRating
{
private Map ageMap = new HashMap();
private String movieRating;
public MovieRating()
{
ageMap.put("PG", 18); //parental Guidance
ageMap.put("G", 4); //general viewing
ageMap.put("A", 27); //Adult material
}
public String getRating()
{
return movieRating;
}
public boolean getStatus(String rating, int age)
{
int minAge = 0;
if(rating.equalsIgnoreCase("PG"))
{
minAge = getPGuidance();
}
else if (rating.equalsIgnoreCase("G"))
{
minAge = getGeneral();
}
else if (rating.equalsIgnoreCase("A"))
{
minAge = getAdult();
}
if(age >= minAge)
{
return true;
}
else
{
return false;
}
}
public int getPGuidance()
{
// Retrieve the minimum age allowed to watch movie rated PG.
return (Integer)ageMap.get("PG");
}
public int getGeneral()
{
// Retrieve the age allowed to watch movie rated G.
return (Integer)ageMap.get("G");
}
public int getAdult()
{
// Retrieve the age allowed to watch movie rated Adult.
return (Integer)ageMap.get("A");
}
public Object getValue()
{
// return ageMap.values();
return ageMap.toString();
}
}