Browse Source

Added files

master
Raphael Roberts 6 years ago
commit
24a91d76d4
  1. 1
      .gitignore
  2. 67
      CharStack.java
  3. 33
      LNode.java
  4. 70
      LinkedNumberStack.java
  5. 15
      NumberStack.java
  6. 33
      StackApps.java
  7. 713
      TestStack.java

1
.gitignore

@ -0,0 +1 @@
*.dat

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 the size 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;
}
}

70
LinkedNumberStack.java

@ -0,0 +1,70 @@
// 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)
{
// TODO: implement this method
}
// remove and return the value at the top of the stack
public int pop()
{
// TODO: implement this method
return -1; // replace this statement with your own return
}
// return the number of elements on the stack
public int size()
{
// TODO: implement this method
return -1; // replace this statement with your own return
}
// 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;
}
}

15
NumberStack.java

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

33
StackApps.java

@ -0,0 +1,33 @@
// The StackApps class that implements two Stack applications
// Your name here
import java.util.Scanner;
public class StackApps
{
// convert a decimal number into a binary number and save it in a stack
// Do not create any arrays! Do not use any Java libraries to do the convertion.
// Doing so will result in points deduction.
public String decToBin(int numDec)
{
CharStack stackBinary = new CharStack(64); // stack used to store the result binary number
// TODO: complete this method
return stackBinary.toString(); // return a string representation of the stack
}
// compute the sum of the two big intergers and save the result in a stack
// Do not create any arrays! Do not use any Java libraries to do the calculation.
// Doing so will result in points deduction.
public String addBigIntegers(String num1, String num2)
{
CharStack stackNum1 = new CharStack(64); // stack used to store number 1
CharStack stackNum2 = new CharStack(64); // stack used to store number 2
CharStack stackResult = new CharStack(64); // stack used to store the result of the addition
// TODO: complete this method
return stackResult.toString(); // return a string representation of the stack
}
}

713
TestStack.java

