在java中编写contains方法

时间:2014-01-22 21:46:51

标签: java arrays generics contains

package homework1;
//author Kyle Fields


public class HomeWork1{


public static void main(String[] args) {

 int [ ] input = { 100, 37, 49 };

boolean result1 = contains( input, new Prime( ) );
boolean result2 = contains( input, new PerfectSquare( ) );
boolean result3 = contains( input, new Negative( ) );
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);

}



 static boolean contains(int[] array, Condition condition) {
   return (condition(array));

}

}



package homework1;

/**
 *
 * @author Kyle Fields
 */
public interface Condition {

    boolean makeYourCondition(int[] input);

}


package homework1;

/**
 *
 * @author Kyle Fields
 */
public class Prime implements Condition {


@Override
public boolean makeYourCondition(int[] input) {
        for (int n : input) { 
        if (n >= 2) {            
        if (n == 2) {
            return true;
        }
        for (int i = 2; i <= Math.sqrt(n) + 1; i++) {
            if (!(n % i == 0)) {
                return true;
            }
        }
}
}
return false;
}

    }

以下其他课程

package homework1;

/**
 *
 * @author Kyle Fields
 */
public class PerfectSquare implements Condition {

@Override
public boolean makeYourCondition(int[] input) {

    for (int i : input) {
//takes the square root
long SquareRoot = (long) Math.sqrt(i);
//multiplys the sqrt by the sqrt to see if it equals the original
 if (((SquareRoot * SquareRoot) == i) == true){
 return true;
 }

    }
return false;

    }
}



package homework1;

/**
 *
 * @author Kyle Fields
 */
public class Negative implements Condition {


boolean Negative(int n){
 if (n <= -1){
 return true;
 }
 return false;
}

@Override
public boolean makeYourCondition(int[] input) { 

    for (int i : input) {
        if(i<0) return true;
    }

       return false;
    }

}

我的问题是,如何完成此代码?含义:我需要为contains方法做什么? (目前,它告诉我方法条件(int [])不是homework1类中的有效方法。)

2 个答案:

答案 0 :(得分:1)

只要你知道你在做什么,

虚拟代码就可以了。 首先是contains()方法,它接受一个数组和一个Condition并返回一个布尔值。我们来写签名

boolean contains(int[] array, Condition condition)

Prime,PerfectSquare和Negative将是Condition的实现,即

interface Condition {
...
}

class Prime implements Condition {
...
}

class PerfectSquare ...

练习的设置暗示在Condition实现中你应该检查参数int值是否满足特定情况; contains()方法遍历数组,如果遇到“true”或“false”则返回,如果耗尽列表。

答案 1 :(得分:-4)

您可以按照以下方式编写代码,使用界面的合同来完成工作,查看符合您条件的方法。

public class HomeWork {
    public static void main(String[] args) {

        int[] arr=new int[] {100, 37, 49};

        Condition[] conditions= new Condition[]{
                                                new Negative(), 
                                                new Prime(), 
                                                new PerfectSquare()
                                            };

        for (Condition condition : conditions) {
            System.out.println(condition.makeYourCondition(arr));
        }
    }
}

interface Condition {
    boolean makeYourCondition(int[] input);
}

class Negative implements Condition {

    @Override
    public boolean makeYourCondition(int[] input) {

        for (int i : input) {
            if(i<0) return true;
        }

        return false;
    }

}

class Prime implements Condition {

    @Override
    public boolean makeYourCondition(int[] input) {

        //TODO PUT YOUR CONDITION

        return false;
    }

}

class PerfectSquare implements Condition {

    @Override
    public boolean makeYourCondition(int[] input) {

        //TODO PUT YOUR CONDITION

        return false;
    }

}