程序功能:
-
使用布尔型数组表示0-100间的整数
-
实现了以下方法:
并集、交集、增减元素、格式化输出、判断两个集合是否相等
运行结果示例:
集合1:39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
集合2:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合3:76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
集合4:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合1和2的并集:39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
集合1和2的交集:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合2和3的并集:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
集合1和3的交集:—
集合1和3的并集:39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
集合1中删除一个元素44后为:39 40 41 42 43 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
集合2中删除一个元素31后为:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合2:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合4:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合2和集合4相等
集合2:40 42 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
集合4:40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
从集合2中删除44后,集合2和集合4不相等
代码:
- 实体类
/**
*
- 8.13 (Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0–100.
- The set is represented by an array of booleans. Array element a[i] is true if integer i is in the set.
- Array element a[j] is false if integer j is not in the set. The no-argument constructor initializes
- the array to the “empty set” (i.e., all false values). Provide the following methods: The static
- method union creates a set that’s the set-theoretic union of two existing sets (i.e., an element
- of the new set’s array is set to true if that element is true in either or both of the existing
- sets—otherwise, the new set’s element is set to false). The static method intersection creates
- a set which is the set-theoretic intersection of two existing sets (i.e., an element of the new
- set’s array is set to false if that element is false in either or both of the existing sets—otherwise,
- the new set’s element is set to true). Method insertElement inserts a new integer k into a set (by
- setting a[k] to true). Method deleteElement deletes integer m (by setting a[m] to false).
- Method toString returns a String containing a set as a list of numbers separated by spaces.
- Include only those elements that are present in the set. Use — to represent an empty set.
- Method isEqualTo determines whether two sets are equal. Write a program to test class IntegerSet.
- Instantiate several IntegerSet objects. Test that all your methods work properly.
*/
public class IntegerSet {
private boolean[] integerSet = new boolean[101];
//无参构造函数
public IntegerSet() {
for (int i=0;i<integerSet.length;i++)
integerSet[i] = false;
}
//以布尔值数组作为参数的构造函数
public IntegerSet(boolean[] integerSet) {
for (int i=0;i<integerSet.length;i++)
this.integerSet[i] = integerSet[i];
}
/**
*
* @param integerSet1
* @param integerSet2
* @return 集合的并集(Union)
*/
public static IntegerSet union(IntegerSet integerSet1, IntegerSet integerSet2) {
boolean[] integerSet = new boolean[101];
for (int i=0;i<100;i++)
integerSet[i] = integerSet1.integerSet[i] || integerSet2.integerSet[i];
return new IntegerSet(integerSet);
}
/**
*
* @param integerSet1
* @param integerSet2
* @return 集合的交集(intersection)
*/
public static IntegerSet intersection(IntegerSet integerSet1, IntegerSet integerSet2) {
boolean[] integerSet = new boolean[101];
for (int i=0;i<100;i++)
integerSet[i] = integerSet1.integerSet[i] && integerSet2.integerSet[i];
return new IntegerSet(integerSet);
}
/**
* 增加指定整数到整数集
* @param i
*/
public void insertElement(int i) {
integerSet[i] = true;
}
/**
* 从整数集中移除指定整数
* @param i
*/
public void deletetElement(int i) {
integerSet[i] = false;
}
/**
* 格式化输出IntegerSet对象中的内容
*/
public String toString() {
int count =0;
String integers ="";
//判断集合中的整数值的数量。
for (int i=0;i<integerSet.length;i++) {
if (integerSet[i])
++count;
}
//如果大于等于1,则依次输出整数值。
if (count>=1) {
for (int i=0;i<integerSet.length;i++) {
count =0; //格式化输出前,清空count
if (integerSet[i]) {
integers += String.format("%d ",i);
++count;
}
if (count>0 && (count%10 ==0))
integers += "%n" ; //此处的换行在Eclipse控制台中未起作用,原因尚未查明
}
}
else //如果为0,则输出“---”(空集)
integers = "---";
return integers;
}
/**
*
* @param integerSet
* @return 此集合是否与所比较的集合相等(一一对比,只要一个不相等,则返回false)
*/
public boolean isEqual (IntegerSet integerSet) {
for (int i=0;i<this.integerSet.length;i++) {
if (this.integerSet[i]!=integerSet.integerSet[i])
return false;
}
return true;
}
}
2. 测试类
public class IntegerSetTest {
private static IntegerSet is1= new IntegerSet();
private static IntegerSet is2= new IntegerSet();
private static IntegerSet is3= new IntegerSet();
private static IntegerSet is4= new IntegerSet();
public static void main(String[] args) {
for (int i=39;i<=75;i++) //向集合1中填充测试数据(39至75间的数)
is1.insertElement(i);
for (int i=39;i<=75;i++) { //向集合2,4中填充测试数据(39至75间的偶数)
if (i%2==0) {
is2.insertElement(i);
is4.insertElement(i);
}
}
for (int i=76;i<=90;i++) { //向集合3中填充测试数据(76至90间的偶数)
is3.insertElement(i);
}
System.out.printf("集合1:%s%n%n",is1);
System.out.printf("集合2:%s%n%n",is2);
System.out.printf("集合3:%s%n%n",is3);
System.out.printf("集合4:%s%n%n",is4);
System.out.printf("集合1和2的并集:%s%n%n",IntegerSet.union(is1, is2));
System.out.printf("集合1和2的交集:%s%n%n",IntegerSet.intersection(is1, is2));
System.out.printf("集合2和3的并集:%s%n%n",IntegerSet.union(is2, is3));
System.out.printf("集合1和3的交集:%s%n%n",IntegerSet.intersection(is1, is3));
System.out.printf("集合1和3的并集:%s%n%n",IntegerSet.union(is1, is3));
is1.deletetElement(44);
System.out.printf("集合1中删除一个元素44后为:%s%n%n",is1); //44不再打印出来
is2.deletetElement(31);
System.out.printf("集合2中删除一个元素31后为:%s%n%n",is2); //保持不变
System.out.printf("集合2:%s%n%n",is2);
System.out.printf("集合4:%s%n%n",is4);
System.out.printf("集合2和集合4%s相等%n%n",is2.isEqual(is4)?"":"不");
is2.deletetElement(44);
System.out.printf("集合2:%s%n%n",is2);
System.out.printf("集合4:%s%n%n",is4);
System.out.printf("从集合2中删除44后,集合2和集合4%s相等%n%n",is2.isEqual(is4)?"":"不");
}