【问题标题】:Writing a contains method in java在java中编写一个包含方法
【发布时间】:2014-01-22 21:46:50
【问题描述】:
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 方法需要做什么? (目前,它告诉我方法 condition(int[]) 不是 homework1 类中的有效方法。)

【问题讨论】:

  • 在问题中,那些不是方法,它们是类。这就是你的老师把它们放在PascalCase 的原因。您应该实现控制模式的反转而不是硬编码方法。要了解它的外观,请查看Guava Predicate 以及它如何与Collections utilities 交互。
  • 你应该再读一遍练习:你需要创建一个类Prime,而不是一个方法Prime()。所有这些类都应该实现一个通用的接口,例如Test,它定义了一种方法来检查给定的 int(或者,在练习 2 中,任何类型)是否满足类的测试,例如对于Prime,它测试数字是否为素数。
  • 您不能将new 与方法一起使用。其实Prime等应该是类!
  • 这个问题似乎是题外话,因为它是关于家庭作业的。 OP有一个描述解决方案的答案,另一个几乎是勺子喂它。 OP 已发布更新的代码以寻求更多帮助。这不是一个家庭作业网站。

标签: java arrays generics contains


【解决方案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”。

【讨论】:

    【解决方案2】:

    您可以编写如下代码,使用接口的合同来完成这项工作,照顾满足您条件的方法。

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

    【讨论】:

    • 您有代码重复 - 我确定这不是预期的。 Predicate 应该只检查 single int - 然后 contains 方法应该进行循环。无论如何,用勺子喂家庭作业问题的答案是不受欢迎的,因为 OP 不会学习。
    • 我的目的不是做功课,而是展示另一种方法。
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    相关资源
    最近更新 更多