Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this please. The following is a java class realisticBit which we need to convert to a realisticBit collection using array and then

Need help with this please. The following is a java class realisticBit which we need to convert to a realisticBit collection using array and then turn that into an integer inside the wordOf32Bits class. Then, implement the methods in the wordOf32Bits class. Test out some of the methods. First the wordOf32Bits class that I need help with and then below it, is the realisticBit class which is already complete.

Follow the steps inside the wordOf32Bits class

public class wordOf32Bits{

//Create must create a collection (array or array list) of RealisticBit (from class realisticBit) and use that for storage

//Turn the collection into an integer using Math.Pow()

//Implement on the following methods

realisticBit getRealisticBit(int i){} // Get realisticBit i

void setRealisticBit(int i, realisticBit value){} // set realisticBit i's value

wordOf32Bits and(wordOf32Bits other){} // and two wordOf32Bitss, returning a third

wordOf32Bits rightShift(int amount){} // rightshift this wordOf32Bits by amount realisticBits, creating a new wordOf32Bits

int getInteger(){} // returns the value of this wordOf32Bits as an int

void copy(wordOf32Bits other){}// copies the values of the realisticBits from another wordOf32Bits into this one

void set(int value){} // set the value of the realisticBits of this wordOf32Bits (used for tests)

}

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions