943,909 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 9510
  • Java RSS
Oct 24th, 2008
0

using Map in java

Expand Post »
Hi, I'm trying to use a map in my application. the class MovieRating which contains the map is as below:
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class MovieRating
  4. {
  5. private Map ageMap = new HashMap();
  6. private String movieRating;
  7. public MovieRating()
  8. {
  9. ageMap.put("PG", 18); //parental Guidance
  10. ageMap.put("G", 4); //general viewing
  11. ageMap.put("A", 27); //Adult material
  12. }
  13. public String getRating()
  14. {
  15. return movieRating;
  16. }
  17.  
  18. public boolean getStatus(String rating, int age)
  19. {
  20. int mAge,gAge,aAge;
  21. mAge = Integer.getInteger((String) ageMap.get("PG"));
  22. gAge = Integer.getInteger((String) ageMap.get("G"));
  23. aAge = Integer.getInteger((String) ageMap.get("G"));
  24. if (age < mAge )
  25. {
  26. return false;
  27. }
  28. else if (age >= mAge)
  29. {
  30. return true;
  31. }
  32. return false;
  33. }
  34. public int getPGuidance()
  35. {
  36. // Retrieve the minimum age allowed to watch movie rated PG.
  37. return (Integer)ageMap.get("PG");
  38. }
  39. public int getGeneral()
  40. {
  41. // Retrieve the age allowed to watch movie rated G.
  42. return (Integer)ageMap.get("G");
  43. }
  44. public int getAdult()
  45. {
  46. // Retrieve the age allowed to watch movie rated Adult.
  47. return (Integer)ageMap.get("A");
  48. }
  49.  
  50. public Object getValue()
  51. {
  52. // return ageMap.values();
  53. return ageMap.toString();
  54. }
  55.  
  56. }
I now want to use the three ratings and age in the map in a method in a different class to determine whether the various individuals of various age groups can watch a certain movie or not. the method is called getMovieGoers and is as below: NOTE: This method is in a different class called Family within the same package. It returns a collection of moviegoers from the arraylist.
Java Syntax (Toggle Plain Text)
  1. public void getMovieGoers(MovieRating mRt)
  2. {
  3. Adult a = new Adult("Robert",33);
  4. Adult b = new Adult("Sue",27);
  5. Adult c = new Adult("Tracy",20);
  6. ArrayList ads = new ArrayList();
  7. ads.add(a); ads.add(b);ads.add(c);
  8. Collection myAdults =ads;
  9. if(mRt.getRating()!=null)
  10. {
  11. try
  12. {
  13. if(ourChild.getAge()< mRt.getAdult() || ourChild.getAge()<mRt.getPGuidance() )
  14. {
  15. System.out.println(ourChild.getName()+" cannot watch movie rated A, but can watch"+
  16. " movie rated PG under parental advice. The following adults can watch: "+
  17. ""+myAdults+"");
  18. }
  19. else if(ourChild.getAge()>= mRt.getPGuidance() && ourChild.getAge()<mRt.getAdult())
  20. {
  21. System.out.println(ourChild.getName()+" can freely watch the movie rated PG."+
  22. " but cannot watch movies rated A. The following can watch both: "+myAdults+"");
  23. }
  24. else
  25. {
  26. System.out.println("The following can watch movies rated G: "+ourChild.getName()+
  27. "," +myAdults+"");
  28. }
  29.  
  30. }
  31. catch(Exception e)
  32. {
  33. System.out.println("Movie rating not found.");
  34. }
  35.  
  36. }
  37. }
When I run my application and call the method in the main, it does not list the moviegoers and I suspect the problem is picking the rating and age pair from the map in the MovieRating class. Some help please.....
Last edited by ~s.o.s~; Oct 24th, 2008 at 6:55 am. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 24th, 2008
0

Re: using Map in java

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
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Oct 24th, 2008
0

Re: using Map in java

