| | |
Printing output of an array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 1
The Family is composed of the father and mother (both class Adult ), and children(Child class)
[code]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;
}
}
[code]
there is also MovieRating class
Child class
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.
[code]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;
}
}
[code]
there is also MovieRating class
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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.
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
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
Java Syntax (Toggle Plain Text)
//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); } }
Java Syntax (Toggle Plain Text)
//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); } }
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:
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:
Java Syntax (Toggle Plain Text)
//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."); } } }
Java Syntax (Toggle Plain Text)
//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; } }
Java Syntax (Toggle Plain Text)
//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; } }
Java Syntax (Toggle Plain Text)
//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); } }
Java Syntax (Toggle Plain Text)
//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(); } }
![]() |
Similar Threads
- Read_One_Value Array (C++)
- Dynamic Array Help (C)
- reading in from vector to a two dimensional array (C)
- Multidimensional array sort problem (C++)
- Working with array of files (C++)
- MERGED: Deleting duplicates in an array (plz help me out!!!!!!!) (C)
- How do I create a program using an Array ? (C++)
Other Threads in the Java Forum
- Previous Thread: Help with mastermind game
- Next Thread: Weird Question
Views: 765 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows





