if I input:

set A = [ A, B, C ] are the elements

set B = [ 1, 2 ] are the elements

if I input this like what is asked in question 4:

(a, 1), ( b, 1), ( c, 2 ) - this is a function.

if I input this:

(a, 1), ( a, 2), (b,2) - this is not a function

If user chooses[4], the program asks the user to inputs a set and the program determines if it is a function. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

The problem is how will I scan those elements to check if it's a function or not, what i'm tinking is to use a string tokenizer or just put them into an array and scan them one-by-one to determine if it's a function or not or is there any package in java that can compare the elements of list 1 to list 2?

[edit]Additional: look at the links to "interfaces" and "implementations".[/edit]

This is the method I created using hashmap I guess this could be minimized but it's working as I expected. I can't use Map because I can't do duplication so I just used HashMap.

[edit]I didn't read the tutorial about HashMap I just looked on the documentation and used it. I presume I used HashMap the wrong way?

__== true__or

__== false__. It is

__if (foo.contains(...)) ...__. Saying == true or == false can cause nasty errors if you write = by mistake.

Look at the return value of the put method. I suggest your function should not permit

`null`values; maybe you could use the older Hashtable class because it throws an Exception if you pass

`null`.

Pass the Set to the method which tests it, create a new Mapand test the Set to see whether it is empty.

Put each member of the Set into the Map, and see what is returned. If you get a certain return value, you are all right. If you get the wrong return value, you know you are not dealing with a function, and can stop.

By the way, the Cartesian product of two sets where the domain Set has more than one member can never be a function.

