CS 304 Homework Assignment 2 Due: 11:59pm, Thursday, February 11th This assignment is scored out of 53. It consists of 2 programming questions. When you submit, you are required to create a folder...

1 answer below »
i need help tp complete this


CS 304 Homework Assignment 2 Due: 11:59pm, Thursday, February 11th This assignment is scored out of 53. It consists of 2 programming questions. When you submit, you are required to create a folder with your name (Last name first, then First name), CS304, HW2, e.g., LastName_FirstName_CS304_HW2. Put all your Java programs (*.java) as well as output files in the same folder. Zip this folder, and submit it as one file to Desire2Learn. Do not hand in any printouts. Triple check your assignment before you submit. If you submit multiple times, only your latest version will be graded and its timestamp will be used to determine whether a late penalty should be applied. Programming Questions In the folder that is provided with this homework assignment, there are seven files: NumberStack.java: an interface that defines the NumberStack ADT LNode.java: a class that defines a node in linked lists LinkedNumberStack.java: a linked list based implementation for the NumberStack ADT CharStack.java: an array based implementation for a char stack ADT StackApps.java: a class that implements binary conversion and big integer subtraction using CharStack TestStack.java: a driver class that tests the implementations of your LinkedNumberStack and StackApps classes testNumbers.dat: a binary file that contains 5 test decimal numbers and 15 pairs of big integers P1 (28pts) a. Completing the LinkedStack class In the LinkedNumberStack class, you are required to implement the following methods: push(int v) – This method adds an integer to the top of the stack. pop() – This method removes an element from the top of the stack and returns the element. It throws a RuntimeException "pop attempted on an empty stack" if this operation is attempted on an empty stack. size() – This method returns the number of elements on the stack. Note that you are only supposed to touch the above three methods. You are NOT allowed to create any other methods, instance variables, or make any changes to methods other than these three methods or files other than "LinkedNumberStack.java". Points will be taken off if you fail to follow this rule. b. Code Testing You are provided with a test driver implemented by "TestStack.java" (Do not make any changes to this file!) so there is no need to write your own. Once you have completed the methods, you can run the test. You should create a plain text file named "output-P1.txt", copy and paste the output corresponding to Problem 1 (if your code crashes or does not compile, copy and paste the error messages) to this file and save it. P2 (25pts) a. Completing the StackApps class In this programming question, you are required to implement two methods, a binary number conversion and a big integer subtraction. (1) Binary number is the fundamental data representation in computer science. However, in most mathematical computations, people usually use decimal because it’s more intuitive and simpler. In the StackApps class, the decToBin() method takes an decimal integer and converts it to a binary number. The digits of the result binary number are stored in a char stack, stackBinary. Make sure you have the digits pushed on to the stack in the correct order. For example, the decimal number 123 is converted to binary number 1111011. Do not create any arrays or use anything from the Character, Integer, Double, and String classes in your implementation, including but not limited to Integer.parseInt, Integer.toString, Integer.toBinaryString, Character.getNumericValue, Character.forDigit. In general, you are only supposed to deal with the digits at the char and int level and make use of stacks. (2) When an integer has more than a certain number of digits, it is represented using scientific notation. The int type itself cannot be used to store numbers with more than 10 digits. In some cases, this may be inconvenient as people usually expect complete numbers and doing big integer computations are inevitable. The subtractBigInteger() method takes two numbers as CharStacks num1 and num2, and subtract the number stored in num2 from the one stored in num1, i.e., num1 - num2. To simply implementation, you can assume that the number stored in num1 is no less than the number stored in num2. For example, when subtracting 181,749 from 314,739,847, the two stacks and the result stack would look like the following: In the subtractBigInteger() method, you are provided with a char stack, stackResult. You will need to perform subtractions between the two operands and save the result on the result stack. Pay attention to the order of the digits (see above figure for reference). You should '7' '4' '8' '9' '3' '7' '4' '1' '3' '9' '4' '7' '1' '8' '1' num1 num2 '3' '1' '4' '5' '5' '8' '0' '9' '8' stackResult low digit high digit high digit low digit compute the subtraction between the two big numbers digit by digit (from low to high). Be sure to take care of borrows. Do not create any arrays or import the java.math.BigInteger library or anything from the Character, Integer, Double, and String classes in your implementation! Same as above, you are only supposed to deal with the digits at the char and int level and make use of stacks. Note that you are only supposed to touch the above two methods. You are NOT allowed to create any other methods, instance variables, or make any changes to methods other than the two method or files other than "StackApps.java". Points will be taken off if you fail to follow this rule. b. Code Testing In "TestStack.java" (Again, do not make any changes to this file!), a test driver for StackApps is provided. You are also given a data file "testNumbers.dat" that contains 5 decimal numbers that need to be converted to binary and 15 pairs of big integers for subtraction. Depending on your programming environment, the data file might need to be placed in different folders so that you test driver can read it. For jGRASP, you can leave the data file in the same folder as your java files. For NetBeans, you should place it in your project folder in which you see directories like build, nbproject, and src, etc. Once you have completed the decToBin and subtractBigInteger methods, you can run the test. You should create a plain text file named "output-P2.txt", copy and paste the output corresponding to Problem 2 (if your code crashes or does not compile, copy and paste the error messages) to this file and save it. Grading Rubric: Code does not compile: -10 P1. Code compiles but crashes when executed: -5 Changes were made to things other than methods push, pop, and size: -5 Has output file: 5 Code passes 23 test cases: 23 (each test case worth 1 point) P2. Code compiles but crashes when executed: -5 Changes were made to things other than the required methods: -5 Arrays were used in the implementation: -15 Character, Integer, Double, and/or String were used in the implementation: -15 java.math.BigInteger was imported or used in the implementation: -15 Has output file: 5 Code passes 20 test cases: 20 (each test case worth 1 point) Sample output for P1: ================ Problem 1 ================ Test 1: size() ==> [Passed] Expected: 0 Yours: 0 Test 2: pop() ==> [Passed] Expected: a RuntimeException Yours: RuntimeException: "pop attempted on an empty stack" Test 3: push(10) and then isEmpty() ==> [Passed] Expected: false Yours: false Test 4: toString() ==> [Passed] Expected (from top to bottom): 10 Yours (from top to bottom): 10 Test 5: top() ==> [Passed] Expected: 10 Yours: 10 Test 6: push(20) and then toString() ==> [Passed] Expected (from top to bottom): 20 10 Yours (from top to bottom): 20 10 ... Test 23: pop() and then size() ==> [Passed] Expected: 0 Yours: 0 Total test cases: 23 Correct: 23 Wrong: 0 ================ End of Problem 1 ================ Sample output for P2: ================ Problem 2 ================ Test 1: decToBin(1) ==> [Passed] Expected: 1 Yours: 1 Test 2: decToBin(2) ==> [Passed] Expected: 10 Yours: 10 ... Test 19: (348759874975932475974395734967214809124234820 - 984576984576858763295876235) ==> [Passed] Expected: 348759874975932474989818750390356045828358585 Yours: 348759874975932474989818750390356045828358585 Test 20: (32475982374590873429573249057322356324596378265837465895236 - 237485963847265873246587364258) ==> [Passed] Expected: 32475982374590873429573249057084870360749112392590878530978 Yours: 32475982374590873429573249057084870360749112392590878530978 Total test cases: 20 Correct: 20 Wrong: 0 ================ End of Problem 2 ================
Answered Same DayFeb 10, 2021

