Menu

(Solved) : Implement Class Skeleton Class Bc Grader Class Please Implement 4 Operations Public Static Q44142704 . . .

Implement this class?

Overview In this assignment you will implementa Java class that can perform the basic arithmetic operations adid, subtract, m

Skeleton Class:

package BinaryCalculator; * Class with methods to implement the basic arithmetic operations by * operating on bit fields. * T

BC Grader Class

1 package BinaryCalculator; B+ import java.io.FileWriter;. in 7 public class Binary CalculatorGrader mm private static final

if(DO_SUB) { n_tests++; BitField res = BinaryCalculator.subtract (arg1.copy (), arg2.copy(); BitField exp = computeResult(arg

n correct++; else { if(!res[0].equals(exp[0]) || !res [1].equals(exp[1])){ System.out.println(String.format(FAIL: <%> | <%s>return null; 13 = 11 / 12; r = 11 % 12; out[@] = trimBits(13, argi.size(); out[1] = trimBits(r, arg1.size()); return out; pri

Please Implement these 4operations:

public static BitField add(BitField a, BitFieldb)
{
      
   if(null == a || null == b || a.size() !=b.size()){
   throw newIllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannotbe null and must be the same length.”);
   }
   return new BitField(a.size());
}

public static BitField subtract(BitField a, BitFieldb)
{
   if(null == a || null == b || a.size() !=b.size()){
   throw newIllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannotbe null and must be the same length.”);
   }
   return new BitField(a.size());
}

public static BitField multiply(BitField a, BitFieldb)
{
   if(null == a || null == b || a.size() !=b.size()){
   throw newIllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannotbe null and must be the same length.”);
   }
   return new BitField(a.size());
}

public static BitField[] divide(BitField a, BitFieldb)
{
   if(null == a || null == b || a.size() !=b.size()){
   throw newIllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannotbe null and must be the same length.”);
   }

Overview In this assignment you will implementa Java class that can perform the basic arithmetic operations adid, subtract, multiply divide) on binary numbers using only logical operatore not using the actual mathematical operates that Jasa lady supports) Askeleton for the implementation is provided and can be retrieved from Canvas The zip file contains a docs/folder that has Javadoc for all the classes There are the main classes in this repository clated to this assignment: 1. BitField – this is a simple class that presents a hitfield a certain number of his long. It is basically a wrapper around a Boolean way with some convenience functions 2. HimaryCalculator – this is the section code for this assument. It has 4 methods (ad subtract, multiply, divide) that must be implemented for this assignment. You MAY NOT change the signature of these methods, they should be self-explanatory. The divide method is different from the others as i rma B a rray of 2 elements, the first of which is the quotient, the second is the remainder 3. BinaryCalculator Grader-this class generales random bit fields and then calls the four functions that must be implemented in Binary Calculator. It then determines if the returned values are correct. You can change the behavior of this class by modifying the constanta the top to focus on a single operation and/or use fewer iterations while you are working on the code For all operation, the resulting BitField objects must have the same number of bits as the input on this implies that if overfo Indrew the result that is shown will not be mathematically co -THIS IS THE EXPECTED BEHAVIOR Maximum Number of Bits Your program should be able to handle binarymbers up to 60 bits long Divide By Zero When asked to divide by your code should return from the divide method Signed versus Unsigned All of the binary values are considered to be signed with the most significant (MSB) being the sign bil, regardless of how many hits there are. Take care to understand the rules for sig of both the quotient and remainder in division package BinaryCalculator; * Class with methods to implement the basic arithmetic operations by * operating on bit fields. * This is the skeleton code for the COMP2691 Binary Calculator Assignment. public class Binary Calculator public BinaryCalculator() { super(); // TODO Auto-generated constructor stub } public static BitField add(BitField a, BitField b) if(null == a || null == b || a.size() != b.size()) { throw new IllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) return new BitField(a.size(); public static BitField subtract(BitField a, BitField b) if(null == a || null == b || a.size() != b.size()){ throw new IllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) return new BitField(a.size()); public static BitField multiply(BitField a, BitField b) if(null == a || null == b || a.size() != b.size()) { throw new IllegalArgumentException (“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) return new BitField(a.size(); public static BitField[] divide (BitField a, BitField b) if(null == a || null == b || a.size() != b.size()) { throw new IllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) // Return both the quotient and the remainder BitField() out = new BitField [ 2 ]; out[@] = new BitField(a.size()); // quotient out[1] = new BitField(a.size(); // remainder return out; 1 package BinaryCalculator; B+ import java.io.FileWriter;. in 7 public class Binary CalculatorGrader mm private static final boolean DO_ADD = true; private static final boolean DO_SUB = true; private static final boolean DO_MUL = true; private static final boolean DO_DIV = true; private static final int NUM_ITERATIONS = 100; private static final int MAX_BITS = 60; private static Random RAND = new Random(1); m in in public static void main(String[] args) throws Exception m m long n_tests = 0; long n_correct = 0; for(int i=0; i<NUM_ITERATIONS; i++) { // pick the number of bits to use. int n_bits = RAND.nextInt(MAX_BITS) +1; // add 1 because zero bits is not valid m si in BitField arg1 = randomBitField(n_bits); BitField arg2 = randomBitField(n_bits); m m m if(DO_ADD) { n_tests++; BitField res = BinaryCalculator.add(arg1.copy (), arg2.copy(); BitField exp = computeResult(arg1, “+”, arg2)[0]; if(!res.equals(exp)) { System.out.println(String.format(“FAIL: <%5> + <%s> expected <%s> but was <%>”, argi, arg2, exp, res)); m in in mmm else { n_correct++; if(DO_SUB) { if(DO_SUB) { n_tests++; BitField res = BinaryCalculator.subtract (arg1.copy (), arg2.copy(); BitField exp = computeResult(arg1, “-“, arg2)[0]; if(!res.equals(exp)) { System.out.println(String.format(“FAIL: <%s> – <%5> expected <%s> but was <%>”, argi, arg2, exp, res)); else { n_correct++; } if(DO_MUL) { n_tests++; BitField res = BinaryCalculator.multiply(arg1.copy (), arg2.copy(); BitField exp = computeResult(arg1, “*”, arg2)[]; if(!res.equals(exp)){ System.out.println(String.format(“FAIL: <%s> * <%s expected <%s> but was <%>”, argi, arg2, exp, res)); else { n_correct++; if(DO_DIV) { n_tests++; BitField[] res = BinaryCalculator.divide (arg1.copy (), arg2.copy(); BitField[] exp = computeResult(arg1, “/”, arg2); // divide by zero if(exp == null) { if(res != null){ System.out.println(String.format(“FAIL: <%s> <%s> expected but was <%s remainder %>”, argi, arg2, res[1], res[1])); else { n_correct++; n correct++; else { if(!res[0].equals(exp[0]) || !res [1].equals(exp[1])){ System.out.println(String.format(“FAIL: <%> | <%s> expected <%s remainder %s > but was <%s remainder %>”, argi, arg2, exp[O], exp[1], res[0], res[1])); else { n_correct++; System.out.println(String.format(“FINAL SCORE: %4.2f %d out of %d correct)”, 100.0*((double)n_correct)/((double)n_tests), n_correct, n_tests)); private static BitField[] computeResult(BitField arg1, String op, BitField arg2) long 11 = arg1. tolongSigned(); long 12 = arg2.toLongSigned(); long 13, r = 0; BitField[] out = new BitField [ 2 ]; switch(op) { case “+”: 13 = 11 + 12; break; case “-“: 13 = 11 – 12; break; case “*”: 13 = 11 * 12; break; default: // division if(12 == 0){ return null: return null; 13 = 11 / 12; r = 11 % 12; out[@] = trimBits(13, argi.size(); out[1] = trimBits(r, arg1.size()); return out; private static BitField trimBits(long 1, int length) BitField out = new BitField(length); for(int i=0; i<length; i++) { if((1 & 0x1) == Ox1) { out.setTrue(i); 1 = 1 >>> 1; return out; private static BitField randomBitField(int n_bits) BitField out = new BitField(n_bits); for(int i=0; i<n_bits; i++){ out.set(i, RAND.nextBoolean(); return out; Show transcribed image text Overview In this assignment you will implementa Java class that can perform the basic arithmetic operations adid, subtract, multiply divide) on binary numbers using only logical operatore not using the actual mathematical operates that Jasa lady supports) Askeleton for the implementation is provided and can be retrieved from Canvas The zip file contains a docs/folder that has Javadoc for all the classes There are the main classes in this repository clated to this assignment: 1. BitField – this is a simple class that presents a hitfield a certain number of his long. It is basically a wrapper around a Boolean way with some convenience functions 2. HimaryCalculator – this is the section code for this assument. It has 4 methods (ad subtract, multiply, divide) that must be implemented for this assignment. You MAY NOT change the signature of these methods, they should be self-explanatory. The divide method is different from the others as i rma B a rray of 2 elements, the first of which is the quotient, the second is the remainder 3. BinaryCalculator Grader-this class generales random bit fields and then calls the four functions that must be implemented in Binary Calculator. It then determines if the returned values are correct. You can change the behavior of this class by modifying the constanta the top to focus on a single operation and/or use fewer iterations while you are working on the code For all operation, the resulting BitField objects must have the same number of bits as the input on this implies that if overfo Indrew the result that is shown will not be mathematically co -THIS IS THE EXPECTED BEHAVIOR Maximum Number of Bits Your program should be able to handle binarymbers up to 60 bits long Divide By Zero When asked to divide by your code should return from the divide method Signed versus Unsigned All of the binary values are considered to be signed with the most significant (MSB) being the sign bil, regardless of how many hits there are. Take care to understand the rules for sig of both the quotient and remainder in division
package BinaryCalculator; * Class with methods to implement the basic arithmetic operations by * operating on bit fields. * This is the skeleton code for the COMP2691 Binary Calculator Assignment. public class Binary Calculator public BinaryCalculator() { super(); // TODO Auto-generated constructor stub } public static BitField add(BitField a, BitField b) if(null == a || null == b || a.size() != b.size()) { throw new IllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) return new BitField(a.size(); public static BitField subtract(BitField a, BitField b) if(null == a || null == b || a.size() != b.size()){ throw new IllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) return new BitField(a.size()); public static BitField multiply(BitField a, BitField b) if(null == a || null == b || a.size() != b.size()) { throw new IllegalArgumentException (“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) return new BitField(a.size(); public static BitField[] divide (BitField a, BitField b) if(null == a || null == b || a.size() != b.size()) { throw new IllegalArgumentException(“BinaryCalculator.add(a,b): a and b cannot be null and must be the same length.”) // Return both the quotient and the remainder BitField() out = new BitField [ 2 ]; out[@] = new BitField(a.size()); // quotient out[1] = new BitField(a.size(); // remainder return out;
1 package BinaryCalculator; B+ import java.io.FileWriter;. in 7 public class Binary CalculatorGrader mm private static final boolean DO_ADD = true; private static final boolean DO_SUB = true; private static final boolean DO_MUL = true; private static final boolean DO_DIV = true; private static final int NUM_ITERATIONS = 100; private static final int MAX_BITS = 60; private static Random RAND = new Random(1); m in in public static void main(String[] args) throws Exception m m long n_tests = 0; long n_correct = 0; for(int i=0; i 1; return out; private static BitField randomBitField(int n_bits) BitField out = new BitField(n_bits); for(int i=0; i

Expert Answer


Answer to Implement this class? Skeleton Class: BC Grader Class Please Implement these 4 operations: public static BitField add(Bi…

OR