I've changed the code a bit and followed what you've advised ( I don't know if I followed it 100% ). I guess the method I created is not smart enough to check if the sets input by the user is a function or not.

If user chooses [1], the program prompts for the elements of the sets:

How many elements for set A? 3

Element 1: A

Element 2: B

Element 3: C

How many elements for set B? 2

Element 1: 1

Element 2: 2

The sets are:

A= {A,B,C}

B={1,2}

If user chooses[2], the program displays the sets. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

If user chooses[3], the program displays the Cartesian Product of set A and B. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

If user chooses[4], the program asks the user to inputs a set and the program determines if it is a function. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

If user chooses[5], the program asks the user to inputs a set and the program determines if it is a relation. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

I was able to answer Question 1, 2 and 3.

I am assuming in Question 4 that the elements to be inserted by the user will come from the sets I defined in question 1 ( or maybe not? ) and it should display if it's a function. According to the article I've read I can only determine if it is a function if in one input there is only one output.

e.g. { A, 1 }, { A, 2 }, { B, 1 }, { C, 1 } <- I understand this is not a function because A has 2 output 1 and 2.

I created this condition to check if there is a duplicate input so if there's a duplicate input the program will assume that it's not a function otherwise a function but I don't know if what I thought is correct. ( I'm totally confused now haha! I guess I just have to clarify Question 4 where does the user will get the input? the user will choose from the sets defined in Question 1 or the user will just enter what he/she wants to form a set to know if it's a function or not?)

If the the user's input will depend on the set defined in Question 1 now I can't think of any idea on how to make my method smart enough to check if it's a function or not. any idea? I'll be reading more about Maps and HashTable while waiting for a reply here.. thanks..

You are right; the Set you describe (or more precisely, the relation), is not a function because it contains (a, 1) and (a, 2).

Your method looks complicated, and I get suspicious whenever I see anything which looks complicated.

You are creating a new Map every time you call that method, so it will only never contain two pairs. I think the best way to sort out that problem is to pass the entire Set to the methodNote there is a fault in the Ranch causing < to be displayed as >< inside the CODE tags. I think I have corrected that misprint, however.

Don't write if (setsDefined() == false). Write if (!setsDefined()). I suspect the ! would be incorrect to test for an empty Set. Better option: set "isFunction" to be equal to a boolean value indicating whether the Set contains any members at all. You might find methods like "isFull" or "isEmpty" in the Set interface

As I already suggested, iterate through the Set, try adding the two halves of the Pair to the Map, then see what the Map#put method returns when you try entering pairs. If the key is already in the Map, you get a different value from when you add a new Key. You can test for those values and alter that value of isFunction accordingly.

If you include isFunction in the continuation condition for your loop, you will get better performance because the loop can be stopped if you encounter a key with two "values" anywhere. And once you find that, you know it is not a function. In the example you quoted, if you add (c, 3) as an additional pair at the end, you don't need to check that because you already have (a, 1) and (a, 2).

Note this method would not print out error messages. You simply return the boolean value and let some other method print the results.

Anyway, The program I created would ask me to input elements for Set A and Set B then it will produce the Cartesian product of the two sets

How many elements for Set A? 3

Enter element for Set A: A

Enter element for Set A: B

Enter element for Set A: C

How many elements for Set B? 2

Enter element for Set B: 1

Enter element for Set B: 2

Sets are now defined.

The cartesian product is: AXB = { A, 1}, { A, 2 }, { B, 1 }, { B, 2 }, { C, 1 }, { C, 2 }

==========================================================

Now I'm stuck with question 3: where will I get the set I'm going to input to determine the set if it is a function or not? from the cartesian product? or from Set A? or from Set B? same question with question 4.

Delete all the "set" methods.

Make both your fields "final".

Make the class "final".

Set both fields in the constructor.

Delete the no-args constructor.

Throw a NullPointerException if you try to pass a

`null`argument to the constructor.Note I have added the three methods usually overridden from java.lang.Object. Also you have a nice immutable class, with one exception: Because you are using boxed classes or Strings, these are all immutable themselves, and can safely be returned. If you used mutable S and F, the "get" methods would have to return "defensive copies" of the S and F.

Now you were trying to put the Pairs into a Map, and get them from a Set. So you want a method something like thisNote the wonderful new feature of the Ranch software: it changes the names of classes to URLs, so if I write NullPointerException you get an automatic link to the API. Note it has started doing it in the code tags . Note I have forgotten the syntax for declaring a parameterised method, but is obvious from your earlier posts that you know enough generics to correct any such errors.

Now all you need to check is: What is the return type from the Map when you add a pair and it is a function, and what if it isn't a function.

No need to check whether a Set contains an element before adding it. Look at the Set#add(E) method, and note its return value carefully. Note you will have to get rid of the System.exit call if you get rid of the contains() calls.

You can add the entire contents of a List to a Set, I think. Look through the Set interface (same link as previous line) for methods.

You have some confusing names for your fields: a List called setA.

With those changes, I think your Cartesian product method looks correct. I presume you have tried it. Remember that you don't get the pairs in any particular order, unless you use something like a linked set or a set based on a tree, which do preserve ordering or sorting.

What follows depends on my having understood the question correctly: I might be mistaken about the question however.

You are going to have to create a Set representing the relation C×I, so you might do well to populate a Set as you iterate through the two for-each loops. You populate that Set with Pair objects (as previously seen) then use the Pair's toString method when printing the results.

I would remind you of what I said last week

I still think you can never get a function out of a Cartesian product. But I made a dreadful mistake which nobody has noticed. I wrote "domain" when I ought to have written "range".By the way, the Cartesian product of two sets where the domain Set has more than one member can never be a function.

I will update the code after work.

I'm confused about the question in question 4: What makes me confused is the part: ask the user to input a set isn't it referring to the defined set I just created in question 1 or I have to input ( define ) a new set?

If user chooses[4], the program asks the user to inputs a set and the program determines if it is a function. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

I don't know if I understood Question 4 correctly, I was thinking that I will input a new set except the set I defined from question 1 to determine if it's a function, I guess it's wrong?

Or I will just pass the Set I defined from Question 1 to the method isFunction to determine if it's a function and I don't need to input a new set, I guess this is what the question really meant?