aspose file tools*
The moose likes Beginning Java and the fly likes problems comparing two people for a dating agency program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problems comparing two people for a dating agency program" Watch "problems comparing two people for a dating agency program" New topic
Author

problems comparing two people for a dating agency program

Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
i have a person class with the attributes set up and all and in the main i have it so i can read in a number of people(using arrays, which i am also havin problems with) but i am havin problems with comparing whether two people are a match or not. there are a few conditions, ie must be one male one female in the couple, both much work the same work shifts, live in the same area, if under 26 the female must not be morethan 1 yr older, and the male must not be more 25cm taller. etc. how would i go about comparing them and returning an answer to the user

[ February 12, 2005: Message edited by: Valerie String ]

[ February 12, 2005: Message edited by: Valerie String ]

[ EJFH: Edited subject line. everyone needs help. ]
[ February 14, 2005: Message edited by: Ernest Friedman-Hill ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Write a function in your Person class that takes a Person instance as an attribute and returns a boolean.
Up to you to decide what to do if you are comparing a person to herself, are people compatible with themselves or not...


42
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
oh, your problem isn't urgent to me. I just have nothing better to do right now.
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
no its has to be a couple of one male and one female. just struggling with the if statements and all that, i am a complete beginr. before few months ago had no experience wit programmin, just have an assignment due and its driving me mad
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well, here's what I'm thinking (sitting in a coffee shop still half asleep)...

You might want to represent compatability as a score -- for example, 0 would be "no way" and 100 would be "perfect match." To get this score, write a method that takes two instances of "Client" (the people you're trying to match up) as arguments...

int compareClients(Client c1, Client c2) {...}

Notice that this method will return an int (which is the score). Within the method, you'll first want to weed out mismatches by immediately returning zero.

if(c1.gender == c2.gender) return 0;

(For simplicity, I've just assumed c1.orientation and c2.orientation are both "heterosexual." But you can make this as complex as you wish. :roll: )

Then move on to building a score. For example, if they both like coffee (and who doesn't?), then you might add 5 to the score. If they both like obscure Guy Maddin films (and who doesn't?), then you might add 12 to the overall score.


Note that the comparisons above simply look for a match. For example, the coffee statement will add 5 to score if both clients love coffee or if both clients hate coffee. But score won't be increased if they differ.

I've also assumed that the variables "coffee" and "maddin" are simple booleans (true/false = like/dislike). But this could also get very complex: If your clients rate their likes and dislikes on a scale, then you could increment score by 5 if c2's rating is within 2 points of c1's rating; or increment score by 3 if the ratings are within 4 points; etc. In other words, you could adjust score differently depending on how "close" each attribute is -- not just whether they match exactly.
[ February 12, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
thanks, that has been the best idea i have had all day. been sittin here for 6 hrs tryin to figure something out and it really has been
hope you enjoyed your coffee prob will have few more problems by tomorrow, for the moment i'm goin out tonight. thanks again
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You still have not shown anything you've done on your own. What exactly are you having problems with? It would help a lot if you post some of your code, any compiler errors you get, and any other information that will help us help you. Did you bother to look at the link Ilja gave you in your other thread? In case you missed it, here's some information about How To Ask a Question the Smart Way. I strongly suggest you read and follow the advice given there. It will definitely benefit you in the long run as more people will be more likely to help you out.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
sorry i am new to this whole thing, will take a look at it now. and the reason i have no code to show an example is that i didnt know where to start but the ideas that i got above were very helpful and hopefully i'll have it nearly finished later today. thanks, next time i will pose my questions different.
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
Summery of what i want to do in the program:
1) Read in from the user how many people they want to match up (ie 4)
2) Ask the user for all the related information, (ie gender, age, height.)
(So this has to be ask 4 different times for each of the 4 people)
3) Then compare whether the couples are compatible or not.
4) Print out the result to the user.


At the moment i have no eclispe or equivilent on my comp so its harder to write the code.And for the moment i am having most problems with the arrays as i am trying to self teach, because we have not come across it in college yet!
In class person i have 8 attributes: name, age, workstatus, shiftstatus, gender, height, post code, smokestatus.
I want to read in all this information(just say 4 times)and store it in a person array. How is this possible:
Valerie String
Greenhorn

