Hi can someone help me on this. I have this code below:
Set.java
--> Can someone please tell me how to implement the union class? And how do you put values on Set A and Set B?
Thanks so much!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi can someone help me on this. I have this code below:
Set.java
--> Can someone please tell me how to implement the union class? And how do you put values on Set A and Set B?
Thanks so much!
Last edited by yel_hiei; July 27th, 2010 at 10:33 PM.
I'll give you a start on defining a Union class (notice uppercase first letter)
public class Union {
... code here ...
}
Just noticed the .js in your post. Is this a javascript question?
You'll get better help on a javascript forum.
Hi Norm thanks for noticing that. That was a typo error on my part. This is a java file, I need to know how to add values on Set A and Set B so I can implement a union function. The problem is Set is declared as an interface... Please advise. Thanks much.
The interface Set force you to implement the following code: a public function called union that takes two arguments of the type Set and return a reference of type Set. Now, if you implement the Union class like below, the Union class pass the test IS-A Set. So the problem "The problem is Set is declared as an interface ..." vanish, since the compiler will be happy because the Union class implements the Set interface. So even if you implement the interface in the Union class like
public class Union implements Set { Set u1; Set u2; public Set union (Set A, Set B) { u1 = A; u2 = B; // the method MUST return a Set // the value is nonsense, but make the code run return u2; } }
you can call the union function with
public class Test { public static void main(String[] args) { Union u1 = new Union(); Union u2 = new Union(); u1.union(u1, u2); } }
Union classes as argument, since Union implements Set and class Union IS-A Set.
Thanks so much for your help. A new method has been added to the Set.java interface, public void add.
Set.java
The public void add function is suppose to add values to a Set but I'm not sure how to do this and what to pass on Object x. Can someone please show me how to implement the Set interface in the Test.java... Thanks so much for the help.public interface Set {
public void add(Object x)public Set union(Set S);}
Test.java
public class Test implements Set{
public Set union (Set A){
}
public void add(Object x){
}
public static void main (String args[]){}
Set A = new MySet();}
Set B = new MySet();
Inside the Test class, how are you going to save the added Objects? An array or where?
When you have decided on that, then the add() method will add an Object to that place where you are saving the objects.
I wanted to add an array. Can you check on my codes below. I'm not quite sure how to program the add and union functions. Thanks much!
public Set union (Set A){
}
public void add(Object x){
// this is not working. I'm not sure how to get the values from the object.
int[] obj;
obj = x;
}
public static void main(String[] args) {
int[] obj1 ={1,2,3};
Set set1 = new MySet();
set1.add(obj1);
}
Before you code the program you need to decide what type of data you are going to save in the Set.
More advanced students would use the Generics technique, but I recommend that you work with the simplest type for now and the convert to the more sophisticated types later. So instead of saving objects in your Set, just use ints.
In the Set class you'll need to define an array to hold the ints. Later you'll want to convert that to an expandable type like ArrayList.
Given an array, consider how to add a new int to that array.
For the first version of your program:
Write an add method for the Set class.
Write a main() method that creates a Set object and calls the add() method a few times.
The Use the Arrays.toString(theArray) method to display what you have added to the array.
Hi, I'm quite confuse with your instructions (so sorry, I'm quite new to Java)... Can you show me the simplest way to implement the Set interface below: Any method would do...
public interface Set {
}
public Set union(Set S);
public void add(Object x);
The simple way to properly implement an interface:
1. Copy over all the methods.
2. Replace the ";" with { }, put your code between the square brackets.
3. [optional] add the @override annotation. This is an "extra check" to make sure you have the method declared correctly. It doesn't provide anything functionally or hinder/help performance at all, however I would highly recommend it when overriding methods from super classes. For interfaces it's less useful because the compiler will automatically puke if you don't have the correct method included.
Last edited by helloworld922; July 28th, 2010 at 11:01 PM.
for good reasons. if you use Object as a parameter, the add method can add any type of objects. But the problem arise when you gone to read them or want to use a method of the passed object. if you store different type of objects, when you read them and you want to call a metehod you have to check which type the object you will be and then cast the object. so, think twice about add(Object x).
Cross posted at Need Urgent Help in Implementing Interface Class - Java Forums