【问题标题】:Counting odd, even & negative numbers in an array using Java Inheritance & Polymorphism使用Java继承和多态计算数组中的奇数、偶数和负数
【发布时间】:2014-11-17 17:02:23
【问题描述】:

[类图]!1

我是一个学习Java继承和多态的新手。我遇到了我一直试图解决的这个问题。我似乎得到了正确的预期答案,但每当我运行测试时,我都会收到 IsEven 和 IsNegative 的错误。尽管我在各个类中的代码运行良好,但与导致这些错误的继承和多态性相关,我可能做错了什么?

问题来了: 上图显示了类之间的关系。 考虑如下整数数组:

int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13}
  1. 完成 Count 类中名为 count(int[] a) 的方法。该方法应返回数组中正数的数量。因此 count(a) 应该返回 7(不计算 2 个负数和 0)。

  2. 在检查您的代码的计数(您对部分-a 的解决方案)时,您将看到与此类似的测试:if (a[i]>0) {...}。如果我们现在要求您修改 count 方法以便计算负数的数量,那将是微不足道的:您所要做的就是将大于比较运算符(“>”)更改为小于-than 运算符(“

一个。完成与您的 count 实现类似的方法 countIF(来自本问题的前面部分),但将实现适当测试的对象作为参数。 countIF 的签名如下:

int countIF( int[] a, Predicate p)

补全类Predicate,它只有一个方法,如果参数大于0,则返回true:

boolean test(int x){ ... ? ... }

当使用 Predicate 对象调用 countIF 时,行为将与以前一样:您的代码将计算大于 0 的整数个数(即执行将返回 7)。 湾。现在完成两个类(IsNegative 和 IsEven)作为 Predicate 类的子类,以帮助您计算负数(IsNegative)和偶数(IsEven)。

class IsNegative extends Predicate {...}
class IsEven extends Predicate {...}

注意:IsNegative 和 IsEven 类应该有一个与之前签名相同的测试方法:test(int x)。编写完这些类后,您将能够将适当类的实例传递给 countIF 以进行我们需要的测试:

1.  countIF( a, new IsNegative() )
2.  countIF( a, new IsEven() )

上面的第一个调用会为我们的数组返回 2。第二次调用将返回 4。

以下是给出的类:

public class Count
{
   int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13};

   public int count( int[] a) {
       return -1; // Note to Student: Delete this line. Write your implementation here.
    }

    public int countIF( int[] a, Predicate p) {
      return -1; // Note to Student: Delete this line. Write your implementation here.
    }

}

public class Predicate
{
    public boolean test(int x) {
        return true; // Note to Student: Delete this line. Write your implementation here.
    }
}

public class IsEven extends Predicate
{
   public boolean test( int x) {
       return true; // Note to Student: Delete this line. Write your implementation here.
    }
}

public class IsNegative extends Predicate
{
    public boolean test( int x) {
        return true; // Note to Student: Delete this line. Write your implementation here.
    }
}

public class CountTest extends junit.framework.TestCase
{
    public void test_count() {
        Count c = new Count();
        int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13};
        assertEquals( c.count(a), 7);
    }

    public void test_countIF_predicate() {
        Count c = new Count();
        int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13};
        assertEquals( c.countIF(a, new Predicate()), 7);
    }

    public void test_countIF_even() {
        Count c = new Count();
        int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13};
        assertEquals( c.countIF(a, new IsEven()), 4);
    }

    public void test_countIF_negative() {
        Count c = new Count();
        int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13};
        assertEquals( c.countIF(a, new IsNegative()), 2);
    }
 }

这是我的课程实现。我在继承和多态方面做错了什么?

public class Count
{
   int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13};
    int positive =0;

     public int count( int[] a) {
     for(int i=0; i<a.length; i++){
           if(a[i]>0){
               positive+=1;
            }
        }
        return positive;

   }

    public int countIF( int[] a, Predicate p) {
        for(int i=0; i<a.length; i++){
            if(a[i]>0){
                positive+=1;

            }
        }
        return positive;
    }
public class Predicate
{
    public boolean test(int x) {
        int positive=0;
        if(x>0){
            return true;
         }else{
            return false;
        }


    }

}

public class Predicate
{
    public boolean test(int x) {
        int positive=0;
        if(x>0){
            return true;
         }else{
            return false;
        }


    }


}

public class IsEven extends Predicate
{
   public boolean test( int x) {
      if((x%2)==0){
          return true;
        }else{
            return false;
        }
    }

      public int countIF( int[] a) {
       int even =0;
      for(int i=0; i<a.length; i++){
          if((a[i]%2)==0){
            even +=1;

            }

        }
         return even;
    }
}

public class IsNegative extends Predicate
{
    public boolean test( int x) {
        if(x<0){
            return true;
        }else{
            return false;
        }

    }

     public int countIF( int[] a) {
       int negative =0;
       //super(x);
      for(int i=0; i<a.length; i++){
          if(a[i]<0){
            negative +=1;

            }

        }
         return negative;
    }
}

【问题讨论】:

    标签: java arrays inheritance polymorphism counting


    【解决方案1】:

    我看到的最大问题是您的 Predicate 子类没有要覆盖的 test() 方法,因此您编写的代码应该在每个类的 Predicate 中。基本上,我建议你改Predicate之类的,

    public class Predicate
    {
      public boolean test(int x) {
        return false;
      }
      public int countIF(int[] a) {
        int count = 0;
        for (int v : a) {
          if (test(x)) count++;
        }   
        return count;
      }
    }
    

    然后你的IsEven(例如),

    public class IsEven extends Predicate
    {
      public boolean test(int x) {
        return ((x%2)==0);
      }
    }
    

    【讨论】:

    • @Elloitt 感谢您的帮助。我已经对我的谓词类以及 IsEven 和 IsNegative 类进行了相关更改。但是,我无法将 Predicate 类中的 countIF 方法与 Count 类中的 countIF 方法相关联。他们不应该有关系吗?目前,我的 Count 类中的 countIF 方法与 Predicate 类中的方法没有关系。我迷路了。能否请您为我澄清一下这方面?
    • 你为什么需要另一个countIF?这个是为了取代它。如果您需要Count 中的countIF,您可以委托给Predicate.countIF()
    猜你喜欢
    • 2016-07-05
    • 2014-06-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多