Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

B. Implementing a Packed Array: Nybbles.java Let\'s suppose we have an applicati

ID: 3596994 • Letter: B

Question

B. Implementing a Packed Array: Nybbles.java Let's suppose we have an application that strains our computer's primary memory capacity and need to fit large arrays of small integers into as little space as possible. We want a data type that provides an array of integers that are limited in range to-8 to 7.Such integers are representable in 4 bits (half a byte, also known as a nybble). So in principle, It ought to be possible to store N intogers in an N/8-Intoger array (packing 8 4-bit integers into each int). Fill in the template below to provide a suitable smal-int array type. Do not perform any additional new operations in the implementation (you may include as many as you want for testing, if you put them in a different fle). /Represents an array of integers each in the range -8..7. */ public class Nybbles f /*An array of size N. / public Nybbles (int N) / DON'T CHANGE THIS./ data - new intl (N+7) 81 this.N-N /Return the number of nybbles in me. */ public int size ) ( return N; /** Return my Kth integer, numbering from 0. Assumes 0

Explanation / Answer

Nybbles.java
------------------------------------------------------------------------
public class Nybbles {

    /** Maximum positive value of a Nybble. */
    public static final int MAX_VALUE = 7;

    /** Return an array of size N. */
    public Nybbles(int N) {
        // DON'T CHANGE THIS.
        _data = new int[(N + 7) / 8];
        _n = N;
    }

    /** Return the size of THIS. */
    public int size() {
        return _n;
    }

    /** Return the Kth integer in THIS array, numbering from 0.
     * Assumes 0 <= K < N. */
    public int get(int k) {
        if (k < 0 || k >= _n) {
            throw new IndexOutOfBoundsException();
        } else {
            int index = k / 8;
            int num = _data[index];
            int c = 0;
            for (int i = 0; i < 3; i += 1) {
                c += ((1 << (i + 4 * (k % 8))) & num) >> (4 * (k % 8));
            }
            int sign = (1 << (3 + 4 * (k % 8))) & num;
            if (sign > 0 && c == 0) {
                c = -8;
            } else if (sign > 0) {
                c ^= 1 << 0;
                c ^= 1 << 1;
                c ^= 1 << 2;
                c = (-1) * (c + 1);
            }
            return c;
        }

    }

    /** Set the Kth integer in THIS array to VAL. Assumes
     * 0 <= K < N and -8 <= VAL < 8. */
    public void set(int k, int val) {
        if (k < 0 || k >= _n) {
            throw new IndexOutOfBoundsException();
        } else if (val < (-MAX_VALUE - 1) || val > MAX_VALUE) {
            throw new IllegalArgumentException();
        } else {
            int i = k / 8;
            int c = _data[i];
            int bit1 = (val & 1) << (4 * (k % 8));
            int bit2 = (val & 2) << (4 * (k % 8));
            int bit3 = (val & 4) << (4 * (k % 8));
            int bit4 = (1 << (3 + 4 * (k % 8))) & val;
            c |= bit1;
            c |= bit2;
            c |= bit3;
            c |= bit4;
            _data[i] = c;
        }
    }

    // DON'T CHANGE OR ADD TO THESE.
    /** Size of current array (in nybbles). */
    private int _n;
    /** The array data, packed 8 nybbles to an int. */
    private int[] _data;
}
-------------------------------------------------------------------------------------------------------
NybblesTester.java
------------------------------------------------------------------
import org.junit.Test;
import static org.junit.Assert.*;

public class NybblesTester {

    @Test
    public void testPos() {
        Nybbles arr = new Nybbles(16);
        for (int i = 0; i < arr.size(); i += 1) {
            arr.set(i, i % 8);
        }
        for (int i = 0; i < arr.size(); i += 1) {
            assertEquals(i % 8, arr.get(i));
        }
    }

    @Test
    public void testNeg() {
        Nybbles arr = new Nybbles(16);
        for (int i = 0; i < arr.size(); i += 1) {
            arr.set(i, i % 8 - 8);
        }
        for (int i = 0; i < arr.size(); i += 1) {
            assertEquals(i % 8 - 8, arr.get(i));
        }
    }

    @Test
    public void testMixed() {
        Nybbles arr = new Nybbles(16);
        for (int i = 0; i < arr.size(); i += 2) {
            arr.set(i, i / 2 - 8);
            arr.set(i + 1, i / 2);
        }
        for (int i = 0; i < arr.size(); i += 2) {
            assertEquals(i / 2 - 8, arr.get(i));
            assertEquals(i / 2, arr.get(i + 1));
        }
    }

    public static void main(String[] args) {
        System.exit(ucb.junit.textui.runClasses(NybblesTester.class));
    }
}