CS 304 Homework Assignment 3 Due: 11:59pm, Thursday, February 25th This assignment is scored out of 69. It consists of 6 questions. The first 5 questions are to be completed on D2L as an online quiz....

1 answer below »
instructions in the pdf


CS 304 Homework Assignment 3 Due: 11:59pm, Thursday, February 25th This assignment is scored out of 69. It consists of 6 questions. The first 5 questions are to be completed on D2L as an online quiz. There is a programming question and you will need to put all your Java programs (*.java) as well as output files for this question in the folder named LastName_FirstName_CS304_HW3. 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. Short Answers Complete the quiz for this homework on D2L by the due date. You might see there is a time limit of 120 minutes on this quiz but it is not enforced so you can ignore it. Before you complete all questions, DO NOT submit! Doing so will prevent any further changes to the answers. You can save your answers for as many times as you want before submission. Programming Questions (41pts) a. Completing the RecursiveMethods class In the RecursiveMethods class, you are required to implement the following methods using recursive solutions (no looping statements): computePI(int n) – One remarkably simple formula for approximating the value of ? is the so-called Madhava–Leibniz series: ? 4 = 1 − 1 3 + 1 5 − 1 7 + 1 9 −⋯+/− 1 2?−1 . Note that the result of the series is a quarter of ?. The accuracy of the approximation is dependent on the value of n. This method takes an int parameter n and calculates the value of ?/4. Pay attention to the sign (positive or negative) of each term. upperStackRec(CharStack s) – This method takes a character stack and converts all lower case letters to upper case ones. Do NOT create any auxiliary data structure, including but not limited to array(s), queue(s), and list(s). Primitive variables are okay. reverseStringRec(String s) – This method reads a string and returns the string in the reversed order. numOccurrencesRec(LNode node, int n, int key) – This method takes as parameters a reference to the head of a linked list, a position specified by n, and a key. It returns the number of occurrences of the key in the linked list beginning at the n-th node. If n = 0, it means you should search in the entire linked list. If n = 1, then you should skip the first node in the list. Do NOT create any additional nodes or any other auxiliary structures (for instance, an array). Do NOT alter the data field of any nodes. Note that you are only supposed to touch the above methods. You are NOT allowed to create any other methods, instance variables, or make any changes to methods other than these four methods or files other than "RecursiveMethods.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 "TestRecursiveMethods.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.txt", copy and paste the output (if your code crashes or does not compile, copy and paste the error messages) to this file and save it. Grading Rubrics: Code does not compile: -10 Code compiles but crashes when executed: -5 Changes were made to things other than the required methods: -5 computePI was implemented in a non-recursive way: -8 computePI does not make use of its return value: -4 upperStackRec was implemented in a non-recursive way: -8 upperStackRec uses auxiliary data structures: -4 reverseStringRec was implemented in a non-recursive way: -8 reverseStringRec does not make use of its return value: -4 numOccurrencesRec was implemented in a non-recursive way: -12 numOccurrencesRec uses auxiliary data structures: -6 numOccurrencesRec alters the data field of linked list nodes: -8 Has output file: 5 Code passes 18 test cases: 36 (each test case worth 2 points) Sample output: Test 1: computePI(1) ==> [Passed] Expected: 1 Yours: 1.0 Test 2: computePI(4) ==> [Passed] Expected: 0.7238 Yours: 0.7238 ... Test 5: upperStackRec("a" (from top to bottom)) ==> [Passed] Expected: "A" (from top to bottom) Yours: "A" (from top to bottom) Test 6: upperStackRec("p?7" (from top to bottom)) ==> [Passed] Expected: "P?7" (from top to bottom) Yours: "P?7" (from top to bottom) ... Test 17: numOccurrencesRec([45->25->73->25->19->25->43->25], 1, 25) ==> [Passed] Expected: 4 Yours: 4 Test 18: numOccurrencesRec([45->25->73->25->19->25->19->45], 4, 19) ==> [Passed] Expected: 2 Yours: 2 Total test cases: 18 Correct: 18 Wrong: 0
Answered Same DayFeb 25, 2021

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

