wood burning stoves*
The moose likes Beginning Java and the fly likes Comparing ages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing ages" Watch "Comparing ages" New topic
Author

Comparing ages

Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21


This is my method to compare ages of two people, coditions are thatwhen a male is under 26 the female must only be 1 year older then him, if over 26 then it does not matter the (answer == true) bit is continued troughout for a condition in the while loop, if that is false it will return that the pair are not a match.

what i want this to do is when i enter the age for a man if it is under26 and i enter a womens age that is 28 or over it will return that the pair are not a match but when i run it, it says they are a match, is their a mistake in that peice of code or is it in the other part, thanks
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I absolutly hate big ugly if statements like that, and try to avoid them anywhere I can. Maybe something more like this:

that, to me, looks simpler. In general simpler means better. Does that do what you are looking for?
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
i got it workin, where i had false i put in true and where is had true i put in false, i works now.

ie
where i had (answer = true) i changed to (answer = false)

but i did not change it where i had (answer == true)

its all good, my code works for the program but is hugely long, i will change it tomorrow, put in few loops, but i have what i want down and its all goo. as long as it works i am happy. i have been so stressed. and everyone has been great to me. Thank you everyone.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
having it working is, of course, the most important thing. The point behind making the if statements smaller, less complex, is so that when you come back to it in a week or a few months, it doesn't take you an hour to figure out what you were doing.

glad you got it working.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Here are a couple of things that might make this easier.

First, readability. Try to think of simple methods you can add to your Person class to let the Person control the details. For example, you haveWhat happens when you change gender to store "M" or "F" or turn it into an integer using constants? Sure, you won't do that in this program, but if you get used to coding defensively, your design will improve.

The Person class is just begging for "boolean isMale()" and "boolean isFemale()"

Second, from what I recall of your ealier description, two people are compatible if and only if one is male and the other is female. So, once you pass that first test, you know you've got one male and one female. Instead of contantly checking which is which, why not set up two local variables to hold them?

Notice how each test has two mirrored if-tests depending on whether "this" happens to be the male or female. This will simplify your if tests later on.Remember, all great programmers are lazy at heart and love to save typing, even if they have to spend hours thinking about a way to save that time. Make sure to cultivate this "skill."

Now, you may be thinking, but "this" is one Person and "N" is the other (What does N that mean?), and you can't very well switch "this". True, that's why you declare two new local Person references. You'll assign "this" and "N" to these new references and use them instead.

In any case, you said you've got it working so just consider these points for future projects. Good job!
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
i know how to do the safe gaurding code but it will take up too much more space. not worth it. thank you everyone, it works perfect, it even is funny!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Sooo, are you going to find us all compatible dates now? Err, the single ones of us, anyway!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Valerie String:
i know how to do the safe gaurding code but it will take up too much more space. not worth it. thank you everyone, it works perfect, it even is funny!


This would make an interesting time capsule.
Take your code from the original post and David's re-worked version of it.
Print them both on the same sheet of paper (or mail it to yourself on a yahoo mail account).

Then, compare both, a year from now.
Do the same thing two years from now.
Each time you compare the two, ask yourself how much space the first one saved you and if it was worth it.

There are very VERY few cases left out there where anything takes precedence over readability.
In those cases, you probably won't be using Java anyway.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
yeh i sure will. my next thing is a poker program. should be fun. have ages for it so hopefully wont have as much problems. thanks all
 
jQuery in Action, 2nd edition
 
subject: Comparing ages
 
Similar Threads
Error With Simple Converter
TreeSet - Comparable
Difficulty in re- rendering form data, eventhough no exception is thrown up
Out of bounds exception in code
? Throw an exception vs other approach