Question: Here is a java class bit which we need to convert to a bit collection(using array) and then turn that into an integer inside the
Here is a java class bit which we need to convert to a bit collection(using array) and then turn that into an integer inside the word class. Then, implement the methods in the word class. Test out some of the methods
public class bit { private int value; // constructor to initialize the bit value to 0 public bit() { value = 0; }
// method to set the bit'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: Bit 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 bit's value @Override public int getValue() { return value; }
// method to perform and on two bits and return a new bit set to the result @Override public bit and(bit other) { bit result = new bit(); // create a new bit 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 bits and return a new bit set to the result @Override public bit or(bit other) { bit result = new bit(); // create a new bit 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 bits and return a new bit set to the result @Override public bit xor(bit other) { bit result = new bit(); // create a new bit for the result if(value == 1) // if one of the bit'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 bit, returning the result as a new bit @Override public bit not() { if(value == 0) set(); else clear(); return this; } // method to return String representation of the bit public String toString() { return value+""; } }
Follow the steps inside the word class
public class word{
//Create must create a collection (array is best) of Bit (from class bit) and use that for storage
//Turn the collection into an integer using Math.Pow()
//Implement on the following methods
bit getBit(int i){} // Get bit i
void setBit(int i, bit value){} // set bit i's value
word and(word other){} // and two words, returning a third
word rightShift(int amount){} // rightshift this word by amount bits, creating a new word
int getInteger(){} // returns the value of this word as an int
void copy(word other){}// copies the values of the bits from another word into this one
void set(int value){} // set the value of the bits of this word (used for tests)
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
