我有一个课程Set
,我应该从Set
课程中读几点
用户问。
我在方法member
和subset
上遇到了一些问题。
我不知道它是如何工作的,所以如果有人可以给我任何一些例子,我可以理解这种方法是如何工作的?
class Set {
private point[] p;
public Set(int n) {
p = new point[n];
readSet(n);
}
private void readSet(int n) {
System.out.println("Please enter " + n + " points");
for (int i = 0; i < n; i++) {
int x = 0;
int y = 0;
p[i] = new point(x, y);
}
}
public void printSet() {
for (int i = 0; i < p.length; i++) {
System.out.print(p[i]);
}
}
public void shiftSet(int dx, int dy) {
for (int i = 0; i < p.length; i++) {
p[i].shift(dx, dy);
}
}
// returns if P is in the set, making use of the equals method in class Point
public boolean member(point P) {
for (int i = 0; i < p.length; i++) {
// ...
}
return true;
}
// returns if current set is a subset of S, making use of the member method
public boolean subset (Set S) {
// ...
return true;
}
}
答案 0 :(得分:2)
这应该是关于它的,我会向你解释:
public boolean member(point P) {
// search for P in p
for (int i = 0; i < p.length; i++) {
// if it is contained
if (p[i].equals(P)) {
// here it is
return true;
}
}
// couldn't find P
return false;
}
public boolean subset(Set S) {
// check for every point in p
for (int i = 0; i < p.length; i++) {
// whether it is contained in S
if (!S.member(p[i])) {
// can't be a true subset
return false;
}
}
// everything is as expected
return true;
}