Question: How do I test the methods in the java class wordOf32Bits_test that I made in class wordOf32Bits. I included the class realisticBit for reference. public
How do I test the methods in the java class wordOf32Bits_test that I made in class wordOf32Bits. I included the class realisticBit for reference.
public class wordOf32Bits{
private bit[] bits;
// constructor
public wordOf32Bits() {
// allocate bits array
bits = new bit[32];
// initialise with 0 bits
for (int i = 0; i < 32; i++) {
bits[i] = new bit();
//bits.getValue();
}
}
public wordOf32Bits(bit[] other) {
bits = new bit[32];
for (int i = 0; i < 32; i++) {
bits[i] = new bit(other[i].getValue());
//this.bits[i] = new bit();
}
}
public bit getBit(int i) {
// check if i is in range
if (i < 0 || i >= 32) {
// i is out of range
// return null
throw new IllegalArgumentException("Argument i out of range");
}
// else i is in range
return bits[i];
}
public void setBit(int i, bit value) {
// sets a given bit
// only update if i in range
if (i >= 0 && i < 32) {
bits[i] = value;
} else {
throw new IllegalArgumentException("Argument i out of range");
}
}
public wordOf32Bits and(wordOf32Bits other){
wordOf32Bits word = new wordOf32Bits();
for(int i = 0; i < 32; i++) {
word.setBit(i, this.bits[i].and(other.bits[i]));
return word;
}}
//THIS IS WHAT NEEDS HELP
public class wordOf32Bits_test{
wordOf32Bits wordOf32Bits = new wordOf32Bits();
private bit[] bits;
public static void main(String[] args) throws Exception {
runtests();
}
public void getBitTest() throws Exception{
}
public void setBitTest() throws Exception{
}
public void wordOf32BitsAndTest() throws Exception{
}
public static void runtests() throws Exception {
}
}
public class realisticBit { private int value; // constructor to initialize the realisticBit value to 0 public realisticBit() { value = 0; }
// method to set the realisticBit's value to passed value @Override public void set(int value) { if(value >= 0 && value <= 1) // validate input value is 0 or 1 this.value = value; else // invalid value, throw exception throw new IllegalArgumentException("ERROR: RealisticBit can be set to 0 or 1"); }
// method to toggle the value from 0 to 1 or 1 to 0 @Override public void toggle() { if(value == 0) value = 1; else value = 0; }
// method to set value to 1 @Override public void set() { value = 1; }
// method to set value to 0 @Override public void clear() { value = 0; }
// method to return realisticBit's value @Override public int getValue() { return value; }
// method to perform and on two realisticBits and return a new realisticBit set to the result @Override public realisticBit and(realisticBit other) { realisticBit result = new realisticBit(); // create a new realisticBit for the result if(value == 0) // if any one of the value is 0, set result to 0 result.clear(); else { if(other.getValue() == 0) result.clear(); else // if both values are 1 , set result to 1 result.set(); } return result; }
// method to perform or on two realisticBits and return a new realisticBit set to the result @Override public realisticBit or(realisticBit other) { realisticBit result = new realisticBit(); // create a new realisticBit for the result if(value == 1) // if any one of the value is 1, set the result to 1 result.set(); else { if(other.getValue() == 1) result.set(); else // both values are 0, set result to 0 result.clear(); } return result; }
// method to perform xor on two realisticBits and return a new realisticBit set to the result @Override public realisticBit xor(realisticBit other) { realisticBit result = new realisticBit(); // create a new realisticBit for the result if(value == 1) // if one of the realisticBit's value is 1, set result to 1, else set it to 0 { if(other.getValue() == 0) result.set(); else result.clear(); }else { if(other.getValue() == 1) result.set(); else result.clear(); } return result; }
// method to perform not on the existing realisticBit, returning the result as a new realisticBit @Override public realisticBit not() { if(value == 0) set(); else clear(); return this; } // method to return String representation of the realisticBit public String toString() { return value+""; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
