This assignment builds off of previous ones, I need this this one done using the other assignments I made. I will attach the previous assignments with all the files together. ICSI 404 – Assignment 4,...

1 answer below »

This assignment builds off of previous ones, I need this this one done using the other assignments I made. I will attach the previous assignments with all the files together.


ICSI 404 – Assignment 4, the multiplier and the ALU


This assignment builds on the previous assignment.


Part 1


With the ability to add and subtract, we can now multiply. You must create a new class, the multiplier. It should have the following as a method:


public static longword multiply (longword a, longword b)


You
must
use the multiplication method shown in class (shifting and using the rippleAdder). You may use a loop. You may not use built-in math. Multiplying 2 32-bit numbers should yield a 64-bit number. We are ignoring the UPPER 32 bits and keeping only the lower 32-bits.


You must provide a test file (multiplier_test.java) that implements void runTests() and call it from your main, along with your existing tests. As with the other tests, these tests must be independent of each other and there must be reasonable coverage. You cannot reasonably test all of the billions of possible combinations, but you can test a few representative samples. Ensure that you test with positive and negative numbers.



Part 2


Now that we have created several mathematical operations, we need to abstract this into a calculator, called an ALU. The ALU will accept 2 longwords and 5 bits. These 5 bits will indicate which operation is to be executed on the 2 longwords. One option for this interface:


public static longword doOp(bit[] operation, longword a, longword b)


Another option is to accept the bits as four different parameters. This is up to you. The mapping of bits to operations must look like this:


1000 – and


1001 – or


1010 – xor


1011 – not (not “a”; ignore b)


1100 – left shift ( “a” is the value to shift, “b” is the amount to shift it; ignore all but the lowest 5 bits)


1101 – right shift ( “a” is the value to shift, “b” is the amount to shift it; ignore all but the lowest 5 bits)


1110 – add


1111 – subtract


0111 - multiply



You
must
compare bits, not use getSigned() or getUnsigned() to determine the operation.



You must provide a test file (ALU_test.java) that implements void runTests() and call it from your main, along with your existing tests. As with the other tests, these tests must be independent of each other and there must be reasonable coverage. You cannot reasonably test all of the billions of possible combinations, but you can test a few representative samples.





You must submit buildable .java files for credit.



















































Rubric



Poor



OK



Good



Great



Comments



None/Excessive (0)



“What” not “Why”, few (5)



Some “what” comments or missing some (7)



Anything not obvious has reasoning (10)



Variable/Function naming



Single letters everywhere (0)



Lots of abbreviations (5)



Full words most of the time (8)



Full words, descriptive (10)



Unit Tests



None (0)



Partial Coverage


(7)



All methods covered, needs more cases (13)



All methods/cases covered (20)



Multiply



None (0)



Attempted (10)



Some cases work (20)



Completely working (30)



ALU



None (0)



Attempted (10)



Some cases work (20)



Completely working (30)



Answered 1 days AfterMar 03, 2021

Answer To: This assignment builds off of previous ones, I need this this one done using the other assignments I...

Sandeep Kumar answered on Mar 05 2021
142 Votes
ICSI_404_4/404_ALU/ALU.java
ICSI_404_4/404_ALU/ALU.java
public class ALU {

    public static longword doOp(bit[] operation, longword a, longword b)
    {
        // Initializing the data I will be using.
        Impl_Longword longword_OpA = new Impl_Longword();
        Impl_Longword longword_OpB = new Impl_Longword();
        // Copying a and b into separate data so I don't accidentally touches them.
        longword_OpA.original = a;
        longword_OpB.original = b;

        // Will be returning this.
        longword result = new longword();

        if(operation[operation.length-1].getValue() == 1)
        {
            // xx11
            if(operation[operation.length-2].getValue() == 1)
            {
                //x111
                if(operation[1].getValue() == 1)
                {
                    //1111
                    if(operation[0].getValue() == 1)
                    {
                        // 1111 - subtract
                        System.out.println("\n1111 - subtract");
                        System.out.pr
intln(longword_OpA.getSigned() + " - " + longword_OpB.getSigned() + " = " + (longword_OpA.getSigned()-
                                longword_OpB.getSigned()));
                        System.out.println();
                        printBit(a.getArray());
                        printBit(b.getArray());
                        System.out.println("____________________________________");
                        result = rippleAdder.subtract(a, b);
                    // 0111
                    }else
                    {
                        // 0111 - multiply
                        System.out.println("\n0111 - multiply\n");
                        System.out.println(longword_OpA.getSigned() + " x " + longword_OpB.getSigned() + " = " 
                        + longword_OpA.getSigned()*longword_OpB.getSigned());
                        System.out.println();
                        printBit(a.getArray());
                        printBit(b.getArray());
                        System.out.println("____________________________________");
                        result = Multiplier.multiply(a, b);
                    }
                // x011 (there is only one choice that have -011 as the last three digits)
                }else
                {
                    // 1011 - not
                    System.out.println("\n1011 - not");
                    printBit(a.getArray());
                    System.out.println("____________________________________");
                    result = longword_OpA.not();
                }
            // xx01
            }else
            {
                // x101 (refer to the top for x011)
                if(operation[1].getValue() == 1)
                {
                    // 1101 - right shift
                    System.out.println("1101 - right shift");
                    printBit(a.getArray());
                    System.out.println("____________________________________");
                    result = longword_OpA.rightShift(longword_OpB.getSigned());
                // x001 (refer to the top for x011)
                }else
                {
                    // 1001 - or
                    System.out.println("\n1001 - or");
                    printBit(a.getArray());
                    printBit(b.getArray());
                    System.out.println("____________________________________");
                    result = longword_OpA.or(b);
                }
            }
        // xxx0
        }else
        {
            // xx10
            if(operation[operation.length-2].getValue() == 1)
            {
                // x110 (refer to the top for x011)
                if(operation[1].getValue() == 1)
                {
                    // 1110 - add
                    System.out.println("\n1110 - add");
                    System.out.println(longword_OpA.getSigned() + " + " + longword_OpB.getSigned() + " = " 
                    + (longword_OpA.getSigned()+longword_OpB.getSigned()));
                    System.out.println();
                    printBit(a.getArray());
                    printBit(b.getArray());
                    System.out.println("____________________________________");
                    result =rippleAdder.add(a, b);
                // x010 (refer to the top for x011)
                }else
                {
                    // 1010 - xor
                    System.out.println("\n1010 - xor");
                    printBit(a.getArray());
                    printBit(b.getArray());
                    System.out.println("____________________________________");
                    result = longword_OpA.xor(b);
                }
            // xx00 
            }else
            {
                // x100 (refer to the top for x011)
                if(operation[1].getValue() == 1)
                {
                    // 1100 - left shift
                    System.out.println("1100 - left shift");
                    printBit(a.getArray());
                    System.out.println("____________________________________");
                    result = longword_OpA.leftshift(longword_OpB.getSigned());
                    // x000 (refer to the top for x011)
                }else
                {
                    // 1000 - and
                    System.out.println("\n1000 - and");
                    printBit(a.getArray());
                    printBit(b.getArray());
                    System.out.println("____________________________________");
                    result = longword_OpA.and(b);
                }
            }
        }

        // And then return the result. Which will be unique because there should be only one operation gets actived. 
        return result;
    }

    public static void printBit(bit[] toPrint)
    {
        int count = 0;
        for(int i = 0; i < toPrint.length; i++)
        {
            System.out.print(toPrint[i].getValue());
            count++;
            if(count == 8)
            {
                System.out.print(",");
                count = 0;
            }
        }
        System.out.println();
    }
}
ICSI_404_4/404_ALU/ALU_Test.java
ICSI_404_4/404_ALU/ALU_Test.java
public class ALU_Test {
    public void runTest()
    {

        long[] testData = {1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 111};
        Impl_Longword longword1 = new Impl_Longword();
        Impl_Longword longword2 = new Impl_Longword();


        int[] firstT = {200, -10};
        int[] secondT = {-3, 555};
        int shiftAmount = 3;


        for(int j = 0; j < firstT.length; j++)
        {
            System.out.println("\n-----------------------------------------------------\n");
            System.out.println("Test " + j + "\nA: " + firstT[j] + "\nB: " + secondT[j]);
        for(int i = 0; i < testData.length; i++)
        {
            longword1.set(firstT[j]);
            longword2.set(secondT[j]);
            if(i == 4 )
            {
                longword2.set(shiftAmount);
                System.out.println("left shift: " + shiftAmount);
                longword1.printBit(ALU.doOp(intTobinary(testData[i]), longword1.original, longword2.original).getArray());
            }else if(i == 5)
            {
                longword2.set(shiftAmount);
                System.out.println("right shift: " + shiftAmount);
                longword1.printBit(ALU.doOp(intTobinary(testData[i]), longword1.original, longword2.original).getArray());
            }else
            {
            longword1.printBit(ALU.doOp(intTobinary(testData[i]), longword1.original, longword2.original).getArray());
            }
            System.out.println();
        }
        }
    }

    public bit[] intTobinary(long theInt)
    {
        bit[] result = new bit[4];
        long holder = theInt;
        for(int i = 3; i >= 0; i--)
        {
            bit lastDigit = new bit();
            lastDigit.setValue((int)holder%10);
            result[i] = lastDigit;
            holder = holder / 10;
        }
        return result;
    }
}
ICSI_404_4/404_ALU/bit.java
ICSI_404_4/404_ALU/bit.java
public class bit {

    private int value;

    // the getter
    public int getValue() {
        return value;
    }
    // the setter
    public void setValue(int value) {
        this.value = value;
    }

}
ICSI_404_4/404_ALU/Driver.java
ICSI_404_4/404_ALU/Driver.java
public class Driver {

    public static void main(String[] args)
    {

        ALU_Test hi = new ALU_Test();

        hi.runTest();
    }
}
ICSI_404_4/404_ALU/ILongword.java
ICSI_404_4/404_ALU/ILongword.java
public interface ILongword {

    // Get bit i
    bit getBit(int i);

    // set bit i's value
    void setBit(int i, bit value);

    // and two longwords, returning a third
    longword and(longword other);

    // or two longwords, returning a third
    longword or(longword other);

    // xor two longwords, returning a third
    longword xor(longword other);

    // negate this longword, creating another
    longword not();

    // rightShift this longword by amount bits, creating a new longword
    longword rightShift(int amount);

    // leftShift this longword by amount bits, creating a new longword
    longword leftshift(int amount);

    // returns a comma separated string of 0's and 1's: "0,0,0,1,1,0,1"
    @Override
    String toString();

    // returns the value of this longword as a long
    long getUnsigned();

    // returns the value of this longword as an int
    int getSigned();

    // copies the values of the bits from another longword into this one
    void copy(longword other);

    // set the value of the bits of this longword(used for tests)
    void set(int value);
}
ICSI_404_4/404_ALU/Impl_Bit.java
ICSI_404_4/404_ALU/Impl_Bit.java
import java.lang.Math;
public class Impl_Bit implements I_Bit {

    // This is to store each instances of the bit class
    bit bitHolder = new bit();

    public void set(int value)
    {
        bitHolder.setValue(value);
    }

    public void toggle()
    {
        bitHolder.setValue(Math.abs(bitHolder.getValue() - 1 ));

    }
    public void set()
    {
        bitHolder.setValue(1);
    }

    public void clear()
    {
        bitHolder.setValue(0);
    }

    public int getValue()
    {
        return bitHolder.getValue();
    }

    public bit and(bit other)
    {
        bit andBit = new bit();

        if(bitHolder.getValue() == other.getValue())
        {
            if(bitHolder.getValue() == 0)
        ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here