aspose file tools*
The moose likes Beginning Java and the fly likes Equals Method, greatherThan lessThan methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals Method, greatherThan lessThan methods" Watch "Equals Method, greatherThan lessThan methods" New topic
Author

Equals Method, greatherThan lessThan methods

Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
I attached the problem statement below.

Here is the part I am having trouble with:

• A greaterThan method that accepts a Month object as an argument. If the calling object's monthNumber field is greater than the argument's monthNumber field, this method should return true. Otherwise it should return false.
• A lessThan method that accepts a Month object as an argument. If the calling object's monthNumber field is less than the argument's monthNumber field, this method should return true. Otherwise it should return false.



I know that I am going entirely wrong somewhere here. I have been searching all of the web and I can't find anything that is really helpful with this. Any pointers would be greatly appreciated because I am going crazy here! lol
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

first of all,why do you need two methods?
Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
seetharaman venkatasamy wrote:first of all,why do you need two methods?


Not sure that is just what the professor asked for.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
seetharaman venkatasamy wrote:first of all,why do you need two methods?


It's because

!lessThan != greaterThan

Instead,

!lessThat == greaterThan || equal
Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
Also I tried commenting it out to see how the rest of my code is and I am getting a "Missing return statement error" I have combed over it about 3 different times and I can not find any missing return statements!

Edit: snipped code to shorten length. I have a new version of it posted below.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
'Sir Yak':

Never, never, never do this:

Do this instead:

Also, I would suggest that you change your user name. This site has a pretty strict policy about user names matching real names, and the moderators will ping you.

John.
Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
John de Michele wrote:'Sir Yak':

Never, never, never do this:

Do this instead:

Also, I would suggest that you change your user name. This site has a pretty strict policy about user names matching real names, and the moderators will ping you.

John.


Thanks for the heads up. I fixed it now. Anyway my prof finally got back to me and basically told me delete that and do it this way:


I have everything fixed except I still can not figure out what is causing this "missing return statement error."

Here is the complete class file:



Also I wanted to thank you guys for helping me out! I actually just came across this forum by accident today while searching for help.

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Colby:

You'll probably want to change that to:

The greaterThan() and lessThan() methods should also be similarly changed. There's no real point in doing an explicit 'return true' or 'return false', since doing a relational operator check already returns a boolean.

As for the error message, can you post the actual stack trace returned? That would be helpful.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to JavaRanch and thank you for correcting the naming issue before any of us mods found out

You are quite right to produce two methods "because the professor told us to". If you are told to produce two methods in an assignment then you need two methods to get full marks. Even if you hadn't seen Embla Tingeling's explanation.

Your constructor which takes the name of the month will never work; the == operator on reference types will not do what you think it does, and you are liable to find monthNumber == 0 at the end of it.
Your constructor with no arguments does nothing useful; in fact it does something potentially harmful, viz choosing no 1 as a default. Get rid of that constructor.
You never use the "month" field. Delete it.
You never use the monthName field. You should set it up in your constructor. If you know how to use arrays, it would be better to put the names into an array and pull the month name from the number. If you are using set methods you should set the month name and number together.

Have you really been told to create an equals() method taking Month as a parameter? The equals() method should take Object as a parameter.

You didn't take John de Michele's hint about the format of a predicate method. Compare your methods with this style guide (§10.5.2).

And please use thread titles which tell us what the thread is about.
Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
Campbell Ritchie wrote:Welcome to JavaRanch and thank you for correcting the naming issue before any of us mods found out

You are quite right to produce two methods "because the professor told us to". If you are told to produce two methods in an assignment then you need two methods to get full marks. Even if you hadn't seen Embla Tingeling's explanation.

Your constructor which takes the name of the month will never work; the == operator on reference types will not do what you think it does, and you are liable to find monthNumber == 0 at the end of it.
Your constructor with no arguments does nothing useful; in fact it does something potentially harmful, viz choosing no 1 as a default. Get rid of that constructor.
You never use the "month" field. Delete it.
You never use the monthName field. You should set it up in your constructor. If you know how to use arrays, it would be better to put the names into an array and pull the month name from the number. If you are using set methods you should set the month name and number together.

Have you really been told to create an equals() method taking Month as a parameter? The equals() method should take Object as a parameter.

You didn't take John de Michele's hint about the format of a predicate method. Compare your methods with this style guide (§10.5.2).

And please use thread titles which tell us what the thread is about.


Well the thing is the professor was VERY specific about what he wanted to see in this program. He told me what kind of constructors, methods, etc. that he wanted to see. He gave us the demo programs that are designed to work with this class. I do appreciate the tips though. I will try to keep them in mind in the future whenever I am writing a program that is not so specific. I see what you mean about the month variable. Not really sure why I made that. On the equals method yes he specifically said to use Month as the argument.

For anyone that reads this in the future the problem with my problem above is that I didn't have a return statement for my String method. I had to go back and assign all of the month names to my monthName variable and then return monthName at the end. This gave me the return statement it was looking for. Thank you all for your help!

I have also went back and changed my title to be more descriptive. This is my first day here on this forum, but I'm learning.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Colby:

When you said your professor was very specific, did he just give you method signatures, or the code in the methods, too? You might want to ask him about it.

John.
Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
John de Michele wrote:Colby:

When you said your professor was very specific, did he just give you method signatures, or the code in the methods, too? You might want to ask him about it.

John.

He told us exactly what he wanted. For example a no-arg Constructor that sets monthNumber to 1. A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than 1 or greater than 12 is passed, the constructor should set monthNumber to 1.

So basically he told us what constructors or methods he wanted and what he wanted in them.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Even though it looks like questionable design to me, if that's what is in the question, that's what's in the question and you will have to fulfill what's in the question to get full marks. Maybe if you tell him somebody queried a "default to 1" policy, you can get into a discussion with him about it. Error-trapping is a slightly more advanced topic which you have presumably not reached yet.
Did he tell you to create an equals(Month) method rather than equals(Object)? Warning about equals:
One would expect that overriding equals() , since it is a fairly common task, should be a piece of cake. The reality is far from that.
(From Angelika Langer, whose website you would do well to remember).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
By the way, have you worked out yet why == applied to those month names doesn't work?
Colby Allen
Greenhorn

Joined: Nov 11, 2009
Posts: 9
Campbell Ritchie wrote:Even though it looks like questionable design to me, if that's what is in the question, that's what's in the question and you will have to fulfill what's in the question to get full marks. Maybe if you tell him somebody queried a "default to 1" policy, you can get into a discussion with him about it. Error-trapping is a slightly more advanced topic which you have presumably not reached yet.
Did he tell you to create an equals(Month) method rather than equals(Object)? Warning about equals:
One would expect that overriding equals() , since it is a fairly common task, should be a piece of cake. The reality is far from that.
(From Angelika Langer, whose website you would do well to remember).


He said to pass a Month object as an argument.

Campbell Ritchie wrote:By the way, have you worked out yet why == applied to those month names doesn't work?


That one is working as far as I know. Actually what I have worked with all 3 of his demo programs. Whether it is right or not I don't know, but it works. It is too late to change anything now though because I had to turn it in at midnight last night.(Although as I stated above pointers are always welcome for future reference)
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

just a though:




to get Month name:


to get Month index:



doing so reduces code to half the size...


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
 
subject: Equals Method, greatherThan lessThan methods