Browse Source

Added files

master
Raphael Roberts 6 years ago
commit
3961bd29a9
  1. 67
      CharStack.java
  2. 33
      LNode.java
  3. 46
      RecursiveMethods.java
  4. 452
      TestRecursiveMethods.java

67
CharStack.java

@ -0,0 +1,67 @@
// 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;
}
}

33
LNode.java

@ -0,0 +1,33 @@
// 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;
}
}

46
RecursiveMethods.java

@ -0,0 +1,46 @@
// The RecursiveMethods class that implements several recursive solutions
// Your name here
public class RecursiveMethods
{
// This method calls the sumSquareRec method and returns the sum of the
// squares of the elements in the array.
public int sumSquares(int[] A)
{
// Do not make any changes to this method!
return sumSquaresRec(A, 0);
}
// This method takes an integer array as well as an integer (the starting
// index) and returns the sum of the squares of the elements in the array.
public int sumSquaresRec(int[] A, int pos)
{
// TODO: implement this method
return -1; // replace this statement with your own return
}
// This method takes a character stack and converts all lower case letters
// to upper case ones.
public void upperStackRec(CharStack s)
{
// TODO: implement this method
}
// This method reads a string and returns the string in the reversed order.
public String reverseStringRec(String s)
{
// TODO: implement this method
return "dummy string"; // replace this statement with your own return
}
// This method takes a reference to the head of a linked list.
// It returns the reference to the head of the linked list in the reversed order.
public LNode reverseListRec(LNode head)
{
// TODO: implement this method
return new LNode(-1); // replace this statement with your own return
}
}

452
TestRecursiveMethods.java

@ -0,0 +1,452 @@
// Test driver for the RecursiveMethods class
// Do not make any changes to this file!
// Xiwei Wang
import java.util.Arrays;
public class TestRecursiveMethods
{
public static void main(String[] args)
{
RecursiveMethods myMethods = new RecursiveMethods();
int numPassedTests = 0;
int numTotalTests = 0;
String testResult;
// Test 1
numTotalTests++;
int iReturn = -1;
testResult = "[Failed]";
String eMsg = "N/A";
try
{
int[] myArray = {10};
iReturn = myMethods.sumSquares(myArray);
if (iReturn == 100)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": sumSquares(10) ==> " + testResult + "\n Expected: 100" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 2
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
int[] myArray = {10, 20, 30, 40, 50, 60};
iReturn = myMethods.sumSquares(myArray);
if (iReturn == 9100)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": sumSquares(10, 20, 30, 40, 50, 60) ==> " + testResult + "\n Expected: 9100" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 3
numTotalTests++;
CharStack s = new CharStack(5);
String sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
s.push('a');
myMethods.upperStackRec(s);
sReturn = s.toString();
if (sReturn.equals("A"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": upperStackRec(\"a\" (from top to bottom)) ==> " + testResult + "\n Expected: \"A\" (from top to bottom)" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 4
numTotalTests++;
s = new CharStack(5);
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
s.push('7');
s.push('?');
s.push('p');
myMethods.upperStackRec(s);
sReturn = s.toString();
if (sReturn.equals("P?7"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": upperStackRec(\"p?7\" (from top to bottom)) ==> " + testResult + "\n Expected: \"P?7\" (from top to bottom)" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 5
numTotalTests++;
s = new CharStack(5);
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
s.push('4');
s.push('K');
s.push('3');
s.push('s');
s.push('c');
myMethods.upperStackRec(s);
sReturn = s.toString();
if (sReturn.equals("CS3K4"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": upperStackRec(\"cs3K4\" (from top to bottom)) ==> " + testResult + "\n Expected: \"CS3K4\" (from top to bottom)" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 6
numTotalTests++;
s = new CharStack(9);
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
s.push('Z');
s.push('y');
s.push('X');
s.push('|');
s.push('E');
s.push('d');
s.push('C');
s.push('b');
s.push('A');
myMethods.upperStackRec(s);
sReturn = s.toString();
if (sReturn.equals("ABCDE|XYZ"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": upperStackRec(\"AbCdE|XyZ\" (from top to bottom)) ==> " + testResult + "\n Expected: \"ABCDE|XYZ\" (from top to bottom)" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 7
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
String myString = "";
sReturn = myMethods.reverseStringRec(myString);
if (sReturn.equals(""))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseStringRec(\"\") ==> " + testResult + "\n Expected: \"\"" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\"\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 8
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
String myString = "a";
sReturn = myMethods.reverseStringRec(myString);
if (sReturn.equals("a"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseStringRec(\"a\") ==> " + testResult + "\n Expected: \"a\"" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\"\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 9
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
String myString = "abc";
sReturn = myMethods.reverseStringRec(myString);
if (sReturn.equals("cba"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseStringRec(\"abc\") ==> " + testResult + "\n Expected: \"cba\"" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\"\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 10
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
String myString = "Hello, Data Structures!";
sReturn = myMethods.reverseStringRec(myString);
if (sReturn.equals("!serutcurtS ataD ,olleH"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseStringRec(\"Hello, Data Structures!\") ==> " + testResult + "\n Expected: \"!serutcurtS ataD ,olleH\"" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: \"" + sReturn + "\"\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 11
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
LNode myNode = null;
sReturn = traverseList(myMethods.reverseListRec(myNode));
if (sReturn.equals("head->null"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseListRec(null) ==> " + testResult + "\n Expected: head->null" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 12
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
LNode myNode = new LNode(20);
sReturn = traverseList(myMethods.reverseListRec(myNode));
if (sReturn.equals("head->20->null"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseListRec(20) ==> " + testResult + "\n Expected: head->20->null" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 13
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
LNode myNode = new LNode(20);
LNode myNode1 = new LNode(30);
LNode myNode2 = new LNode(40);
myNode.setLink(myNode1);
myNode1.setLink(myNode2);
sReturn = traverseList(myMethods.reverseListRec(myNode));
if (sReturn.equals("head->40->30->20->null"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseListRec(20->30->40) ==> " + testResult + "\n Expected: head->40->30->20->null" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 14
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
LNode myNode = new LNode(90);
LNode myNode1 = new LNode(80);
LNode myNode2 = new LNode(70);
LNode myNode3 = new LNode(60);
LNode myNode4 = new LNode(50);
LNode myNode5 = new LNode(40);
myNode.setLink(myNode1);
myNode1.setLink(myNode2);
myNode2.setLink(myNode3);
myNode3.setLink(myNode4);
myNode4.setLink(myNode5);
sReturn = traverseList(myMethods.reverseListRec(myNode));
if (sReturn.equals("head->40->50->60->70->80->90->null"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": reverseListRec(90->80->70->60->50->40) ==> " + testResult + "\n Expected: head->40->50->60->70->80->90->null" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
}
public static String traverseList(LNode head)
{
String listContent = "head->";
LNode current = head;
while (current != null)
{
listContent += current.getInfo() + "->";
current = current.getLink();
}
listContent += "null";
return listContent;
}
}
Loading…
Cancel
Save