2

我需要这个项目的帮助。我似乎找不到让测试代码运行的方法。我不允许更改测试代码,只能更改 IntegerSet 类。我还在学习,请帮助我解决这个问题。

我知道代码很长,所以我把评论问题!在测试和当前代码中似乎有危险信号的两个点附近。也许 toString() 与它有关,但不确定。

当前代码:

  public class IntegerSet {
  private int [] a;  // holds a set of numbers from 0 - 100

  public IntegerSet () {
    // an empty set, all a[i] are set to 0
    a = new int [101];
  }

  // A constructor that copies from an existing set.
  public IntegerSet (IntegerSet existingSet) {
    a = new int [101];
    for(int i=0; i<a.length; i++)
      a[i] = existingSet.a[i];
  }

  public void deleteElement(int i) {
    if ((i >= 0) && (i < a.length))
      a[i] = 0;  // set to 1
  }

  public void insertElement(int i) {
    if ((i >= 0) && (i < a.length))
      a[i] = 1;  // set to 1
  }

  public boolean isSet(int i) {
    return (a[i] == 1);
  }
  // PROBLEM!!!!

  // The union of this set and another set
  public IntegerSet union(IntegerSet otherSet) {
    IntegerSet newSet = new IntegerSet(this);

    // newSet is now a copy of the current set,  Next we
    // want to union with set a

    for(int i=0; i<a.length; i++) {
      if (otherSet.isSet(i))
        newSet.insertElement(i);
    }

    return newSet;
  }
  // PROBLEM!!!
  // The intersection of this set and another set
  public IntegerSet intersection(IntegerSet otherSet) {
    IntegerSet newSet = new IntegerSet(this);

    // newSet is now a copy of the current set,  Next we
    // want to intersect with set a

    for(int i=0; i<a.length; i++) {
      if (!otherSet.isSet(i))
        newSet.deleteElement(i);
    }

    return newSet;
  }


  // return true if the set has no elements
  public boolean isEmpty() {
    for (int i=0; i<a.length; i++)
      if (isSet(i)) return false;
    return true;
  }

  // return the 'length' of a set
  public int cardinality() {
    int count = 0;
    for (int i=0; i<a.length; i++)
      if (isSet(i))
        count++;
    return count;
  }

  // Print a set to System.out
  public void setPrint() {
    System.out.print("[Set:");

    if (isEmpty())
      System.out.print("---");

    for (int i=0; i<a.length; i++) {
      if (isSet(i))
        System.out.print(" " + i);
    }

    System.out.print("]\n");
  }

  // return true if two sets are equal
  public boolean isEqualTo(IntegerSet otherSet) {
    for(int i=0; i<a.length; i++) {
      if (otherSet.isSet(i) != isSet(i))
        return false;
    }
    return true;
  }

测试代码:

import java.util.Scanner;

public class IntegerSetTest {

    public static void main(String args[]) {

        IntegerSet setA = new IntegerSet();
        IntegerSet setB = new IntegerSet();

        Scanner scan = new Scanner(System.in);
        int input;

        do {            
            // Just for formatting purposes...
            System.out.println();            
            System.out.println("setA: " + setA);
            System.out.println("setB: " + setB);
            System.out.println("1) insertElement into setA");
            System.out.println("2) deleteElement from setA");
            System.out.println("3) insertElement into setB");
            System.out.println("4) deleteElement from setB");
            System.out.println("5) intersection of setA and setB");
            System.out.println("6) union of setA and setB");
            System.out.println("7) equality of setA and setB");
            System.out.println("Select from the menu above (or 0 to exit): ");
            input = scan.nextInt();

            switch(input) {
                case 1: 
                    System.out.print("Enter an element to insert into setA: ");
                    setA.insertElement(scan.nextInt());
                    break;
                case 2:
                    System.out.print("Enter an element to delete from setA: ");
                    setA.deleteElement(scan.nextInt());                  
                    break;
                case 3:    
                    System.out.print("Enter an element to insert into setB: ");
                    setB.insertElement(scan.nextInt());                 
                    break;
                case 4:
                    System.out.print("Enter an element to delete from setB: ");
                    setB.deleteElement(scan.nextInt());                  
                    break;
                case 5: // PROBLEM!   
                    System.out.println("The intersection of setA and setB is: " + IntegerSet.intersection(setA, setB));
                    break;
                case 6: // PROBLEM!
                    System.out.println("The union of setA and setB is: " + IntegerSet.union(setA, setB));
                    break;
                case 7:
                    System.out.println("setA and setB are " + (setA.isEqualTo(setB) ? "" : "un") + "equal");
                    break;
                default:
                    if (input != 0) {
                        System.out.println("\n*** Error, invalid input! ***\n");
                    }
            }          
        }while(input != 0);
    }

}
4

2 回答 2

0

您会注意到测试代码正在运行

IntegerSet.intersection(setA, setB)
IntegerSet.union(setA, setB)

setA.intersection(setB)
setA.union(setB)

区别在于第一个是对静态方法的调用,第二个是对实例方法的调用。您的实现包含实例方法,这些方法使用自身 ( this) 而不是接受第二个参数。

(简而言之,更改this第二个参数的值并将static关键字添加到您的方法中。此外,您必须更改对私有的引用才能a使用访问器方法。)

于 2012-12-04T04:53:14.643 回答
0
 public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) {

    for(int i=0; i<a.length; i++) {
      if (otherSet.isSet(i))
        nextSet.insertElement(i);
    }

    return nextSet;
  }

  public static IntegerSet intersection(IntegerSet otherSet, IntegerSet nextSet) {

    for(int i=0; i<a.length; i++) {
      if (!otherSet.isSet(i))
        nextSet.deleteElement(i);
    }

    return nextSet;
  }
于 2012-12-04T05:21:13.133 回答