简体   繁体   中英

JAVA Implementing interface with methods that take and return type interface

I've run into a problem. I have to implement this interface:

public interface Set_of_objects {
    boolean equal(Set_of_objects s);
    Set_of_objects cross(Set_of_objects s);
}

The class implementing it has to take an array of objects and test it with another array of objects. The method "equal" should return true if they are equal (false if they are not equal), and the method "cross" has to return an array of objects consisting of objects that they share.

I haven't been able to figure out how to even compare those 2 arrays because of the type of argument required in the method.

public class Object_array_test implements Set_of_objects {
    Object[] x = new Object[5];

    public Object_array_test (Object[] x) {
        this.x = x;
    }
    public boolean equal(Set_of_objects s) {
        return  s.equals(x);
    }
    public cross(Set_of_objects s) {
        return null;
    }
}

When I do it like this it says: "Unlikely argument type for equals(): Object[] seems to be unrelated to Set_of_objects"

And when I try casting (like I saw on the internet):

Object[] y = (Object[]) s;

I get this: "Cannot cast from Set_of_objects to Object[]"

How do i fix this problem and can somebody please explain how does it exactly work? Thank you and sorry for the stupid question.

A. Just try to read your requirements over again (and slowly :-) ):

  1. The class implementing it has to take an array of objects

  2. method "cross" has to return an array of objects

so... neither

boolean equal(Set_of_objects s);

nor

Set_of_objects cross(Set_of_objects s);

satisfy your requirements - Set_of_objects is not an array , but just an object implemented Set_of_objects interface.

your interface's method signatures must look like

public interface Set_of_objects {
   boolean equal(Set_of_objects[] s);
   Set_of_objects[] cross(Set_of_objects[] s);
}

B. From other hands you can use your Set_of_objects as a holder for array as you do... but you need another method (getter for your array eg getX() ) and use it to compare array members...

PS I guess you will figure out how to loop through array to compare Objects and put them into another one to return from cross method.

also, in real world it is not recommended to return new instance of array from methods... but as exercise it does not matter too much.

Good luck!

There appear to be a few minor issues here. Here is an updated interface:

package com.simi.practice.work.PracticeWork;
import java.util.*;
import java.lang.*;


public interface Set_of_objects {

        boolean equals(Object[] x, Object[] y);

        Object cross(Object[] x, Object[] y);

        }

In your original interface you were not passing an Array as an object. You were simply passing an interface "type". You have to pass an actual Array Object however in order to pass an Array to any of your methods or constructors in your class. We can pass an x object and ay object in the interface, and any class implementing the interface will obviously use these methods:

package com.simi.practice.work.PracticeWork;
import java.util.*;
import java.lang.*;

public class Object_array_test implements Set_of_objects{


    public boolean equals(Object[]s, Object[]t) {
        String v = Arrays.toString(s);
        String r = Arrays.toString(t);

        if(v.equals(r)) {

          return true;
         }

         return false;

      }
    public Object[] cross(Object[]s, Object[]t) {
        ArrayList<Object> n = new ArrayList<Object>();
        for(int x= 0; x<s.length; x++) {
           if(s[x].equals(t[x])) {
              n.add(s[x]);

           }
        }System.out.println(n.toArray());
        Object[] p = n.toArray();
        System.out.println(Arrays.toString(p));
        return  p;
    }

 }

In the code above, you don't know which kind of object you will pass which is why we convert it to a String. As I noted earlier, the methods need to take an Object as an argument in order to work with the Arrays. Simply passing the interface "type" will not work. In the .equals() method we can then compare the two Arrays to see if they are identical. if not, the code will return false. If they are identical, the code will return true.

And lastly, lets test the class and the output:

public class App {
        public static void main(String... args) throws IOException, MyException{

        Object_array_test test = new Object_array_test();
        Object[] i = {1, 2, 3, 4};
        Object[] j = {1, 2, 90, 4};
        System.out.println(test.equals(j, i));
        System.out.println(test.cross(i,  j));

     }
 }

As you can see these two Arrays are not equal and the code returns false. We also created a new array from an Array list that returns the numbers that are the same in the Array. We return the new Object that we have created and print it to the console as well.

And here is your output:

false
[Ljava.lang.Object;@7852e922
[1, 2, 4]
[Ljava.lang.Object;@4e25154f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM