I have some problem with this question:
my Family class is as below:
import java.util.ArrayList;
import java.util.Collection;
import java.io.*;
public class Family
{
private Adult father;
private Adult mother;
private Child[] children;
private Dog ourDog;
private Cats ourCat;
private Rabbit ourRabbit;
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)
{
ArrayList mGoers = new ArrayList();
// 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();
Child[] children = {new Child("Frank",10), new Child("Lisa",18)};
Family fam = new Family(new Adult("John",37), new Adult("Mary",35),children);
//List Movie goers who can watch a movie rated G
ArrayList mgGoers= (ArrayList)fam.getMovieGoers(mRt.getGeneral());
System.out.println("The following can watch movies rated G: "+mgGoers+"");
//List Movie goers who can watch a movie rated PG
ArrayList mpGoers= (ArrayList)fam.getMovieGoers(mRt.getPGuidance());
System.out.println("The following can watch movies rated PG: "+mpGoers+"");
//List Movie goers who can watch a movie rated A
ArrayList maGoers= (ArrayList)fam.getMovieGoers(mRt.getAdult());
System.out.println("The following can watch movies rated A: "+maGoers+"");
//pets
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.");
}
}
and MovieRating class is as:
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();
}
}
So the question was:
Add a getMovieGoers method to the family that takes a rating as a parameter and returns a collection of all the family members that can watch the movie.
However, when I run my application I get the output as
The following can watch movies rated G: [myinheritancepolymorph.Adult@1e63e3d, myinheritancepolymorph.Adult@1004901, myinheritancepolymorph.Child@1b90b39, myinheritancepolymorph.Child@18fe7c3]
The following can watch movies rated PG: [myinheritancepolymorph.Adult@1e63e3d, myinheritancepolymorph.Adult@1004901, myinheritancepolymorph.Child@18fe7c3]
The following can watch movies rated A: [myinheritancepolymorph.Adult@1e63e3d, myinheritancepolymorph.Adult@1004901]
We own the following pets: African Shepherd Dog Black in color; a small Brownish white Angora cat; and a White Akorino rabbit.
BUILD SUCCESSFUL (total time: 9 seconds)
But I want it to print only the names of Family members who can watch a movie. Some urgent help please?