Answer To: CS 304 Homework Assignment 2 Due: 11:59pm, Thursday, February 11th This assignment is scored out of...

Valupadasu answered on Feb 11 2021
150 Votes
CharStack.java
CharStack.java
// The CharStack class that implements a stack of characters
// Xiwei Wang
public class CharStack
{
    // instance variables
    private char[] m_array;
    private int m_index;

    // constructor
    public CharStack(int cap)
    {
        m_array = new char[cap];
        m_index = 0;
    }
    // check whether the stack is empty
    public boolean isEmpty()
    {
        if (m_index == 0)
            return true;
        else
            return false;
    }
    // return the element at the top of the stack
    public char top()
    {
        if (isEmpty())
            throw new RuntimeException("top attempted on an empty stack");
        else
            return m_array[m_index - 1];
    }
 
    // push a character onto the stack
    public void push(char c)
    {
        m_array[m_index] = c;
        m_index++;
    }

    // remove and return the element at the 
top of the stack
    public char pop()
    { 
        if (isEmpty())
            throw new RuntimeException("pop attempted on an empty stack");
        else
        {
            char c = m_array[m_index - 1];
            m_index--;
            return c;
        }
    }

    // return the number of elements on the stack
    public int size()
    {
        return m_index;
    }
    // return a string representation of the stack
    @Override
    public String toString()
    {
        String stackContent = "";

        for (int i = m_index - 1; i >= 0; i--)
            stackContent += m_array[i];

        return stackContent;
    }
}
LinkedNumberStack.java
LinkedNumberStack.java
// The linked list based implementation for the NumberStack ADT
// Your name here
public class LinkedNumberStack implements NumberStack
{
    // instance variable
    private LNode m_top;
    // check whether the stack is empty
    public boolean isEmpty()
    { 
        if (m_top == null)
            return true;
        else
            return false;
    }
    // check whether the stack is full
    public boolean isFull()
    {
        return false;
    }

