2

对于分配,我们假设修改自定义 BitString 类。我们需要实际编写代码的函数超过 10 个,而我被困在第一个函数上。这是该类的开始部分以及我尝试使用的一些方法:

public class BitString implements Cloneable {
    // An array to hold the bits that make up the bit string.
    private boolean bits[];
    /**
     * A constant that defines the size of the default bit string.
     */
    public static final int DEFAULT_SIZE = 8;

    /**
     * Creates a new, all false, bit string of the given size.
     */
    public BitString(int size) {
        if (size < 1) throw new IllegalArgumentException("Size must be positive");
        bits = new boolean[size];
    }

    /**
     * Creates a new all false bit string of size DEFAULT_SIZE.
     */
     public BitString() {
         this(DEFAULT_SIZE);
     }

     /**
      * Set the value of a bit string at the given index to true.
      */
     public void set(int index) {
         bits[index] = true;
     }

     /**
      * Set the value of a bit string at the given index to false.
      */
     public void clear(int index) {
         bits[index] = false;
     }

下面是我正在处理的方法(给出的唯一部分是方法和输入类型)我无法调用bits.set()bits.clear()他们正在执行的相同操作。编译时我得到

错误:无法对非静态字段位进行静态引用

在两个方法调用上。

public static BitString decimalToUnsigned(int n, int size) {
    //throw new UnsupportedOperationException("This function needs to be completed!");
    int result = 0;
    int multiplier = 1;
    int base = 2;

    while(n > 0) {
        int remainder = n % base;
        n = n / base;
        if (remainder == 0) {
            //value = false;
            try {
                //bits.clear(size);
                bits[size] = false;
            } catch (InsufficientNumberOfBitsException ie) {}
        } else {
            //value = true;
            try {
                //bits.set(size);
                bits[size] = true;
            } catch (InsufficientNumberOfBitsException ie) {}
        }
        result = result + remainder * multiplier;
        multiplier = multiplier * 10;
        size--;
    }

    System.out.println("Result..." + result);

    return(bits);
} 

谢谢你的帮助。

4

2 回答 2

1

我们必须在这里做一些假设:例如,静态方法是 BitString 上的方法。

鉴于此,该方法显然应该创建一个 BitString 对象,因为它返回一个。因此,它应该为您正在处理的参数创建您需要的大小之一。由于您有不允许调用 set 和 clear 方法的(任意的,有些愚蠢的)限制,因此您需要bits从您直接创建的 BitString 中访问该变量;由于静态方法在 BitString 类上,您可以这样做:

public static BitString decimalToUnsigned(int n, int size)
{
  // ...
  BitString bitString = new BitString(size);
  // ... loops, logic, etc. all to be put in here; when you're ready to
  // access the bits array, use:
  bitString.bits[index] = false;
  // ...
  // then when you're ready to return your BitString object, just:
  return bitString;
}

是的,位被声明为私有的,但这只是意味着它不能从类外部访问。静态方法在类中,尽管它不能使用成员变量,因为静态方法不对实例(除了它创建的实例之外)进行操作。

看看这是否能让你通过编译错误并进入你的逻辑。

ps 我不认为这是一个很好的任务;它会让您了解静态与非静态方法,但我认为有更好的方法可以做到这一点。并且说你必须使用并返回一个类但你不能调用它的方法,这几乎不是现实世界的场景。

于 2014-09-18T01:00:58.467 回答
0

在您的静态方法中,您需要一个 BitString 的实例来放入您的 vales。这就是我的做法:

public class BitString implements Cloneable {

  /** A constant that defines the size of the default bit string. */
  public static final int DEFAULT_SIZE = 8;

  // an array to hold the bits that make up the bit string
  private boolean bits[];

  /** Creates a new, all false, bit string of the given size. */
  public BitString(int size) {
    if (size < 1) {
      throw new IllegalArgumentException("size must be positive");
    }
    bits = new boolean[size];
  }

  /** Creates a new all false bit string of size DEFAULT_SIZE. */
  public BitString() {
    this(DEFAULT_SIZE);
  }

  /** Set the value of a bit string at the given index to true. */
  public void set(int index) { // might want to check index bounds
    bits[index] = true;
  }

  /** Set the value of a bit string at the given index to false. */
  public void clear(int index) { // might want to check index bounds
    bits[index] = false;
  }

  public String toString() { // one possible implementation, might not want to add leading 0's
    StringBuilder buf = new StringBuilder(bits.length);
    for (Boolean bit : bits) {
      buf.append(bit ? '1' : '0');
    }
    return buf.toString();
  }

  public static BitString decimalToUnsigned(int n, int size) {
    // throw new UnsupportedOperationException("this function needs to be completed");
    // might want to check that size is big enough

    // this is the key here: you need an instance of the object that has the bits array inside it
    BitString result = new BitString(size);
    while (n != 0 && size > 0) {
      size--; // use size to index into the BitString
      if ((n & 1) == 1) { // % 2 doesn't work well with negative numbers, you have to worry about +-1 then
        result.set(size); // set bit if needed
      }
      n = n >>> 1; // unsigned shift to the next bit
    }
    return result;
  }

  public static void main(String[] args) {
    // can be invoked with just decimalToUnsigned(42, 10) but I want to make it more clear
    BitString example1 = BitString.decimalToUnsigned(42, 10);
    System.out.println(example1);

    BitString example2 = BitString.decimalToUnsigned(-42, 10); // will treat -42 as unsigned
    System.out.println(example2);

    BitString example3 = BitString.decimalToUnsigned(-1, 33); // will treat -1 as unsigned giving 32 1's
    System.out.println(example3);
  }
}

它打印:

0000101010
1111010110
011111111111111111111111111111111

于 2014-09-18T01:50:59.677 回答