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
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 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 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...