    // return the element at the top of the stack
    public int top()
    { 
        if (isEmpty())
            throw new RuntimeException("top attempted on an empty stack");
        else
            return m_top.getInfo();
    }
    // push a value onto the stack
    public void push(int v)
    {
        // create new node with data
        LNode newNode = new LNode(v);
 
        // put top reference into new node link
        newNode.setLink(m_top);
 
        // update top reference
        m_top = newNode;
    }
    // remove and return the value at the top of the stack
    public int pop()
    { 
         // check for stack underflow
        int topV = -1;
        if (m_top == null) 
            throw new RuntimeException("top attempted on an empty stack");
        else {
            topV = m_top.getInfo();
            // update the top pointer to point to the next node
            m_top = (m_top).getLink();
        }
        return topV;
 

    }
    // return the number of elements on the stack
    public int size()
    { 
         LNode temp = m_top; 
         int count = 0; 
         while (temp != null) 
         { 
             count++; 
             temp = temp.getLink(); 
         } 
         return count; 
    }
    // return a string representation of the stack
    @Override
    public String toString()
    {
        String stackContent = "";
        LNode current = m_top;
        while (current != null)
        {
            stackContent += current.getInfo() + " ";
            current = current.getLink();
        }
        return stackContent;
    }
}
LNode.java
LNode.java
// The LNode class that represents a node in linked lists
// Do not make any changes to this file!
// Xiwei Wang
public class LNode 
{
    // instance variables
    private int m_info;
    private LNode m_link;

    // constructor
    public LNode(int info)
    {
        m_info = info;
        m_link = null;
    }

    // member methods
    public void setLink(LNode link)
    {
        m_link = link;
    }

    public LNode getLink()
    {
        return m_link;
    }