Sonu answered on Feb 25 2021
149 Votes
assignment8/CharStack.java
assignment8/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 emp
ty 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 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;
    }
}
assignment8/LNode.java
assignment8/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;
    } 
}
assignment8/RecursiveMethods (1).java
assignment8/RecursiveMethods (1).java
// The RecursiveMethods class that implements several recursive solutions
// Your name here
public class RecursiveMethods 

    // This method takes an int as the parameter and estimates the result of the
    // Madhava–Leibniz series. The result is then considered as an approximation
    // of pi/4. The method returns this value (pi/4).
    public double computePI(int n)
    {
          if(n == 1){
            return 1.0;
        }
        if(n%2 == 0){
            Double m = new Double(n);
            return computePI(n-1) - (1/((2*m)-1));
        }
        else{
            Double m = new Double(n);
            return computePI(n-1) + (1/((2*m)-1));
        } 
    }  
    // This method takes a character stack and converts all lower case letters
    // to upper case ones.
    public void upperStackRec(CharStack s)
    {
        if(!s.isEmpty()){
            char a = s.pop();
            upperStackRec(s);
            if(a >= 'a' && a <= 'z'){
                a = Character.toUpperCase(a);
            }
            s.push(a);
        }
    }

    // This method reads a string and returns the string in the reversed order.
    public String reverseStringRec(String s)
    {
        if(s.length() == 0) 
            return "";

        return reverseStringRec(s.substring(1,s.length())) + s.charAt(0);
    }

    // This method takes as parameters a reference to the head of a linked list, a
    // position specified by n, and a key. It returns the number of occurrences
    // of the key in the linked list beginning at the n-th node. If n = 0, it means
    // you should search in the entire linked list. If n = 1, then you should skip
    // the first node in the list.
    public int numOccurrencesRec(LNode node, int n, int key)
    {
          if(node.getLink() == null){
            if(node.getInfo() == key){
                return 1;
            } 
            else{
                return 0;
            }
        } 
        if(n > 0){
            n--;
            return numOccureencesRec(node->getLink(), n, key);
        }
        else{
            if(node.getInfo() == key){
                return numOccureencesRec(node->getLink(), n, key)+1;
            }
            return numOccureencesRec(node->getLink(), n, key);
        } 
    }
}
assignment8/RecursiveMethods.java
assignment8/RecursiveMethods.java
// The RecursiveMethods class that implements several recursive solutions
// Your name here
public class RecursiveMethods 

    // This method takes an int as the parameter and estimates the result of the
    // Madhava–Leibniz series. The result is then considered as an approximation
    // of pi/4. The method returns this value (pi/4).
    public double computePI(int n)
    {
          if(n == 1){
            return 1.0;
        }
        if(n%2 == 0){
            Double m = new Double(n);
            return computePI(n-1) - (1/((2*m)-1));
        }
        else{
            Double m = new Double(n);
            return computePI(n-1) + (1/((2*m)-1));
        } 
    }  
    // This method takes a character stack and converts all lower case letters
    // to upper case ones.
    public void upperStackRec(CharStack s)
    {
        if(!s.isEmpty()){
            char a = s.pop();
            upperStackRec(s);
            if(a >= 'a' && a <= 'z'){
                a = Character.toUpperCase(a);
            }
            s.push(a);
        }
    }

    // This method reads a string and returns the string in the reversed order.
    public String reverseStringRec(String s)
    {
        if(s.length() == 0) 
            return "";

        return reverseStringRec(s.substring(1,s.length())) + s.charAt(0);
    }

    // This method takes as parameters a reference to the head of a linked list, a
    // position specified by n, and a key. It returns the number of occurrences
    // of the key in the linked list beginning at the n-th node. If n = 0, it means
    // you should search in the entire linked list. If n = 1, then you should skip
    // the first node in the list.
    public int numOccurrencesRec(LNode node, int n, int key)
    {
          if(node.getLink() == null){
            if(node.getInfo() == key){
                return 1;
            } 
            else{
                return 0;
            }
        } 
        if(n > 0){
            n--;
            return numOccureencesRec(node->getLink(), n, key);
        }
        else{
            if(node.getInfo() == key){
                return numOccureencesRec(node->getLink(), n, key)+1;
            }
            return numOccureencesRec(node->getLink(), n, key);
        } 
    }
}
assignment8/Screenshot (221).png
assignment8/Screenshot...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here