所以我解決了大部分問題,有了幫助,但是現在當我嘗試正確地打印IntegerSet時,它不會改變。程序輸出十六進制爲IntegerSet沒有任何輸入?
我不知道爲什麼會出現這種類型的問題,也許是因爲測試很糟糕?
This is how it remains.
setA: [email protected] // Any ideas why this happens?
setB: [email protected] // And this?
1) insertElement into setA
2) deleteElement from setA
3) insertElement into setB
4) deleteElement from setB
5) intersection of setA and setB
6) union of setA and setB
7) equality of setA and setB
Select from the menu above (or 0 to exit):
這是類代碼:
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);
}
public int lengthOfArray(){
return this.a.length;
}
public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) {
for(int i=0; i<otherSet.length(); i++) {
if (otherSet.isSet(i))
nextSet.insertElement(i);
}
return nextSet;
}
public static IntegerSet intersection(IntegerSet otherSet, IntegerSet nextSet) {
for(int i=0; i<otherSet.length(); i++) {
if (!otherSet.isSet(i))
nextSet.deleteElement(i);
}
return nextSet;
}
// 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 length() {
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:
System.out.println("The intersection of setA and setB is: " + IntegerSet.intersection(setA, setB));
break;
case 6:
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);
}
}