Click to Expand / Collapse  Quote originally posted by stultuske ...
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
MovieRating class

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class MovieRating
  4. {
  5. private Map ageMap = new HashMap();
  6. private String movieRating;
  7. public MovieRating()
  8. {
  9. ageMap.put("PG", 18); //parental Guidance
  10. ageMap.put("G", 4); //general viewing
  11. ageMap.put("A", 27); //Adult material
  12. }
  13. public String getRating()
  14. {
  15. return movieRating;
  16. }
  17.  
  18. public boolean getStatus(String rating, int age)
  19. {
  20. int mAge,gAge,aAge;
  21. mAge = Integer.getInteger((String) ageMap.get("PG"));
  22. gAge = Integer.getInteger((String) ageMap.get("G"));
  23. aAge = Integer.getInteger((String) ageMap.get("A"));
  24. if (age < mAge )
  25. {
  26. return false;
  27. }
  28. else if (age >= mAge)
  29. {
  30. return true;
  31. }
  32. else if(age<aAge)
  33. {
  34. return false;
  35. }
  36. else if(age>=aAge)
  37. {
  38. return true;
  39. }
  40. else if(age<gAge)
  41. {
  42. return false;
  43. }
  44. else if(age>=gAge)
  45. {
  46. return true;
  47. }
  48. return false;
  49. }
  50. public int getPGuidance()
  51. {
  52. // Retrieve the minimum age allowed to watch movie rated PG.
  53. return (Integer)ageMap.get("PG");
  54. }
  55. public int getGeneral()
  56. {
  57. // Retrieve the age allowed to watch movie rated G.
  58. return (Integer)ageMap.get("G");
  59. }
  60. public int getAdult()
  61. {
  62. // Retrieve the age allowed to watch movie rated Adult.
  63. return (Integer)ageMap.get("A");
  64. }
  65.  
  66. public Object getValue()
  67. {
  68. // return ageMap.values();
  69. return ageMap.toString();
  70. }
  71. }


Child class


Java Syntax (Toggle Plain Text)
  1. class Child extends Human
  2. {
  3. int childAge;
  4.  
  5.  
  6. public Child(String name, int age)
  7. {
  8. super(name);
  9. this.age=age;
  10. }
  11.  
  12. public boolean isObjectEmpty(Object obj)
  13. {
  14. boolean result = obj==null;
  15. return result;
  16. }
  17.  
  18. @Override
  19. public void setAge(int age)
  20. {
  21. childAge = age;
  22. }
  23. @Override
  24. public int getAge()
  25. {
  26. return this.age;
  27. }
  28. public String getName()
  29. {
  30. return this.name;
  31. }
  32. public boolean canWatchMovie(MovieRating mRt)
  33. {
  34. return mRt.getStatus(this.mrating , this.age);
  35. }
  36.  
  37. public static void main(String args[])
  38. {
  39. Child ch = new Child("Joe",7);
  40. }
  41. }


Family class

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3. public class Family
  4. {
  5. private Dog ourDog;
  6. private Cats ourCat;
  7. private Rabbit ourRabbit;
  8. private Adult adults;
  9.  
  10. String me,myPatner;
  11. private Child ourChild;
  12. int Age;
  13.  
  14. public Family(String myName, String patner)
  15. {
  16. /*
  17.   * uses 'this' to refer to
  18.   * the current object being manipulated
  19.   */
  20. this.me =myName;
  21. this.myPatner=patner;
  22. adults = new Adult("Robert",26);
  23. ourChild=new Child("Joe",7);
  24. ourDog=new Dog("");
  25. ourCat=new Cats("");
  26. ourRabbit = new Rabbit("");
  27.  
  28. }
  29.  
  30. public void getMovieGoers(MovieRating mRt)
  31. {
  32. Adult a = new Adult("Robert",33);
  33. Adult b = new Adult("Sue",27);
  34. Adult c = new Adult("Tracy",20);
  35. ArrayList ads = new ArrayList();
  36. ads.add(a); ads.add(b);ads.add(c);
  37. Collection myAdults =ads;
  38. if(mRt.getRating()!=null)
  39. {
  40. try
  41. {
  42. if(ourChild.getAge()< mRt.getAdult() || ourChild.getAge()<mRt.getPGuidance() )
  43. {
  44. System.out.println(ourChild.getName()+" cannot watch movie rated A, but can watch"+
  45. " movie rated PG under parental advice. The following adults can watch: "+
  46. ""+myAdults+"");
  47. }
  48. else if(ourChild.getAge()>= mRt.getPGuidance() && ourChild.getAge()<mRt.getAdult())
  49. {
  50. System.out.println(ourChild.getName()+" can freely watch the movie rated PG."+
  51. " but cannot watch movies rated A. The following can watch both: "+myAdults+"");
  52. }
  53. else
  54. {
  55. System.out.println("The following can watch movies rated G: "+ourChild.getName()+
  56. "," +myAdults+"");
  57. }
  58.  
  59. }
  60. catch(Exception e)
  61. {
  62. System.out.println("Movie rating not found.");
  63. }
  64.  
  65. }
  66. }
  67.  
  68. public static void main(String args[])throws IOException
  69. {
  70. Adult ad = new Adult("Robert",26);
  71. MovieRating mRt = new MovieRating();
  72. /*
  73.   * new instance of the Family
  74.   */
  75. Family fam= new Family("Robert","Mrembo");
  76.  
  77. /*
  78.   * set string values to variables
  79.   */
  80. fam.ourCat.catType("Angora");
  81. fam.ourCat.setCatColor("Brownish white");
  82. fam.ourDog.dogType("African Shepherd Dog");
  83. fam.ourDog.setDogColor("Black");
  84. fam.ourRabbit.setRabbitColor("White");
  85. fam.ourRabbit.rabbitType("Akorino");
  86.  
  87. System.out.println("Hi, I'm "+ad.getName()+" and my partner is Mrs Robert" +
  88. "We own the following pets: " +
  89. " " + fam.ourDog.MydogType+ " " + fam.ourDog.dog_color + " in color;" +
  90. " a small " + fam.ourCat.cat_color + " " +fam.ourCat.MyCatType+ " cat;"+
  91. " and a "+ fam.ourRabbit.rabbit_color +" "+ fam.ourRabbit.MyRabbitType +
  92. " rabbit.");
  93. fam.getMovieGoers(mRt);
  94. }
  95. }