    public int getInfo()
    {
        return m_info;
    } 
}
NumberStack.java
NumberStack.java
// The NumberStack interface
// Do not make any changes to this file!
// Xiwei Wang
public interface NumberStack 
{
    boolean isEmpty();      // check whether the stack is empty
    boolean isFull();       // check whether the stack is full
    int top();              // return the element at the top of the stack
    int pop();              // remove and return the element at the top of the stack
    void push(int v);       // push a value onto the stack
    int size();             // return the number of elements on the stack
    @Override
    String toString();      // return a string representation of the stack
}
output-P1.txt
================ Problem 1 ================
Test 1: size() ==> [Passed]
Expected: 0
Yours: 0
Test 2: pop() ==> [Passed]
Expected: a RuntimeException
Yours: RuntimeException - "top attempted on an empty stack"
Test 3: push(10) and then isEmpty() ==> [Passed]
Expected: false
Yours: false
Test 4: toString() ==> [Passed]
Expected (from top to bottom): 10
Yours (from top to bottom): 10
Test 5: top() ==> [Passed]
Expected: 10
Yours: 10
Test 6: push(20) and then toString() ==> [Passed]
Expected (from top to bottom): 20 10
Yours (from top to bottom): 20 10
Test 7: top() ==> [Passed]
Expected: 20
Yours: 20
Test 8: pop() ==> [Passed]
Expected: 20
Yours: 20
Test 9: toString() ==> [Passed]
Expected (from top to bottom): 10
Yours (from top to bottom): 10
Test 10: push(30), push(40), push(50), and then toString() ==> [Passed]
Expected (from top to bottom): 50 40 30 10
Yours (from top to bottom): 50 40 30 10
Test 11: size() ==> [Passed]
Expected: 4
Yours: 4
Test 12: pop() and then top() ==> [Passed]
Expected: 40
Yours: 40
Test 13: pop() and then size() ==> [Passed]
Expected: 2
Yours: 2
Test 14: push(20) and then toString() ==> [Passed]
Expected (from top to bottom): 20 30 10
Yours (from top to bottom): 20 30 10
Test 15: size() ==> [Passed]
Expected: 3
Yours: 3
Test 16: pop() twice and then top() ==> [Passed]
Expected: 10
Yours: 10
Test 17: toString() ==> [Passed]
Expected (from top to bottom): 10
Yours (from top to bottom): 10
Test 18: pop() and then isEmpty() ==> [Passed]
Expected: 10, true
Yours: 10, true
Test 19: size() ==> [Passed]
Expected: 0
Yours: 0
Test 20: pop() ==> [Passed]
Expected: a RuntimeException
Yours: RuntimeException - "top attempted on an empty stack"
Test 21: push(70) and then toString() ==> [Passed]
Expected (from top to bottom): 70
Yours (from top to bottom): 70
Test 22: top() ==> [Passed]
Expected: 70
Yours: 70
Test 23: pop() and then size() ==> [Passed]
Expected: 0
Yours: 0
Total test cases: 23
Correct: 23
Wrong: 0
================ End of Problem 1 ================
output-P2.txt
================ Problem 2 ================
Test 1: decToBin(1) ==> [Passed]
Expected: 1
Yours: 1
Test 2: decToBin(2) ==> [Passed]
Expected: 10
Yours: 10
Test 3: decToBin(125) ==> [Passed]
Expected: 1111101
Yours: 1111101
Test 4: decToBin(65536) ==> [Passed]
Expected: 10000000000000000
Yours: 10000000000000000
Test 5: decToBin(181749) ==> [Passed]
Expected: 101100010111110101
Yours: 101100010111110101
Test 6: (2 - 1) ==> [Passed]
Expected: 1
Yours: 1
Test 7: (18 - 3) ==> [Passed]
Expected: 15
Yours: 15
Test 8: (15 - 10) ==> [Passed]
Expected: 5
Yours: 5
Test 9: (123 - 123) ==> [Passed]
Expected: 0
Yours: 0
Test 10: (100050007 - 60008) ==> [Passed]
Expected: 99989999
Yours: 99989999
Test 11: (214981273498237 - 12479832714) ==> [Passed]
Expected: 214968793665523
Yours: 214968793665523
Test 12: (10000000 - 1) ==> [Passed]
Expected: 9999999
Yours: 9999999
Test 13: (99999998 - 99995999) ==> [Passed]
Expected: 3999
Yours: 3999
Test 14: (17894912387490712 - 7853) ==> [Passed]
Expected: 17894912387482859
Yours: 17894912387482859
Test 15: (2314791876574390817491724309 - 1243098230867945) ==> [Passed]
Expected: 2314791876573147719260856364
Yours: 2314791876573147719260856364
Test 16: (7865484396536534653498653489756 - 0) ==> [Passed]
Expected: 7865484396536534653498653489756
Yours: 7865484396536534653498653489756
Test 17: (2502356342568397265346587326459 - 2502356342568397265346587326459) ==> [Passed]
Expected: 0
Yours: 0
Test 18: (8736485392502356342568397265346587326459863228356478 - 1093274987473843765971492387401927409237409221347) ==> [Passed]
Expected: 8735392117514882498802425772959185399050625819135131
Yours: 8735392117514882498802425772959185399050625819135131
Test 19: (348759874975932475974395734967214809124234820 - 984576984576858763295876235) ==> [Passed]
Expected: 348759874975932474989818750390356045828358585
Yours: 348759874975932474989818750390356045828358585
Test 20: (32475982374590873429573249057322356324596378265837465895236 - 237485963847265873246587364258) ==> [Passed]
Expected: 32475982374590873429573249057084870360749112392590878530978
Yours: 32475982374590873429573249057084870360749112392590878530978
Total test cases: 20
Correct: 20
Wrong: 0
================ End of Problem 2...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here