简体   繁体   English

用Java编写一个contains方法

[英]Writing a contains method in java

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

    }

other classes below 下面的其他课程

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

}

my question is this, how do I finish this code? 我的问题是,如何完成此代码? meaning: what do I need to do for my contains method? 含义:我的contains方法需要做什么? (currently, it is telling me the method condition(int[]) is not a valid method in the homework1 class.) (当前,这是在告诉我条件condition(int [])在homework1类中不是有效的方法。)

dummy code is fine as long as you know what you're doing. 只要您知道自己在做什么,就可以使用伪代码。 First the contains() method, it takes an array and a Condition and returns a boolean. 首先,contains()方法需要一个数组和一个Condition并返回一个布尔值。 Let's write the signature 让我们来写签名

boolean contains(int[] array, Condition condition)

Prime, PerfectSquare, and Negative will be implementations of Condition, ie Prime,PerfectSquare和Negative将是Condition的实现,即

interface Condition {
...
}

class Prime implements Condition {
...
}

class PerfectSquare ...

The setup of the exercise hints that in the Condition implementations you should check whether the argument int value satisfies the particular case; 练习的设置提示,在Condition实现中,您应检查参数int value是否满足特定情况; the contains() method iterates through the array and returns if it encounters a "true" or "false" if exhausts the list. contains()方法遍历数组,如果遇到穷尽列表,则返回“ true”或“ false”。

You can write the code as follow, using the contract of an interface to do the job, look after methods that attend your condition. 您可以按照以下步骤编写代码,使用接口协定来完成工作,并照看符合您条件的方法。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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