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.

Results 1 to 13 of 13

Thread: Need Help on the Interface Function

  1. #1
    Junior Member yel_hiei's Avatar
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help on the Interface Function

    Hi can someone help me on this. I have this code below:

    Set.java
    public interface Set{
      public Set union (Set A,Set B)
    }
     
    MySet.java
     
    public class MySet implements Set{
     
       public Set union (Set A, Set B){
     
       }
     
       public static void main (String args[]){
           Set A = new MySet();
           Set B = new MySet();
           MySet mainSet = new MySet();
           mainSet.union();
       }
    }
    --> 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Help on the Interface Function

    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.

  3. #3
    Junior Member yel_hiei's Avatar
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on the Interface Function

    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.

  4. #4
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Thumbs up Re: Need Help on the Interface Function

    Quote Originally Posted by yel_hiei View Post
    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.

  5. #5
    Junior Member yel_hiei's Avatar
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on the Interface Function

    Thanks so much for your help. A new method has been added to the Set.java interface, public void add.

    Set.java

    public interface Set {
    public void add(Object x)
    public Set union(Set S);
    }
    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.

    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();
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Help on the Interface Function

    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.

  7. #7
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Need Help on the Interface Function

    Quote Originally Posted by yel_hiei View Post
    Can someone please show me how to implement the Set interface in the Test.java... Thanks so much for the help.
    what type of objects do you want to add?

  8. #8
    Junior Member yel_hiei's Avatar
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on the Interface Function

    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);
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Help on the Interface Function

    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.

  10. #10
    Junior Member yel_hiei's Avatar
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on the Interface Function

    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);
    }

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need Help on the Interface Function

    The simple way to properly implement an interface:

    // the interface
    public interface Set
    {
        public Set union(Set S);
        public void add(Object x);
    }

    1. Copy over all the methods.

    public class TestSet implements Set
    {
        public Set union(Set S);
        public void add(Object x);
    }

    2. Replace the ";" with { }, put your code between the square brackets.

    public class TestSet implements Set
    {
        public Set union(Set S)
        {
            // TODO: put your code here
        }
     
        public void add(Object x)
        {
            // TODO: put your code here
        }
    }


    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.

    public class TestSet implements Set
    {
        @override
        public Set union(Set S)
        {
            // TODO: put your code here
        }
     
        @override
        public void add(Object x)
        {
            // TODO: put your code here
        }
    }
    Last edited by helloworld922; July 28th, 2010 at 11:01 PM.

  12. #12
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Need Help on the Interface Function

    Quote Originally Posted by Norm View Post
    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.
    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).

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Help on the Interface Function

    Cross posted at Need Urgent Help in Implementing Interface Class - Java Forums

Similar Threads

  1. How To Make The Program Jump To The Next Function..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 3rd, 2010, 12:33 AM
  2. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 11:29 AM
  3. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Programming Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM
  4. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM
  5. Traverse Function for Knights tour
    By budder8818 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 4th, 2009, 03:49 PM