Java的;无法理解给定赋值的结构

时间:2014-01-03 13:07:11

标签: java structure

前一段时间我们已经有了一项任务,说我们应该制作一个Java程序,它给我们提供了两套装置的结合和结合(是'设置'对吗?我在德国生活)。无论如何,教授给了我们这种结构,我们应该填写所需的代码。

这是他给我们的:

public class CharSet {
    // Attributes
    private char[] set; // set as Array
    private int size; // number of set's elements

    // Constructors
    public CharSet() { ... }
    public CharSet(char[] set) { ... }

    // get-/set-methods
    public char[] getSet() { ... }
    public int getSize() { ... }
    public void setSet(char[] a) { ... }

    // Method-overlay of the class 'Object'
    public boolean equals(Object x) { ... }
    public String toString() { ... }

    // Methoden application
    public CharSet intersection(CharSet c) { ... }
    public CharSet union(CharSet c) { ... }

    // main-method
    public static void main(String[] args) { ... }
}

另外,翻译引用:

  

填写指定的班级。方法equals(Object x)将返回   如果当前设置和参数x相等,则为true。 [...] 方法   toString()应将当前设置为{2,b,a,x}。你当然知道   可以向类添加属性和方法。你可以使用的方法   String类,但不允许导入任何类

     

输入:

java CharSet {2,a,b,x} {b,1,2,3,a} {0,1,A,3,a} 
     

可能的输出:

Union of the first two sets: {2,a,b,x,1,3}
Intersection of the first two sets: {b,2,a}
Union of all three sets: {2,a,b,x,1,3,0,A}
Intersection of all three sets: {a}

如果他只是给了我没有给定结构的任务,我肯定会在2周前完成所有工作,但是像这样...... 我已经离开了大约3天了,我已经做了大约4天的研究,仍然无法弄清楚如何理解他给定的班级。 在这项任务中获得最低分(这意味着我不能写考试)并不会让我感到困扰,但它确实困扰了我很多,我不明白如何处理它。我真的很生气,因为我不理解其他人可能很容易掌握的东西。

我真的很感激,如果有人需要花一些时间并试着向我解释一下这个类的背后是什么......我的意思是,例如,两个CharSets和get / set方法有什么作用?去做?!我可以用最多3种方法编写代码来解决所有问题。 我的个人解决方案将是大约200行主要使用for循环与list.contains和list.add,但它不适合他的给定形式... meh

新年快乐:b

3 个答案:

答案 0 :(得分:2)

如果你至少知道一些基本的java,你的教授任务是非常直截了当的。 但是,编写方法和构造函数的结构使得在查看示例时有点令人困惑。

回答你的问题: get和set-methods应该返回并设置类中的变量。

给你一个动手的例子:

public class CharSet {
// Attributes
    private char[] set; // set as Array
    private int size; // number of set's elements

// Constructors
public CharSet() { 
    this.size = 0;
    this.set = new char[this.size];
}

public CharSet(char[] set) {
    this.set = set;
    this.size = set.length;
}

// get-/set-methods
public char[] getSet() { 
    return this.set;
}

public void setSet(char[] a) { 
    this.set = a;
}

}

当涉及到union和intersection-methods时,你将不得不使用循环并创建一个新的chars数组,你可以在创建一个新的CharSet时使用它。

toString和equal-methods应该[toString]返回此CharSet的字符串表示形式,并返回true或false,回答此CharSet等于另一个对象的问题。 在equals-method中,您需要进行一些类比较和转换,以便能够访问其内部变量(如果它是CharSet类型)。

示例:

CharSet other = null;
if(x instanceof CharSet) {
    other = (CharSet) x;
}
祝你好运!

答案 1 :(得分:1)

一些帮助,与其他答案结合起来:

for (int i = 0; i < set.length; ++i) {
    char ch = set[i];
    ...

或者

for (char ch : set) {

然后

    boolean found = false;
    for (char otherCh : charSet.getSet()) {
         if (ch == otherCh) {
             found = true;
             break;
         }
    }

使用固定大小的char[],您可能需要先计算匹配项,以便了解结果的数组大小:new char[n]

由于允许使用String,您可以使用它来构成增长结果。以下将删除重复的字符:

String result = "";
for (char ch : set) {
    if (result.indexOf(ch) == -1) {
        result += ch;
    }
}
set = result.toCharArray();

答案 2 :(得分:1)

这是我尝试提供所有方法/属性的描述,以便它可以帮助您理解:

// This is the set of characters represented as an array
// If the set is {a, b, c} then this array will have the elements
// a, b, and c is some order (order doesn't matter in a set)
private char[] set;
// This is the size of the set. If the set is {a, b, c} then the size is 3
private int size;

// Constructors
// This creates an empty set
public CharSet() { ... }
// This creates a set with the given characters in it. It is presumed
// that the parameter to this constructor may have repeated
// characters so as they are added to this set they must be 
// filtered out. For example if [a, b, c, c] is the parameter
// then the set will become {a, b, c}
public CharSet(char[] set) { ... }

// get-/set-methods
// This method returns this set as an array
// So if this set is {a, b, c} then this will return
// [a, b, c] or [a, c, b] or [c, a, b] or etc...
// all of these are valid outputs of this method
public char[] getSet() { ... }
// This method returns the size of this set
// If this set is {a, b, c} then this will return 3
public int getSize() { ... }
// This method throws away the current contents of this set
// and replaces them with the given characters. Again the input
// here may have duplicate characters so that has to be taken care of
// For example if the set was {a, b, c} and this method was called with 
// [b, c, d, d], then this set will become {b, c, d}
public void setSet(char[] a) { ... }

// Method-overlay of the class 'Object'
// This will return true if and only if 'x' is a CharSet that has the
// same characters as this CharSet. (Note: order of array representation
// does not matter)
public boolean equals(Object x) { ... }
// This will print this set as a String. For example if the set was 
// {a, b, c} then "{a, b, c}" or "{a, c, b}" or etc... is printed
public String toString() { ... }

// Methoden application
// This method returns the intersection of this set and the set given
// The set intersection will contain only elements common to both
// this set and the other set.
public CharSet intersection(CharSet c) { ... }
// This method returns the union of this set and the set given
// All elements of this set and the other set will be in the set union
public CharSet union(CharSet c) { ... }

// main-method
// This method is used for testing this class presumeably
public static void main(String[] args) { ... }