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?

Recommended Answers

All 13 Replies

you're trying to print the entire array, you should use a loop to run through each and every element of it, and print the elements.

for instance:

ArrayList movieGoers = ..//you already have this
String persons = "";

for ( int i = 0; i < movieGoers.size(); i++){
  if ( persons.equals(""))
     persons += (String)movieGoers.elementAt(i);
  else
    persons += ", " + (String)movieGoers.elementAt(i);
}
System.out.println("The next people can go watch the movie: " + persons);

well, I've tried to do this:

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)};
        Adult father = new Adult("John",42);
        Adult mother = new Adult("Laura",36);
        Family fam = new Family(father,mother,children);
//        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+"");
        int rating = mRt.getGeneral();
        ArrayList mgGoers= (ArrayList)fam.getMovieGoers(rating);//fam.getMovieGoers(mRt.getGeneral());
        String persons = "";
        for ( int i = 0; i < mgGoers.size(); i++)
        {
            if ( persons.equals(""))
            {
               persons += (String)mgGoers.get(i);
//               System.out.println("The next people can go watch the movie: " + persons);
            }
            else
            {
                persons += ", " + (String)mgGoers.get(i);//.elementAt(i);
                System.out.println("The next people can go watch the movie: " + persons);

            }
//            System.out.println("The next people can go watch the movie: " + persons);
        }
}

but it still doesnt print out the names of the movie goers. Could the problem be the way I've initialized the Family in the main method? Any suggestion.

String persons = "";
        for ( int i = 0; i < mgGoers.size(); i++)
        {
            if ( persons.equals(""))
            {
               persons += (String)mgGoers.get(i);
//               System.out.println("The next people can go watch the movie: " + persons);
            }
            else
            {
                persons += ", " + (String)mgGoers.get(i);//.elementAt(i);
                System.out.println("The next people can go watch the movie: " + persons);

            }
//            System.out.println("The next people can go watch the movie: " + persons);
        }

don't just copy-paste my post, I didn't check you're entire code, just your print instruction. make sure your System.out.print is outside of the loop, like this

String persons = "";
        for ( int i = 0; i < mgGoers.size(); i++)
        {
            if ( persons.equals(""))
            {
               persons += (String)mgGoers.get(i);
            }
            else
            {
                persons += ", " + (String)mgGoers.get(i);//.elementAt(i);
            }
        }
         System.out.println("The next people can go watch the movie: " + persons);

once you did this, run the app again, tell us what it is printing, what it should be printing and, if any, what error messages you get.

just saying 'it doesn't print the name of the moviegoers' is very helpfull in finding the problem.
you can also add some test prints within your if-else blocks, just to see whether they are reached, how many times, and what is added to persons.

okay, I have a Family consisting of father, mother and children. the constructor for the Family is

public Family(Adult father, Adult mother, Child[] children)
    {//something}

in the main method i have this:

Child[] children = {new Child("Frank",10), new Child("Lisa",18)};
        Adult father = new Adult("John",42);//       
        Adult mother = new Adult("Laura",36);
        Family fam = new Family(father,mother,children);

so I want to add the family members to the ArrayList in this method

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

then call this method in the main method. The parameter int rating is the minimum age allowed to watch a movie, so I expect to print out only the names of persons whose age>=rating.
But when I run the application there's no error and the names are not printed out.

does it print anything at all?
have you checked wether or not the method runs?

It doesnt print anything apart from this

run-single:
BUILD SUCCESSFUL (total time: 6 seconds)

How can I initialize the Family(father, mother,children) in the main method ; like Adult father = new Adult("John",45); and use this in the getMovieGoers method. Because earlier I was getting NullPointerException in the method.

It doesnt print anything apart from this

How can I initialize the Family(father, mother,children) in the main method ; like Adult father = new Adult("John",45); and use this in the getMovieGoers method. Because earlier I was getting NullPointerException in the method.

as far as I can tell, you're initializing it just fine
what code runs, and where are you getting that nullpointerexception?

the entire code is:

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++)
                {
                    if ( persons.equals(""))
                    {
                       persons += (String)mgGoers.get(i);
                    }
                    else
                    {
                        persons += ", " + (String)mgGoers.get(i);
                    }
                    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++)
            {
                if ( persons.equals(""))
                {
                   persons += (String)mpgGoers.get(i);
                }
                else
                {
                    persons += ", " + (String)mpgGoers.get(i);
                }
                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++)
                {
                    if ( persons.equals(""))
                    {
                       persons += (String)maGoers.get(i);
                    }
                    else
                    {
                        persons += ", " + (String)maGoers.get(i);
                    }
                    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.");

          }            
     }
}

