📄 javashy33.java
字号:
/********************************************************************************************
第4章习题5
定义一个整形集合类integerSet。这种类型的对象可以存储10个20至80之间的整数,即它的内部有一个整形数组存储数据。编程:
(1) 判断两个integerSet类对象S1和S2是否相等。提示:集合相等的前提是所有元素相等。
(2) 输出两个集合对象的交集。
(3) 输出两个集合对象的并集。
(4) 将一个整形数据插入到一个集合对象中。
(5) 从一个集合中删除某一个元素。
********************************************************************************************/
public class Javashy33{
private boolean isEmpty;
private final int size = 10;
private int current;
private int set[] = new int[size];
public Javashy33() {
this.isEmpty = true;
this.current = 0;
for (int i = 0; i < size; i++)
this.set[i] = 0;
}
public void Clear() {
this.isEmpty = true;
this.current = 0;
for (int i = 0; i < this.size; i++)
this.set[i] = 0;
}
public boolean MergeJavashy33s(Javashy33 arg1, Javashy33 arg2) {
if (arg1.isEmpty && arg2.isEmpty) {
this.Clear();
return true;
}
if (arg1.isEmpty == true) {
this.isEmpty = false;
this.current = arg2.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg2.set[i];
return true;
}
if (arg2.isEmpty == true) {
this.isEmpty = false;
this.current = arg1.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg1.set[i];
return true;
}
if (arg1.equal(arg2) == true) {
this.isEmpty = false;
this.current = arg1.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg1.set[i];
return true;
}
Javashy33 tmp = new Javashy33();
tmp.isEmpty = false;
tmp.current = this.current;
for (int i = 0; i < this.size; i++)
tmp.set[i] = this.set[i];
this.isEmpty = false;
this.current = arg1.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg1.set[i];
for (int i = 0; i < arg2.size; i++) {
if (this.isIn(arg2.set[i]) == false) {
if (this.current == 10) {
System.out.println("Javashy33 Over flow!");
this.isEmpty = false;
this.current = tmp.current;
for (int j = 0; j < this.size; j++)
this.set[j] = tmp.set[j];
return false;
}
this.InsertElement(arg2.set[i]);
}
}
return true;
}
public boolean IntersectJavashy33(Javashy33 arg1, Javashy33 arg2) {
if (arg1.isEmpty == true || arg2.isEmpty == true)
return true;
for (int i = 0; i < this.size; i++)
if (arg2.set[i] != 0 && arg1.isIn(arg2.set[i]) == true)
this.InsertElement(arg2.set[i]);
return true;
}
public boolean InsertElement(int arg) {
if (arg < 20 || arg > 80) {
System.out.println("The argument must >20 and <80");
return false;
}
if (this.isEmpty == true) {
this.isEmpty = false;
this.current++;
this.set[0] = arg;
return true;
}
if (this.current == 10)
return false;
if (this.isIn(arg) == true) {
System.out.println(arg + " is already in the Javashy33!");
return false;
}
for (int i = 0; i < this.size; i++) {
if (this.set[i] == 0) {
this.set[i] = arg;
this.current++;
return true;
}
}
return false;
}
public boolean DeleteElement(int arg) {
if (arg < 20 || arg > 80) {
System.out.println("The argument must >20 and <80");
return false;
}
// empty set
if (this.isEmpty == true)
return false;
else {
if (this.isIn(arg) == true) {
int pos = size - 1;
for (int i = 0; i < size; i++)
if (this.set[i] == arg)
pos = i;
this.current--;
if (this.current == 0)
this.isEmpty = true;
this.set[pos] = 0;
return true;
} else {
System.out.println(arg + " is not in the Javashy33!");
return false;
}
}
}
public boolean valid() {
for (int i = 0; i < this.size; i++) {
if (this.set[i] == 0)
continue;
if (this.set[i] < 20 || this.set[i] > 80)
return false;
}
return true;
}
public boolean equal(Javashy33 arg) {
for (int i = 0; i < size; i++)
if (this.isIn(arg.set[i]) == false)
return false;
return true;
}
public boolean isIn(int arg) {
if (this.isEmpty == true)
return false;
for (int i = 0; i < size; i++)
if (arg == set[i])
return true;
return false;
}
public void print() {
System.out.print("( ");
for (int i = 0; i < this.size; i++)
System.out.print(this.set[i] + " ");
System.out.print(")\n");
}
public static void main(String[] args) {
Javashy33 t1 = new Javashy33();
Javashy33 t2 = new Javashy33();
Javashy33 t3 = new Javashy33();
Javashy33 t4 = new Javashy33();
t1.InsertElement(30);
t1.InsertElement(33);
t1.InsertElement(38);
System.out.println("t1");
t1.print();
t2.InsertElement(30);
t2.InsertElement(32);
t2.InsertElement(38);
t2.InsertElement(43);
t2.InsertElement(54);
System.out.println("t2");
t2.print();
t3.MergeJavashy33s(t1, t2);
t3.print();
t4.IntersectJavashy33(t1, t2);
t4.print();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -