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 mAge,gAge,aAge; mAge = Integer.getInteger((String) ageMap.get("PG")); gAge = Integer.getInteger((String) ageMap.get("G")); aAge = Integer.getInteger((String) ageMap.get("G")); if (age < mAge ) { return false; } else if (age >= mAge) { return true; } 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(); } }
public void getMovieGoers(MovieRating mRt) { Adult a = new Adult("Robert",33); Adult b = new Adult("Sue",27); Adult c = new Adult("Tracy",20); ArrayList ads = new ArrayList(); ads.add(a); ads.add(b);ads.add(c); Collection myAdults =ads; if(mRt.getRating()!=null) { try { if(ourChild.getAge()< mRt.getAdult() || ourChild.getAge()<mRt.getPGuidance() ) { System.out.println(ourChild.getName()+" cannot watch movie rated A, but can watch"+ " movie rated PG under parental advice. The following adults can watch: "+ ""+myAdults+""); } else if(ourChild.getAge()>= mRt.getPGuidance() && ourChild.getAge()<mRt.getAdult()) { System.out.println(ourChild.getName()+" can freely watch the movie rated PG."+ " but cannot watch movies rated A. The following can watch both: "+myAdults+""); } else { System.out.println("The following can watch movies rated G: "+ourChild.getName()+ "," +myAdults+""); } } catch(Exception e) { System.out.println("Movie rating not found."); } } }
well... you're code is quite unreadable. please try using code tags, it 'll make it a lot easier.
you also go through a lot of effort, using HashMaps while simple int variables would suffice, but I guess that's up to you to choose.
lastly, in the method you want us to check, you use the variable ourChild, while you don't show a declaration or set a value to it in the code you show, so please show the entire code, so we know what we're dealing with.
also, don't it's no use creating the Collection myAdults. you already have that Collection, called ads
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 mAge,gAge,aAge; mAge = Integer.getInteger((String) ageMap.get("PG")); gAge = Integer.getInteger((String) ageMap.get("G")); aAge = Integer.getInteger((String) ageMap.get("A")); if (age < mAge ) { return false; } else if (age >= mAge) { return true; } else if(age<aAge) { return false; } else if(age>=aAge) { return true; } else if(age<gAge) { return false; } else if(age>=gAge) { return true; } 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(); } }
class Child extends Human { int childAge; public Child(String name, int age) { super(name); this.age=age; } public boolean isObjectEmpty(Object obj) { boolean result = obj==null; return result; } @Override public void setAge(int age) { childAge = age; } @Override public int getAge() { return this.age; } public String getName() { return this.name; } public boolean canWatchMovie(MovieRating mRt) { return mRt.getStatus(this.mrating , this.age); } public static void main(String args[]) { Child ch = new Child("Joe",7); } }
import java.util.*; import java.io.*; public class Family { private Dog ourDog; private Cats ourCat; private Rabbit ourRabbit; private Adult adults; String me,myPatner; private Child ourChild; int Age; public Family(String myName, String patner) { /* * uses 'this' to refer to * the current object being manipulated */ this.me =myName; this.myPatner=patner; adults = new Adult("Robert",26); ourChild=new Child("Joe",7); ourDog=new Dog(""); ourCat=new Cats(""); ourRabbit = new Rabbit(""); } public void getMovieGoers(MovieRating mRt) { Adult a = new Adult("Robert",33); Adult b = new Adult("Sue",27); Adult c = new Adult("Tracy",20); ArrayList ads = new ArrayList(); ads.add(a); ads.add(b);ads.add(c); Collection myAdults =ads; if(mRt.getRating()!=null) { try { if(ourChild.getAge()< mRt.getAdult() || ourChild.getAge()<mRt.getPGuidance() ) { System.out.println(ourChild.getName()+" cannot watch movie rated A, but can watch"+ " movie rated PG under parental advice. The following adults can watch: "+ ""+myAdults+""); } else if(ourChild.getAge()>= mRt.getPGuidance() && ourChild.getAge()<mRt.getAdult()) { System.out.println(ourChild.getName()+" can freely watch the movie rated PG."+ " but cannot watch movies rated A. The following can watch both: "+myAdults+""); } else { System.out.println("The following can watch movies rated G: "+ourChild.getName()+ "," +myAdults+""); } } catch(Exception e) { System.out.println("Movie rating not found."); } } } public static void main(String args[])throws IOException { Adult ad = new Adult("Robert",26); MovieRating mRt = new MovieRating(); /* * new instance of the Family */ Family fam= new Family("Robert","Mrembo"); /* * set string values to variables */ 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("Hi, I'm "+ad.getName()+" and my partner is Mrs Robert" + "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."); fam.getMovieGoers(mRt); } }
class Adult extends Human { String movieRating,aName,pName; int mAge; public Adult(String name, int age) { super(name); this.age=age; } protected String getName() { return this.name; } @Override public int getAge() { return this.age; } protected void getPatnerName(String partnerName) { pName = partnerName; } @Override public boolean canWatchMovie(MovieRating mRt) { return true; } public static void main(String args[]) { Adult me = new Adult("Robert",26); System.out.println("I'm " + me.getName() + " and i'm "+ me.getAge() + " years old" + " that means I'm an Adult"); } }
public class Family{ private Adult father; private Adult mother; private Child[] children; public Family(Adult father, Adult mother, Children[] children){ setFather(father); setMother(mother); setChildren(children); } public Adult getFather(){ return this.father;} public Adult getMother(){ return this.mother;} public Child[] getChildren(){ return this.children;} public void setFather(Adult father){ this.father = father;} public void setMother(Adult mother) { this.mother = mother;} public void setChildren(Child[] children){ this.children = children;} }
well there's a getMovieGoers method in the Family class that takes a rating as a parameter and returns a collection of all the family members that can watch the movie. This method is to be called in the main method of family.
... putlic static void main(String args[]){ Movie movie = new Movie(); Children[] children = {new Child("Frank",10), new Child("Lisa",18)}; Family one = new Family(new Adult("John",37), new Adult("Mary",35), ArrayList visitors = (ArrayList)one.getMovieGoers(movie.getRating()); }
public Collection getMovieGoers(int rating){ // do not go and create Adults or children here, use your class var's Collection returnValue = new Collection(); // I consider rating here to be the starting age that is allowed for the movie if ( father.getAge() >= rating) returnValue.add(father); if ( mother.getAge() >= rating) returnValue.add(mother); for(int i = 0; i < children.length; i++){ if (children[i].getAge() >= rating) returnValue.add(children[i]); } return returnValue; }
| DaniWeb Message | |
| Cancel Changes | |