when I run it this is what i get:\

run-single:
End
BUILD SUCCESSFUL (total time: 3 minutes 6 seconds)

But the expected output is something like this:

The next people can go watch the movie: John, Laura, Frank

and this should vary from one scenario to another, for example when the movie is rated G, PG or A.

I know what you try to get, but what I want to know is: what part of your code is running, and where did you get that Exception?
further more, this is not the entire code (or since the latest update classes like 'Father', 'Mother', 'Child',...) and such are standard in the java language.

I'll try to check your other classes in your previous posts, but have you made any changes in them since you posted them? if so, repost if you can.

The Family is composed of the father and mother (both class Adult ), and children(Child class)

public class Adult extends Human
{
    public Adult(String name, int age)
    {
        super(name);
        this.age=age;
    }
    protected String getName()
    {
       return this.name;
    }
    public int getAge(int mAge)
    {
        return this.age=mAge;
    }
    public boolean canWatchMovie(String mR) 
    {
        return true;
    }       
}

there is also MovieRating class

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

Child class

public class Child extends Human
{
    int childAge; 
    MovieRating mRts = new MovieRating();
    String mrating;

    public Child(String name, int age)
    {
        super(name); 
        this.age=age;
    }

    public boolean isObjectEmpty(Object obj)
    {
        boolean result = obj==null;
        return result;
    }

    public void setAge(int age)
    {
       childAge = age;
    }

    public int getAge(int mAge) 
    { 
        return this.age=mAge;
    }
    public String getName()
    {
        return this.name;
    }
    public boolean canWatchMovie(String mR) 
    {
       return mRts.getStatus(this.mrating , this.age);
    }
}

So the getMovieGoers method take rating as a parameter and the rating is checked by the MovieRating class, that's why I have something like this getMovieGoers(mRt.getGeneral) for movies rated G and getMovieGoers(mRt.getAdult) for movies rated A.

and what's in Human.java?

just a couple of remarks:

what's the use of setting getName() in adult as protected?
also, just check the two codes for Child.java I post under this, the first one is the one you posted, the second one is (IMO) a bit more correct

//Your current code
public class Child extends Human
{
    int childAge; 
    MovieRating mRts = new MovieRating();
    String mrating;
   
    public Child(String name, int age)
    {
        super(name); 
        this.age=age;
    }

    public boolean isObjectEmpty(Object obj)
    {
        boolean result = obj==null;
        return result;
    }
    
    public void setAge(int age)
    {
       childAge = age;
    }

    public int getAge(int mAge)	
    { 
        return this.age=mAge;
    }
    public String getName()
    {
        return this.name;
    }
    public boolean canWatchMovie(String mR) 
    {
       return mRts.getStatus(this.mrating , this.age);
    }
}
//My revision
public class Child extends Human
{
    int childAge; 
    MovieRating mRts = new MovieRating();
    String mrating;
   
    public Child(String name, int age)
    {
        super(name); 
        this.childAge=age;
    }
    // I'm not sure what this is supposed to do. is it supposed to check the current object? 
   // if not, it doesn't really belong in Child.java 
    public boolean isObjectEmpty(Object obj)
    {
        boolean result = obj==null;
        return result;
    }
    
    public void setAge(int age)
    {
       this.childAge = age;
    }

    public int getAge() // No argument needed for a getter, the data's already here	
    { 
        return childAge;
    }
    public String getName()
    {
        return this.name;
    }
    public boolean canWatchMovie(String mR) 
    {
       return mRts.getStatus(this.mrating , this.childAge); 
    }
}
commented: that's good. Thanks a lot +1

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