我如何检查点是否是Set上的成员?

问题描述:

我有一个类Set,我应该从Set类两点 用户要求的阅读。我如何检查点是否是Set上的成员?

我有一些问题的方法membersubset

我没有任何想法是如何工作的,所以如果任何一个可以给我说,我能理解这种方法是如何工作的例子?

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

哦,哦,有一个名为两个变量'P'和'p'是危险的,或者至少非常混乱。如果你改变了,我会看看你的代码。 –

这应该是它,我会解释给你听:

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; 
} 
+1

我怀疑你被写为他或她的代码最好帮助提问者。请参阅[如何提出和回答问题的家庭作业?](http://meta.*.com/questions/334822/how-do-i-ask-and-answer-homework-questions)不怀疑你试图有帮助;只是还有其他方式更有帮助。 –

+1

你当然这样做了。 (1)有很多例子表明OP应该能够理解,更好地引用他或她的搜索引擎。 (2)我认为,提供全面和完成代码的答案往往会吸引像“给MED代码”,我们不喜欢对堆栈溢出问题的问题。 –

+1

这应该是一样的,@ alex213。试试看看。 –