@ -0,0 +1,713 @@
// Test driver for the LinkedNumberStack and StackApps classes
// Do not make any changes to this file!
// Xiwei Wang
import java.util.*;
import java.io.*;
public class TestStack
{
public static void main(String[] args)
{
System.out.println("================ Problem 1 ================");
TestP1();
System.out.println("================ End of Problem 1 ================\n\n");
System.out.print("Press any key to test Problem 2...");
try
{
System.in.read();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("================ Problem 2 ================");
TestP2();
System.out.println("================ End of Problem 2 ================");
}
public static void TestP1()
{
NumberStack myStack = new LinkedNumberStack();
int numPassedTests = 0;
int numTotalTests = 0;
String testResult;
// Test 1
numTotalTests++;
int iReturn = -1;
testResult = "[Failed]";
String eMsg = "N/A";
try
{
iReturn = myStack.size();
if (iReturn == 0)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 0" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 2
numTotalTests++;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
}
catch (RuntimeException e)
{
if (!(e instanceof NullPointerException))
{
numPassedTests++;
testResult = "[Passed]";
}
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() ==> " + testResult + "\n Expected: a RuntimeException");
System.out.println(" Yours: " + eMsg + "\n");
// Test 3
numTotalTests++;
boolean bReturn = false;
testResult = "[Failed]";
eMsg = "N/A";
try
{
myStack.push(10);
bReturn = myStack.isEmpty();
if (bReturn == false)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": push(10) and then isEmpty() ==> " + testResult + "\n Expected: false");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + bReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 4
numTotalTests++;
String sReturn = myStack.toString();
if (sReturn.equals("10 "))
{
numPassedTests++;
testResult = "[Passed]";
}
else
testResult = "[Failed]";
System.out.println("Test " + numTotalTests + ": toString() ==> " + testResult + "\n Expected (from top to bottom): 10 ");
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
// Test 5
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.top();
if (iReturn == 10)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": top() ==> " + testResult + "\n Expected: 10");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 6
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
myStack.push(20);
sReturn = myStack.toString();
if (sReturn.equals("20 10 "))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": push(20) and then toString() ==> " + testResult + "\n Expected (from top to bottom): 20 10 ");
if (eMsg.equals("N/A"))
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 7
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.top();
if (iReturn == 20)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": top() ==> " + testResult + "\n Expected: 20");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 8
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
if (iReturn == 20)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() ==> " + testResult + "\n Expected: 20");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 9
numTotalTests++;
sReturn = myStack.toString();
if (sReturn.equals("10 "))
{
numPassedTests++;
testResult = "[Passed]";
}
else
testResult = "[Failed]";
System.out.println("Test " + numTotalTests + ": toString() ==> " + testResult + "\n Expected (from top to bottom): 10 ");
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
// Test 10
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
myStack.push(30);
myStack.push(40);
myStack.push(50);
sReturn = myStack.toString();
if (sReturn.equals("50 40 30 10 "))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": push(30), push(40), push(50), and then toString() ==> " + testResult + "\n Expected (from top to bottom): 50 40 30 10 ");
if (eMsg.equals("N/A"))
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 11
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.size();
if (iReturn == 4)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 4" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 12
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
iReturn = myStack.top();
if (iReturn == 40)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() and then top() ==> " + testResult + "\n Expected: 40");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 13
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
iReturn = myStack.size();
if (iReturn == 2)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() and then size() ==> " + testResult + "\n Expected: 2");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 14
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
myStack.push(20);
sReturn = myStack.toString();
if (sReturn.equals("20 30 10 "))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": push(20) and then toString() ==> " + testResult + "\n Expected (from top to bottom): 20 30 10 ");
if (eMsg.equals("N/A"))
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 15
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.size();
if (iReturn == 3)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 3" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 16
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
iReturn = myStack.pop();
iReturn = myStack.top();
if (iReturn == 10)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() twice and then top() ==> " + testResult + "\n Expected: 10");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 17
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
sReturn = myStack.toString();
if (sReturn.equals("10 "))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": toString() ==> " + testResult + "\n Expected (from top to bottom): 10 ");
if (eMsg.equals("N/A"))
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 18
numTotalTests++;
bReturn = false;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
bReturn = myStack.isEmpty();
if (bReturn == true && iReturn == 10)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() and then isEmpty() ==> " + testResult + "\n Expected: 10, true");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + ", " + bReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 19
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.size();
if (iReturn == 0)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 0" );
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 20
numTotalTests++;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
}
catch (RuntimeException e)
{
if (!(e instanceof NullPointerException))
{
numPassedTests++;
testResult = "[Passed]";
}
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() ==> " + testResult + "\n Expected: a RuntimeException");
System.out.println(" Yours: " + eMsg + "\n");
// Test 21
numTotalTests++;
sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
myStack.push(70);
sReturn = myStack.toString();
if (sReturn.equals("70 "))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": push(70) and then toString() ==> " + testResult + "\n Expected (from top to bottom): 70 ");
if (eMsg.equals("N/A"))
System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 22
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.top();
if (iReturn == 70)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": top() ==> " + testResult + "\n Expected: 70");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 23
numTotalTests++;
iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
iReturn = myStack.pop();
iReturn = myStack.size();
if (iReturn == 0)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": pop() and then size() ==> " + testResult + "\n Expected: 0");
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
}
public static void TestP2()
{
try
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("testNumbers.dat"));
StackApps myApps = new StackApps();
ArrayList<Integer> results = new ArrayList<Integer>();
ArrayList<String> numbers = new ArrayList<String>();
results = (ArrayList<Integer>)in.readObject();
numbers = (ArrayList<String>)in.readObject();
String r;
String eMsg;
String currentLine;
int numPassedTests = 0;
int numTotalTests = 0;
for (int i = 0; i < 5; i++)
{
numTotalTests++;
currentLine = numbers.get(i);
r = "";
eMsg = "N/A";
try
{
r = myApps.decToBin(Integer.valueOf(currentLine));
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.print("Test " + numTotalTests + ": decToBin(" + currentLine + ") ==> ");
if (r.equals(results.get(i)))
{
System.out.println("[Passed]");
numPassedTests++;
}
else
System.out.println("[Failed]");
System.out.println(" Expected: " + results.get(i));
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + r + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
}
for (int i = 5; i < numbers.size(); i++)
{
numTotalTests++;
currentLine = numbers.get(i);
String[] operands = currentLine.split(" ");
r = "";
eMsg = "N/A";
try
{
r = myApps.addBigIntegers(operands[0], operands[1]);
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.print("Test " + numTotalTests + ": (" + operands[0] + " + " + operands[1] + ") ==> ");
if (r.equals(results.get(i)))
{
System.out.println("[Passed]");
numPassedTests++;
}
else
System.out.println("[Failed]");
System.out.println(" Expected: " + results.get(i));
if (eMsg.equals("N/A"))
System.out.println(" Yours: " + r + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
}
System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
}
catch (Exception e)
{
System.out.println("Error occurred: " + e.getMessage());
}
}
}
Loading…
Cancel
Save