NOTE: Don't worry about the Cat, Dog and Rabbit classes. They are not included here.


Adult class

Java Syntax (Toggle Plain Text)
  1. class Adult extends Human
  2. {
  3. String movieRating,aName,pName;
  4. int mAge;
  5. public Adult(String name, int age)
  6. {
  7. super(name);
  8. this.age=age;
  9. }
  10. protected String getName()
  11. {
  12. return this.name;
  13. }
  14. @Override
  15. public int getAge()
  16. {
  17. return this.age;
  18. }
  19. protected void getPatnerName(String partnerName)
  20. {
  21. pName = partnerName;
  22. }
  23.  
  24. @Override
  25. public boolean canWatchMovie(MovieRating mRt)
  26. {
  27. return true;
  28. }
  29. public static void main(String args[])
  30. {
  31. Adult me = new Adult("Robert",26);
  32. System.out.println("I'm " + me.getName() + " and i'm "+ me.getAge() + " years old" +
  33. " that means I'm an Adult");
  34. }
  35.  
  36.  
  37. }

My main problem is the method getMovieGoers(MovieRating mRt) in Family class. and how to use rating as the key and minimum age as the value from the Map in this method to get movie goers for each rating.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 24th, 2008
0

Re: using Map in java

I think you'd do better to first learn the basic logic of OO programming.
you're Family class, for instance:

you'd have a Family with just one adult, with (max) one child, ...
next to that, the names 'me' and 'myName' are a bit ... confusing.
or, of course, you want to be able to create just one family (yours), or you want to be part of every family.

in a better implementation, a more OO approach, your Family.java should look more like this:

Java Syntax (Toggle Plain Text)
  1. public class Family{
  2.  
  3. private Adult father;
  4. private Adult mother;
  5.  
  6. private Child[] children;
  7.  
  8. public Family(Adult father, Adult mother, Children[] children){
  9. setFather(father);
  10. setMother(mother);
  11. setChildren(children);
  12. }
  13.  
  14. public Adult getFather(){ return this.father;}
  15. public Adult getMother(){ return this.mother;}
  16. public Child[] getChildren(){ return this.children;}
  17.  
  18. public void setFather(Adult father){ this.father = father;}
  19. public void setMother(Adult mother) { this.mother = mother;}
  20. public void setChildren(Child[] children){ this.children = children;}
  21.  
  22. }

this is about all there has to be in it. about the watchin movies or not, that does not belong in family, that should be in the class that uses family
Last edited by stultuske; Oct 24th, 2008 at 7:49 am. Reason: damn tab
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Oct 24th, 2008
0

Re: using Map in java

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 24th, 2008
0

Re: using Map in java

The rating is to be picked from the Map in MovieRating class, because there are some movies which can oly be watched by Adults or Children of certain age. I hope it's not confusing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 24th, 2008
0

Re: using Map in java

Click to Expand / Collapse  Quote originally posted by Achupa ...
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.
well, than you're better of with an approach like this:
java Syntax (Toggle Plain Text)
  1. ...
  2. putlic static void main(String args[]){
  3. Movie movie = new Movie();
  4. Children[] children = {new Child("Frank",10), new Child("Lisa",18)};
  5. Family one = new Family(new Adult("John",37), new Adult("Mary",35),
  6. ArrayList visitors = (ArrayList)one.getMovieGoers(movie.getRating());
  7.  
  8. }

and in the actual Family class:
java Syntax (Toggle Plain Text)
  1. public Collection getMovieGoers(int rating){
  2. // do not go and create Adults or children here, use your class var's
  3. Collection returnValue = new Collection();
  4. // I consider rating here to be the starting age that is allowed for the movie
  5. if ( father.getAge() >= rating)
  6. returnValue.add(father);
  7. if ( mother.getAge() >= rating)
  8. returnValue.add(mother);
  9. for(int i = 0; i < children.length; i++){
  10. if (children[i].getAge() >= rating)
  11. returnValue.add(children[i]);
  12. }
  13. return returnValue;
  14. }
Last edited by ~s.o.s~; Oct 24th, 2008 at 10:42 am. Reason: Fixed code tags.
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Fetching data as Arabic from database??
Next Thread in Java Forum Timeline: linear programming formulation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC