[类别图]!1
我是Java中的新手学习继承和多态。我遇到了这个我一直试图解决的问题。我似乎正确地得到了预期的答案,但每当我运行测试时,我都会遇到IsEven和IsNegative的错误。即使我在各个类中的代码运行良好,导致这些错误的继承和多态也可能出错?
以下是问题: 上图显示了类之间的关系。 考虑一个整数数组如下:
int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13}
在Count类中完成名为count(int [] a)的方法。该方法应返回数组中的正数。因此count(a)应返回7(不计2个负数和0)。
在检查您的计数代码(您对part-a的解决方案)时,您将看到类似于此的测试:if(a [i]> 0){...}。如果我们现在要求您修改计数方法以便计算负数的数量,那将是微不足道的:您所要做的就是将大于比较运算符(“>”)更改为小于运算符(“<”)。如果我们要求您修改计数方法来计算偶数的数量(无论是正数还是负数),那么您将修改代码以进行以下几行的测试:if(isEven(a [i])){ ...}。同样,您可以更改其他类型的计数代码(素数,大于10的数字等)。请注意,在每种情况下,我们都可以进行任何类型的计数,但我们需要编辑count方法的代码。这是增强现有代码功能的简单示例。对于这部分问题,您将编写Java代码,允许您在不修改现有方法代码的情况下增强计数功能。你将使用继承和多态的力量来做到这一点。
一个。完成与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;
}
}
答案 0 :(得分:0)
我看到的最大问题是,您的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);
}
}