Joined: Feb 12, 2005
Posts: 21
sorry i hit the wrong button before i was finished that post.
Here is some of my code(in random order)





Is the index code even needed, how does the person array store all the attributs the user has entered. i am really confused.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Remember the 3 steps in getting an array "up and running"...
  • Declare. You've done this with Person[] numberOfPeople;
  • Instantiate. You've done this with numberOfPeople = new Person[size];
  • Value. You seem to be stuck here.
  • Instantiating the array simply created a Person array of the desired size (and yes, an int is exactly what you want to specify the size). But this array's elements are all initialized to null references. You want these to reference new instances of...? Answer that, and I think you'll be on your way.
    [ February 13, 2005: Message edited by: marc weber ]
    Valerie String
    Greenhorn

    Joined: Feb 12, 2005
    Posts: 21
    Yeh i was thinking that myself. I think i will have to do more research on arrays and get back to ye on this one. Thanks for all the help thus far. (really am a beginner!)
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    You've almost got all of the array handling complete, so this suggestion won't really apply to it. However, I still think it's good advice. Whenever I learn something new or teach coding to others, the first step is to break up the problem into small easily-digestible steps.

    In this case, I would have first written just the code to read in a single Person's information. Once that's working, looping to read in multiple Persons is much easier. By constantly adding a small bit of code and keeping your program in a running state at all times (save backups along the way!), it's easier to get the next thing working or backtrack if you decide it's not worth it.

    Anyway, you're close with the arrays, so I'll say just one thing. The name of your array doesn't really convey what it holds. The "size" variable already tells you the "number of people" you are going to store. What does the array variable store?

    Finally, here's a tip about looping. Your code above (which is fine so ignore this if you're overwhelmed)has an equivalent construct in Java called a "for loop." It's very handy when you are looping over a sequence of values as you are. The following does exactly the same thing but tends to be more readable as it brings your intent across more clearly.Here I've replaced "numberOfPeople.length" with "size" since they're equal and "index = index + 1" with its equivalent "index++". The ++ operator increments the variable it is next to. There's also a -- decrement operator.

    Using the comments I put into your code gives you the general form.The initialize step is done once at the start of the loop. Then, before each iteration, the loop condition is checked. If it fails (is false), the loop stops. If it succeeds (is true), the loop work is executed and then the increment step is executed. The loop then goes back to check the loop condition, and the whole thing repeats until the condition is false. As you can see, this works exactly as your while loop does.
    [ February 13, 2005: Message edited by: David Harkness ]
    Valerie String
    Greenhorn

    Joined: Feb 12, 2005
    Posts: 21
    Thanks for the idea, saw that being used in some of the code here and looked it up and i do understand it but for the moment i am havin enough trouble with learning arrays that i think i will stick with my while for a little bit longer as it does the same thing and i am used to using it..

    As for the other advise, are you suggesting not to use arrays, because at this point it seems very tempting, well i do have the code to read in for one person. we have been told the we can make the code specific, as in only read in for 4 people no more no less, as we have not reach arrays and it is unfair to make us do that. do you think i should take that option instead.
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by Valerie String:
    sorry i hit the wrong button before i was finished that post.
    Here is some of my code(in random order)
    ...

    For a program to run, code cannot be "in random order." In Java, the program starts with a method named main(), so this is one place to start. You should create a class with this particular method:

    While this is a decent place to start writing the code, you can soon become stuck. You may ask, "What do I need to do in main()?" Well this is where a time-honored pencil and paper come in handy. Often you should write some ideas down. For example, does it make sense to split this program up into different classes? To identify these classes, look to see what different functionality is needed to accomplish the task. Often classes model real-life entities, so in this case it looks like a Person class will be handy.

    Next, you should ask yourself what information is needed to model a Person. The attributes you choose can be used as fields. Also, what actions can a Person perform (or can be performed on a person). These actions are typically implemented as methods in the class.

    My advice here is intentionally somewhat vague. As you learn Java syntax, I think it is also important to learn about design principles. Learning how to design a program makes coding it much easier, imho. My suggestions above are mostly centered around figuring out the design before you even begin coding.

    Anyway, I hope your figuring things out. Please come back with more questions.

    Layne
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Originally posted by Valerie String:
    i think i will stick with my while for a little bit longer as it does the same thing and i am used to using it..

    As for the other advise, are you suggesting not to use arrays, because at this point it seems very tempting, well i do have the code to read in for one person.
    I agree about keeping with the while loop. Sometimes the advice I give isn't just for that specific person to implement immediately, but also for others who read these posts and have similar questions. Stick with what works until you have the time and inclination to improve it.

    Looking at your code, I assumed you were starting with the arrays. I apologize as I try not to assume. Oops. So no, I'd stick with the arrays because you are so very close and they will make the rest of the program easier in my opinion.

    How are you storing the data you read for each person? Do you have a Person class as Layne suggests? If you can post some of that code or at least describe it, we can help you tie them together. You've declared your array as a Person[], so I'm guessing that you do have a Person class already. Can you show us where you create Persons and read in data about them?
    Valerie String
    Greenhorn

    Joined: Feb 12, 2005
    Posts: 21
    yeh i have the very basics done.

    Valerie String
    Greenhorn

    Joined: Feb 12, 2005
    Posts: 21


    This is rough code, not filled in a still with some errors, have lectures now but am i on the right track???
    [ February 14, 2005: Message edited by: Valerie String ]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    You're definitely on the right track!

    But there seems to be some confusion on the creation of Person objects.

    First, your code attempts to create two different instances -- one assigned to a variable called "person" and the other as an array element. Remember, whenever you say "new," you get a new (distinct) object...

    One of these serves no apparent purpose. (I'll leave it to you to identify which one.)

    Second, notice that I said "attempts to create." Both of these lines try to call a no-args constructor, which the Person class (as defined above) does not have. Remember, if any constructor is defined, then there will be no default constructor automatically provided by the compiler.

    Third, your Person constructor is private. Can you explain why? or what additional requirements will this impose on your design?
    [ February 14, 2005: Message edited by: marc weber ]
    Valerie String
    Greenhorn

    Joined: Feb 12, 2005
    Posts: 21
    I have change my code to deal with just 6 people, i gave up usin arrays, im sorry for the waste of all your time and my own. I just couldnt understand it. I know ye are all probably annoyed with me now for givin up and as you wer sayin i was so near but i just wasnt gettin it. now it seems to be working for me and we are allowed do it this way if we make it for 6. which means one long peice of code.
    I feel really bad, but i appreciate all the help ye have given me. i'll hopefully be able to tell you it works by the end of the night. thank you all again
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Valerie String:
    ...i gave up using arrays...

    Hmmm... When you start coding the comparisons, I think you'll find that arrays make it very easy to loop through objects by index. You might actually be making this more complicated without arrays.

    Besides, arrays are fundamental; so the sooner you get comfortable with them, the easier your coding life will be.
    Valerie String
    Greenhorn

    Joined: Feb 12, 2005
    Posts: 21
    I know and if we were thought them in college i would know a bit better. but i am bad at self teaching. for this particular piece of code i am happy makin it more complicated because it is not THAT long(so long) but atleast i know what i am doin. thank you again
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    The general that does not know the appropriate time for an orderly retreat gets all his soldiers killed. Don't sweat taking a step back when you're biting off too much to chew. As you said, you'll get to them later anyway. I think you're getting overwhelmed by too much at once rather than arrays being complicated, for when you do study them, you'll see that they're not that different from normal variables.

    So is it correct that you now have the code working to read in data for and create six Person objects? What's the next step?
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by David Harkness:
    The general that does not know the appropriate time for an orderly retreat gets all his soldiers killed...

    That's a good point. I guess I had missed the fact that arrays hadn't been formally covered yet.
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    I think it's also hard for us because in our minds arrays are old hat. We can see that Valerie's one step away from getting it to work using arrays, and we want her to get it.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: problems comparing two people for a dating agency program
     
    Similar Threads
    your experience*** true Experience
    Enums
    problem with logical condition
    ABC Is Looking For Interviews
    Absolutes