• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with this class  RSS feed

 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a class:
Create a MovieRating class that contains a private map with rating as the key and minimum age as the value, populate this map when the class is instantiated. Also include a method which takes two parameters, rating and age and checks the minimum age in the map and returns true or false to indicate whether or not a human with the specified age is allowed to watch a movie with the specified rating.

I have tried this and get the following:


But I am aware there are a lot of errors in this. First of all when I try to compile this I get this error:
C:\NETBEANSProjects\src\Human\MovieRating.java:16: <identifier> expected
humanMap.put(5, 18);
1 error

Can anybody explain what this error means?
Thanks
Ricky
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside a class you can only declare variables and methods. You cannot make calls to methods like what you are trying to do. If you need to call a method when your class is instantiated, put it inside your constructor.
And if you don't know what a constructor is, it's basically a method that is called whenever you make a new instance of your class. If you say new nameOfClass(); the constructor runs. When you don't write a constructor, one is provided for you (free of charge). The method doesn't return anything, so you don't declare a return value. And the name of the method must match the name of the class EXACTLY. So in your case you would declare the constructor:

[ March 31, 2007: Message edited by: Eric Daly ]
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Daly:
[QB]Inside a class you can only declare variables and methods. You cannot make calls to methods like what you are trying to do. If you need to call a method when your class is instantiated, put it inside your constructor.
And if you don't know what a constructor is, it's basically a method that is called whenever you make a new instance of your class. If you say new nameOfClass(); the constructor runs. When you don't write a constructor, one is provided for you (free of charge). The method doesn't return anything, so you don't declare a return value. And the name of the method must match the name of the class EXACTLY. So in your case you would declare the constructor:


Thanks for the reply Eric.

I re-tried the code but when I try to compile it now I get this error:
missing return statement




I was trying to get the value of the key "PG" into minAge which is int.

Can you please tell me how I can do that?

Thanks
Ricky
[ March 31, 2007: Message edited by: Ricky James ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line doesn't seem right:


The result from the hash is an Object that is not a String but an Integer. Cast it as an Integer, and you'll have better luck. Also, are you sure you want to say .get("PG") and not use your method's rating parameter?

Another comment: As a general rule, I like to have one single return statement at the end of my methods. Jumping out of a method in the middle w/ one of many returns makes it harder to debug (at least for me). I often create a temp variable of the same type as the return type early on in the method, set it to a preliminary value (here it would be a boolean tempBool = false ; ) and then change it's value as is necessary before returning it at the end of the method.

Last comment (I think): Are you allowed to use generics? If so, I highly recommend that you use something like this:

[ March 31, 2007: Message edited by: pete stein ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!