summaryrefslogtreecommitdiff
path: root/projects/project2_LInfiniteInteger
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
commit89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch)
treecdc0fd8165e65b1637fa54cac11c932acefc8a89 /projects/project2_LInfiniteInteger
downloadcoe0445-master.tar.gz
coe0445-master.tar.bz2
coe0445-master.zip
Inital commitHEADmaster
Diffstat (limited to 'projects/project2_LInfiniteInteger')
-rw-r--r--projects/project2_LInfiniteInteger/InfiniteInteger y was constructed using the following statement76
-rw-r--r--projects/project2_LInfiniteInteger/InfiniteInteger.pdfbin0 -> 123056 bytes
-rw-r--r--projects/project2_LInfiniteInteger/InfiniteIntegerCalculator.java14
-rw-r--r--projects/project2_LInfiniteInteger/InfiniteIntegerCalculatorFrame.java495
-rw-r--r--projects/project2_LInfiniteInteger/InfiniteIntegerInterface.java59
-rw-r--r--projects/project2_LInfiniteInteger/LInfiniteInteger.java562
-rw-r--r--projects/project2_LInfiniteInteger/LInfiniteIntegerTester.java1136
-rw-r--r--projects/project2_LInfiniteInteger/LStack.java66
-rw-r--r--projects/project2_LInfiniteInteger/StackInterface.java9
9 files changed, 2417 insertions, 0 deletions
diff --git a/projects/project2_LInfiniteInteger/InfiniteInteger y was constructed using the following statement b/projects/project2_LInfiniteInteger/InfiniteInteger y was constructed using the following statement
new file mode 100644
index 0000000..658ce14
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/InfiniteInteger y was constructed using the following statement
@@ -0,0 +1,76 @@
+ LInfiniteInteger thattemp = new LInfiniteInteger(anInfiniteInteger.toString());
+ thattemp.isNegative = false;
+ thistemp.isNegative = false;
+ if(thattemp.compareTo(thistemp) > 0)
+ {
+ negateAtEnd = true;
+ }
+ System.out.println("[ME] Attempting to detect if final answer should be negitive: " + negateAtEnd + " : " + thattemp.compareTo(thistemp) + " : When compareing " + thattemp.toString() + " with " + thistemp.toString());
+ //find the 9's complement of the this number
+ LInfiniteInteger ninesc = new LInfiniteInteger("");
+ Node thisnode = thistemp.lastNode;
+ Node thatnode = thattemp.lastNode;
+ while(thisnode != null || thatnode != null)
+ {
+ if(thatnode != null)
+ {
+ ninesc.addfirst(9-thatnode.data);
+ thatnode = thatnode.previous;
+ }
+ else
+ {
+ ninesc.addfirst(9);
+ }
+ if(thisnode != null)
+ {
+ thisnode = thisnode.previous;
+ }
+
+ }
+ ninesc.removeLast();
+ System.out.println("[ME] Calculated 9's complement: " + ninesc);
+ LInfiniteInteger answer =(LInfiniteInteger) ninesc.plus(thistemp);
+ answer = (LInfiniteInteger) answer.plus(new LInfiniteInteger(1));
+ //Remove carry
+ System.out.println("[ME] Answer's number of digits: " + answer.getNumberOfDigits() + " Ninesc's number of digits: " + ninesc.getNumberOfDigits());
+ if(answer.getNumberOfDigits() > ninesc.getNumberOfDigits())
+ {
+ answer.removeFirst();
+ }
+ System.out.println("[ME] 9's complement + other + 1 number = " + answer.toString());
+
+ ninesc = new LInfiniteInteger("");
+ for(Node tmp = answer.lastNode; tmp != null; tmp = tmp.previous)
+ {
+ ninesc.addfirst(9-tmp.data);
+ }
+ ninesc.removeLast();
+ System.out.println("[ME] After reverseing again, number = " + ninesc.toString());
+ answer = (LInfiniteInteger) ninesc.plus(new LInfiniteInteger(1));
+ //Remove leading 0's if nessessary
+ System.out.println("[ME] Answer's number of digits: " + answer.getNumberOfDigits() + "(" + answer.toString() +") Ninesc's number of digits: " + ninesc.getNumberOfDigits() + "(" + ninesc.toString() + ")");
+ if(answer.getNumberOfDigits() > ninesc.getNumberOfDigits())
+ {
+ answer.removeFirst();
+ }
+ for(Node tmp = answer.firstNode; tmp != null; tmp = tmp.next)
+ {
+ if(tmp.data == 0)
+ {
+ System.out.println("[ME] Removeing a leading 0");
+ answer.removeFirst();
+ }
+ else
+ {
+ break;
+ }
+ }
+ System.out.println("[ME] After removeing padded 0's number is " + answer.toString());
+ if(answer.firstNode == null)
+ {
+ answer.addfirst(0);
+ }
+ //If the final answer has no numbers, add a 0
+ answer.isNegative = negateAtEnd;
+ System.out.println("[ME] Reversed back, final answer is " + answer.toString());
+ return answer; \ No newline at end of file
diff --git a/projects/project2_LInfiniteInteger/InfiniteInteger.pdf b/projects/project2_LInfiniteInteger/InfiniteInteger.pdf
new file mode 100644
index 0000000..12ce885
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/InfiniteInteger.pdf
Binary files differ
diff --git a/projects/project2_LInfiniteInteger/InfiniteIntegerCalculator.java b/projects/project2_LInfiniteInteger/InfiniteIntegerCalculator.java
new file mode 100644
index 0000000..540706d
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/InfiniteIntegerCalculator.java
@@ -0,0 +1,14 @@
+import javax.swing.JFrame;
+
+
+public class InfiniteIntegerCalculator
+{
+ public static void main(String[] args)
+ {
+ JFrame frame = new InfiniteIntegerCalculatorFrame();
+ frame.setTitle("Infinite Integer Calculator");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(600,300);
+ frame.setVisible(true);
+ }
+}
diff --git a/projects/project2_LInfiniteInteger/InfiniteIntegerCalculatorFrame.java b/projects/project2_LInfiniteInteger/InfiniteIntegerCalculatorFrame.java
new file mode 100644
index 0000000..6e88ff8
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/InfiniteIntegerCalculatorFrame.java
@@ -0,0 +1,495 @@
+import java.awt.BorderLayout;
+import java.awt.Font;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyListener;
+import java.util.ArrayList;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+
+@SuppressWarnings("serial")
+public class InfiniteIntegerCalculatorFrame extends JFrame
+{
+ private JButton[] numberButtons; // number buttons (0 to 9)
+ private JButton plusButton; // plus button (+)
+ private JButton minusButton; // minus button (-)
+ private JButton multiplyButton; // multiply button (*)
+ private JButton backButton; // back button (<-)
+ private JButton equalButton; // equal button (=)
+ private JButton clearButton; //clear button (C)
+ private JTextArea textArea; // Text area for expression and result
+ private JPanel buttonPanel; // panel for all buttons
+ private ArrayList<String> ppString; // Pretty printing string
+ private KeyListener kListener;
+
+ public InfiniteIntegerCalculatorFrame()
+ {
+ textArea = new JTextArea();
+ textArea.setText("0");
+ textArea.setEditable(false);
+ textArea.setLineWrap(true);
+
+ kListener = new KeyEvent();
+ textArea.addKeyListener(kListener);
+
+ Font font = new Font(Font.SERIF, Font.BOLD, 20);
+ textArea.setFont(font);
+
+ setButtonsAndPanel();
+
+ add(buttonPanel, BorderLayout.EAST);
+ add(textArea, BorderLayout.CENTER);
+
+ ppString = new ArrayList<String>();
+ ppString.add("0");
+ }
+
+ public class KeyEvent implements KeyListener
+ {
+ public void keyPressed(java.awt.event.KeyEvent arg0)
+ {
+ int keyCode = arg0.getKeyCode();
+ char keyChar = arg0.getKeyChar();
+
+ if(keyCode >= 48 && keyCode <=57 && keyChar != '*')
+ {
+ ButtonListener bl = new ButtonListener(keyCode - 48);
+ bl.actionPerformed(new ActionEvent(numberButtons[keyCode - 48], 1001, "" + (keyCode - 48)));
+ }
+ else if(keyCode >= 96 && keyCode <= 105)
+ {
+ ButtonListener bl = new ButtonListener(keyCode - 96);
+ bl.actionPerformed(new ActionEvent(numberButtons[keyCode - 96], 1001, "" + (keyCode - 96)));
+ }
+ else if(keyCode == 61 && keyChar == '+')
+ {
+ ButtonListener bl = new ButtonListener(10);
+ bl.actionPerformed(new ActionEvent(equalButton, 1001, "+"));
+ }
+ else if(keyCode == 107)
+ {
+ ButtonListener bl = new ButtonListener(10);
+ bl.actionPerformed(new ActionEvent(equalButton, 1001, "+"));
+ }
+ else if(keyCode == 45 && keyChar == '-')
+ {
+ ButtonListener bl = new ButtonListener(11);
+ bl.actionPerformed(new ActionEvent(minusButton, 1001, "-"));
+ }
+ else if(keyCode == 109)
+ {
+ ButtonListener bl = new ButtonListener(11);
+ bl.actionPerformed(new ActionEvent(minusButton, 1001, "-"));
+ }
+ else if(keyCode == 56 && keyChar == '*')
+ {
+ ButtonListener bl = new ButtonListener(12);
+ bl.actionPerformed(new ActionEvent(multiplyButton, 1001, "*"));
+ }
+ else if(keyCode == 106)
+ {
+ ButtonListener bl = new ButtonListener(12);
+ bl.actionPerformed(new ActionEvent(multiplyButton, 1001, "*"));
+ }
+ else if(keyCode == 8)
+ {
+ ButtonListener bl = new ButtonListener(13);
+ bl.actionPerformed(new ActionEvent(backButton, 1001, "<-"));
+ }
+ else if(keyCode == 61 && keyChar == '=')
+ {
+ ButtonListener bl = new ButtonListener(14);
+ bl.actionPerformed(new ActionEvent(equalButton, 1001, "="));
+ }
+ else if(keyCode == 10)
+ {
+ ButtonListener bl = new ButtonListener(14);
+ bl.actionPerformed(new ActionEvent(equalButton, 1001, "="));
+ }
+ else if(keyCode == 67)
+ {
+ ButtonListener bl = new ButtonListener(15);
+ bl.actionPerformed(new ActionEvent(clearButton, 1001, "C"));
+ }
+ }
+
+ public void keyReleased(java.awt.event.KeyEvent arg0)
+ {
+ }
+
+ public void keyTyped(java.awt.event.KeyEvent arg0)
+ {
+ }
+ }
+
+ /**
+ * setButtonsAndPanel
+ *
+ * Initialize all buttons, initialize the button panel,
+ * and add all buttons into button panel.
+ */
+ private void setButtonsAndPanel()
+ {
+ numberButtons = new JButton[10];
+ ButtonListener[] nbls = new ButtonListener[16];
+ for(int i = 0; i <= 9; i++)
+ {
+ numberButtons[i] = new JButton("" + i);
+ nbls[i] = new ButtonListener(i);
+ numberButtons[i].addActionListener(nbls[i]);
+ }
+
+ plusButton = new JButton("+");
+ nbls[10] = new ButtonListener(10);
+ plusButton.addActionListener(nbls[10]);
+
+ minusButton = new JButton("-");
+ nbls[11] = new ButtonListener(11);
+ minusButton.addActionListener(nbls[11]);
+
+ multiplyButton = new JButton("*");
+ nbls[12] = new ButtonListener(12);
+ multiplyButton.addActionListener(nbls[12]);
+
+ backButton = new JButton("<-");
+ nbls[13] = new ButtonListener(13);
+ backButton.addActionListener(nbls[13]);
+
+ equalButton = new JButton("=");
+ nbls[14] = new ButtonListener(14);
+ equalButton.addActionListener(nbls[14]);
+
+ clearButton = new JButton("C");
+ nbls[15] = new ButtonListener(15);
+ clearButton.addActionListener(nbls[15]);
+
+ buttonPanel = new JPanel();
+ buttonPanel.setLayout(new GridLayout(4,4));
+
+ buttonPanel.add(numberButtons[7]);
+ buttonPanel.add(numberButtons[8]);
+ buttonPanel.add(numberButtons[9]);
+ buttonPanel.add(plusButton);
+ buttonPanel.add(numberButtons[4]);
+ buttonPanel.add(numberButtons[5]);
+ buttonPanel.add(numberButtons[6]);
+ buttonPanel.add(minusButton);
+ buttonPanel.add(numberButtons[1]);
+ buttonPanel.add(numberButtons[2]);
+ buttonPanel.add(numberButtons[3]);
+ buttonPanel.add(multiplyButton);
+ buttonPanel.add(numberButtons[0]);
+ buttonPanel.add(equalButton);
+ buttonPanel.add(clearButton);
+ buttonPanel.add(backButton);
+ }
+
+ private class ButtonListener implements ActionListener
+ {
+ private int number;
+
+ private ButtonListener(int aNumber)
+ {
+ /* Number associated with each button as follows:
+ * - 0 to 9 for number buttons
+ * - 10 for +
+ * - 11 for -
+ * - 12 for *
+ * - 13 for <-
+ * - 14 for =
+ * - 15 for C
+ */
+ number = aNumber;
+ }
+
+ public void actionPerformed(ActionEvent arg0)
+ {
+ int ppSize = ppString.size();
+ boolean error = false;
+
+ if(number < 10)
+ {
+ if(ppSize == 1)
+ {
+ String temp = ppString.get(0);
+
+ if(temp.equals("+") || temp.equals("*"))
+ {
+ ppString.add("" + number);
+ }
+ else if(temp.equals("0"))
+ {
+ ppString.set(0, "" + number);
+ }
+ else
+ {
+ ppString.set(0,temp + number);
+ }
+ }
+ else
+ {
+ String temp = ppString.get(ppSize - 1);
+
+ if(temp.equals("+") || temp.equals("*"))
+ {
+ ppString.add("" + number);
+ }
+ else if(temp.equals("-"))
+ {
+ if(ppString.get(ppSize - 2).equals("+") || ppString.get(ppSize - 2).equals("*"))
+ {
+ ppString.set(ppSize - 1, temp + number);
+ }
+ else
+ {
+ ppString.add("" + number);
+ }
+ }
+ else
+ {
+ ppString.set(ppSize - 1, temp + number);
+ }
+ }
+ }
+ else if(number == 10)
+ {
+ ppString.add("+");
+ }
+ else if(number == 11)
+ {
+ ppString.add("-");
+ }
+ else if(number == 12)
+ {
+ ppString.add("*");
+ }
+ else if(number == 13)
+ {
+ if(ppSize > 0)
+ {
+ String temp = ppString.get(ppSize - 1);
+
+ if(temp.length() > 1)
+ {
+ ppString.set(ppSize - 1, temp.substring(0, temp.length() - 1));
+ }
+ else
+ {
+ ppString.remove(ppSize - 1);
+ }
+ }
+ }
+ else if(number == 14)
+ {
+ InfiniteIntegerInterface result = calculateResult();
+
+ if(result == null)
+ {
+ error = true;
+ }
+ else
+ {
+ ppString.clear();
+ ppString.add(result.toString());
+ }
+ }
+ else if(number == 15)
+ {
+ ppString.clear();
+ ppString.add("0");
+ }
+
+ // Set textArea according to ppString
+
+ ppSize = ppString.size();
+ String displayString = "";
+
+ if(ppSize > 0)
+ {
+ for(int i = 0; i < ppSize; i++)
+ {
+ displayString = displayString + ppString.get(i) + " ";
+ }
+ }
+ else
+ {
+ displayString = "0";
+ }
+
+ if(error)
+ {
+ displayString = displayString + "\nInvalid Expression";
+ }
+
+ textArea.setText(displayString);
+ }
+ }
+
+ /**
+ * calculateResult()
+ *
+ * This method constructs a postfix expression base on the infix expression
+ * stored in the array list of string ppString using stack as explained in
+ * class. Then evaluate the postfix expression using stack.
+ *
+ * @return an infinite integer as a result if the expression is valid.
+ * Otherwise, return null to indicate invalid expression.
+ */
+ private InfiniteIntegerInterface calculateResult()
+ {
+ ArrayList<String> postFix = new ArrayList<String>(); // Postfix expression
+ StackInterface<String> operators = new LStack<String>(); // Stack to store operators
+
+ // Convert infix expression into postfix expression
+
+ int expSize = ppString.size();
+
+ for(int i = 0; i < expSize; i++)
+ {
+ String temp = ppString.get(i);
+
+ if(temp.length() > 1)
+ {
+ postFix.add(temp);
+ }
+ else if(isOperator(temp.charAt(0)))
+ {
+ if(temp.equals("+") || temp.equals("-"))
+ {
+ while(!operators.isEmpty())
+ {
+ postFix.add(operators.pop());
+ }
+ operators.push(temp);
+ }
+ else
+ {
+ while(!operators.isEmpty() && operators.peek().equals("*"))
+ {
+ postFix.add(operators.pop());
+ }
+ operators.push(temp);
+ }
+ }
+ else
+ {
+ postFix.add(temp);
+ }
+ }
+
+ while(!operators.isEmpty())
+ {
+ postFix.add(operators.pop());
+ }
+
+ // Display postfix expression
+
+ System.out.print("Postfix: ");
+
+ for(int i = 0; i < postFix.size(); i++)
+ {
+ System.out.print(postFix.get(i) + " ");
+ }
+ System.out.println();
+
+ // Evaluate postfix expression
+
+ StackInterface<InfiniteIntegerInterface> resultStack = new LStack<InfiniteIntegerInterface>();
+
+ int postFixSize = postFix.size();
+
+ for(int i = 0; i < postFixSize; i++)
+ {
+ String temp = postFix.get(i);
+
+ if(temp.length() == 1 && isOperator(temp.charAt(0)))
+ {
+ /* Encounter an operator, pop the top of the stack
+ * twice to use those numbers as operand, calculate
+ * the result, and push the result back onto the
+ * stack.
+ */
+ InfiniteIntegerInterface op1, op2; // operand 1 and 2
+
+ if(resultStack.isEmpty())
+ {
+ return null; // if the stack is empty, invalid expression
+ }
+ else
+ {
+ op2 = resultStack.pop(); // Pop the second operand
+ }
+
+ if(resultStack.isEmpty())
+ {
+ return null; // if the stack is empty, invalid expression
+ }
+ else
+ {
+ op1 = resultStack.pop(); // Pop the first operand
+ }
+
+ /* Calculating result of "op1 operator op2"
+ * and push the result back onto the stack.
+ */
+ if(temp.equals("+"))
+ {
+ resultStack.push(op1.plus(op2));
+ }
+ else if(temp.equals("-"))
+ {
+ resultStack.push(op1.minus(op2));
+ }
+ else
+ {
+ resultStack.push(op1.multiply(op2));
+ }
+ }
+ else
+ {
+ // Encounter a number, push the number onto the stack.
+
+ resultStack.push(new LInfiniteInteger(temp));
+ }
+ }
+
+ /* The expression is valid if the stack contain exactly
+ * one item. That one single item is the result of evaluating
+ * the arithmetic expression.
+ */
+
+ InfiniteIntegerInterface result = null;
+
+ if(resultStack.isEmpty())
+ {
+ return null; // The stack is empty, invalid expression
+ }
+ else
+ {
+ result = resultStack.pop(); // Pop the result out of the stack
+
+ if(!resultStack.isEmpty())
+ {
+ result = null; // The stack is NOT empty, invalid expression
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * isOperator: Return true if the character c is an
+ * operator, +, -, or *. Otherwise, return false.
+ * @param c the character to be tested whether it is
+ * an operator.
+ * @return true if c is an operator. Otherwise, return false.
+ */
+ private boolean isOperator(char c)
+ {
+ return c == '+' || c == '-' || c == '*';
+ }
+}
diff --git a/projects/project2_LInfiniteInteger/InfiniteIntegerInterface.java b/projects/project2_LInfiniteInteger/InfiniteIntegerInterface.java
new file mode 100644
index 0000000..5cd9944
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/InfiniteIntegerInterface.java
@@ -0,0 +1,59 @@
+
+public interface InfiniteIntegerInterface
+{
+ /**
+ * Gets the number of digits of this infinite integer.
+ * @return an integer representing the number of digits
+ * of this infinite integer.
+ */
+ public int getNumberOfDigits();
+
+ /**
+ * Checks whether this infinite integer is a negative number.
+ * @return true if this infinite integer is a negative number.
+ * Otherwise, return false.
+ */
+ public boolean isNegative();
+
+ /**
+ * Calculates the result of this infinite integer plus anInfiniteInteger
+ * @param anInfiniteInteger the infinite integer to be added.
+ * @return a NEW infinite integer representing the result of this
+ * infinite integer plus anInfiniteInteger
+ */
+ public InfiniteIntegerInterface plus(final InfiniteIntegerInterface anInfiniteInteger);
+
+ /**
+ * Calculates the result of this infinite integer subtracted by anInfiniteInteger
+ * @param anInfiniteInteger the infinite integer to subtract.
+ * @return a NEW infinite integer representing the result of this
+ * infinite integer subtracted by anInfiniteInteger
+ */
+ public InfiniteIntegerInterface minus(final InfiniteIntegerInterface anInfiniteInteger);
+
+ /**
+ * Calculates the result of this infinite integer multiplied by anInfiniteInteger
+ * @param anInfiniteInteger the multiplier.
+ * @return a NEW infinite integer representing the result of this
+ * infinite integer multiplied by anInfiniteInteger.
+ */
+ public InfiniteIntegerInterface multiply(final InfiniteIntegerInterface anInfiniteInteger);
+
+ /**
+ * Generates a string representing this infinite integer. If this infinite integer
+ * is a negative number a minus symbol should be in the front of numbers. For example,
+ * "-12345678901234567890". But if the infinite integer is a positive number, no symbol
+ * should be in the front of the numbers (e.g., "12345678901234567890").
+ * @return a string representing this infinite integer number.
+ */
+ public String toString();
+
+ /**
+ * Compares this infinite integer with anInfiniteInteger
+ * @return either -1, 0, or 1 as follows:
+ * If this infinite integer is less than anInfiniteInteger, return -1.
+ * If this infinite integer is equal to anInfiniteInteger, return 0.
+ * If this infinite integer is greater than anInfiniteInteger, return 1.
+ */
+ public int compareTo(final InfiniteIntegerInterface anInfiniteInteger);
+}
diff --git a/projects/project2_LInfiniteInteger/LInfiniteInteger.java b/projects/project2_LInfiniteInteger/LInfiniteInteger.java
new file mode 100644
index 0000000..c74ab41
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/LInfiniteInteger.java
@@ -0,0 +1,562 @@
+
+public class LInfiniteInteger implements InfiniteIntegerInterface
+{
+ private Node firstNode;
+ private Node lastNode;
+ private int numberOfDigits;
+ private boolean isNegative;
+
+ /**
+ * Constructor: Constructs this infinite integer from a string
+ * representing an integer.
+ * @param s a string represents an integer
+ */
+ public LInfiniteInteger(String s)
+ {
+ if(s.toCharArray().length > 0 && s.toCharArray()[0] == '-') //Short circuit in case the string is empty
+ {
+ isNegative = true;
+ s = s.substring(1);
+ }
+ boolean significantDigit = false;
+ for(char c : s.toCharArray())
+ {
+ if(!significantDigit && Character.getNumericValue(c) == 0)
+ {
+ }
+ else
+ {
+ significantDigit = true;
+ Node n = new Node(lastNode,Character.getNumericValue(c),null);
+ if(firstNode == null)
+ {
+ firstNode = n;
+ }
+ lastNode = n;
+ numberOfDigits++;
+ }
+ }
+ if(firstNode == null)
+ {
+ firstNode = new Node(null,0,null);
+ lastNode = firstNode;
+ numberOfDigits++;
+ }
+ for(Node tmp = lastNode; tmp != null && tmp.previous != null;tmp = tmp.previous)
+ {
+ //System.out.println("[ME] Setting node with data " + tmp.previous.data + " to have corret next.");
+ tmp.previous.next = tmp;
+ }
+ }
+
+ /**
+ * Constructor: Constructs this infinite integer from an integer.
+ * @param anInteger an integer
+ */
+ public LInfiniteInteger(int anInteger)
+ {
+ isNegative = anInteger < 0;
+ //System.out.println("[ME] Makeing with integer" + anInteger);
+ if(isNegative)
+ {
+ anInteger *= -1;
+ }
+ for(int i = 10; i/10 <= anInteger; i *= 10)
+ {
+ //System.out.println("[ME] Makeing a node with " + (anInteger%i)/(i/10));
+ Node n = new Node(null,(anInteger%i)/(i/10),firstNode);
+ firstNode = n;
+ numberOfDigits++;
+ }
+ Node tmp = firstNode;
+ for(; tmp != null && tmp.next != null;tmp = tmp.next)
+ {
+ //System.out.println("[ME] Setting node with data " + tmp.previous.data + " to have corret next.");
+ tmp.next.previous = tmp;
+ }
+ lastNode = tmp;
+ if(anInteger == 0)
+ {
+ Node n = new Node(null,0,null);
+ firstNode = n;
+ lastNode = n;
+ }
+ //numberOfDigits--; //Subtract 1, because we looped 1 too many times for digits, but needed to take into account potential digits to the left
+ //System.out.println("[ME]Created from integer, number is " + this.toString());
+ }
+
+ /**
+ * Gets the number of digits of this infinite integer.
+ * @return an integer representing the number of digits
+ * of this infinite integer.
+ */
+ public int getNumberOfDigits()
+ {
+ numberOfDigits = 0;
+ for(Node tmp = firstNode; tmp != null; tmp = tmp.next)
+ {
+ numberOfDigits++;
+ }
+ return numberOfDigits;
+ }
+
+ /**
+ * Checks whether this infinite integer is a negative number.
+ * @return true if this infinite integer is a negative number.
+ * Otherwise, return false.
+ */
+ public boolean isNegative()
+ {
+ return isNegative;
+ }
+
+ /**
+ * Calculates the result of this infinite integer plus anInfiniteInteger
+ * @param anInfiniteInteger the infinite integer to be added to this
+ * infinite integer.
+ * @return a NEW infinite integer representing the result of this
+ * infinite integer plus anInfiniteInteger
+ */
+ public InfiniteIntegerInterface plus(final InfiniteIntegerInterface anInfiniteInteger)
+ {
+ LInfiniteInteger output = new LInfiniteInteger("");
+
+ //System.out.println("[ME] Attempting to add " + this.toString() + " and " + anInfiniteInteger.toString());
+
+ //If both are negative do addition, but the answer is negative
+ if(this.isNegative() && anInfiniteInteger.compareTo(new LInfiniteInteger("")) <= 0)
+ {
+
+ //And the other number is zero, set output to negative, and add
+ output.isNegative = true;
+ }
+ else if(this.compareTo(new LInfiniteInteger("")) <= 0 && anInfiniteInteger.isNegative())
+ {
+ output.isNegative = true;
+ }
+ //If one is negitive, find out which, and do a subtraction instead
+ if(this.compareTo(new LInfiniteInteger("")) < 0 && anInfiniteInteger.compareTo(new LInfiniteInteger("")) > 0)
+ {
+ //this is negitive, but the other is positive
+ LInfiniteInteger temp = new LInfiniteInteger(this.toString());
+ temp.isNegative = false;
+ return anInfiniteInteger.minus(temp);
+ }
+ if(this.compareTo(new LInfiniteInteger("")) > 0 && anInfiniteInteger.compareTo(new LInfiniteInteger("")) < 0)
+ {
+ //anInfiniteInteger is negitive, but we are positive.
+ LInfiniteInteger temp = new LInfiniteInteger(anInfiniteInteger.toString());
+ temp.isNegative = false;
+ return this.minus(temp);
+ }
+ //We should only get to this point if we're really doing addition
+ int carry = 0;
+ Node thattemp = new LInfiniteInteger(anInfiniteInteger.toString()).lastNode;
+ Node temp = this.lastNode;
+ if(temp == null)
+ {
+ //System.out.println("[ME] LAST NODE IS NULL FROM START!");
+ }
+ for(; temp != null || thattemp != null;)
+ {
+ int sum = 0;
+ //System.out.print("[ME] After this interation, ");
+ if(temp != null)
+ {
+ sum += temp.data;
+ //System.out.print(" " + temp.data + " + ");
+ temp = temp.previous;
+ }
+ else
+ {
+ //System.out.print(" NULL + ");
+ }
+ if(thattemp != null)
+ {
+ //System.out.print(thattemp.data);
+ sum += thattemp.data;
+ thattemp = thattemp.previous;
+ }
+ else
+ {
+ //System.out.print(" NULL ");
+ }
+ sum += carry;
+ carry = sum / 10;
+ output.addfirst(sum % 10);
+ //System.out.print(" , sum is " + sum%10 + " and carry is " + carry + "\n");
+ }
+ if(carry > 0)
+ {
+ output.addfirst(carry);
+ }
+ output.removeLast();
+ //Return the result
+ return output;
+ }
+ private void removeLast()
+ {
+ Node tmp = lastNode;
+ if(tmp != null)
+ {
+ tmp = lastNode.previous;
+ if(tmp != null)
+ {
+ tmp.next = null;
+ }
+ lastNode = tmp;
+ }
+ }
+ private void removeFirst()
+ {
+ Node tmp = firstNode;
+ if(tmp != null)
+ {
+ tmp = firstNode.next;
+ if(tmp != null)
+ {
+ tmp.previous = null;
+ }
+ firstNode = tmp;
+ }
+ }
+ private void addfirst(int data)
+ {
+ Node tmp = firstNode;
+ Node n = new Node(null,data,firstNode);
+ firstNode = n;
+ if(tmp != null)
+ {
+ tmp.previous = n;
+ }
+ }
+ private void addlast(int data)
+ {
+ Node tmp = lastNode;
+ Node n = new Node(lastNode,data,null);
+ lastNode = n;
+ if(tmp != null)
+ {
+ tmp.next = n;
+ }
+ }
+ private Node getLastNode()
+ {
+ return lastNode;
+ }
+ private Node getFirstNode()
+ {
+ return firstNode;
+ }
+ /**
+ * Calculates the result of this infinite integer subtracted by anInfiniteInteger
+ * @param anInfiniteInteger the infinite integer to subtract.
+ * @return a NEW infinite integer representing the result of this
+ * infinite integer subtracted by anInfiniteInteger
+ */
+ public InfiniteIntegerInterface minus(final InfiniteIntegerInterface anInfiniteInteger)
+ {
+ //System.out.println("[ME] Attempting to subtract " + anInfiniteInteger.toString() + " from " + this.toString());
+ // Figure out if we're really supposed to subtract, or we need to do a clever add
+ //Cases:
+ //We are negative, and anInteger is not negative (-this - that) = (-this + -that)
+ if(this.isNegative && !anInfiniteInteger.isNegative())
+ {
+ //Just add them as negitive numbers
+ LInfiniteInteger tmp= new LInfiniteInteger(anInfiniteInteger.toString());
+ tmp.isNegative = true;
+ return this.plus(tmp);
+
+ }
+ //We are not negative, and anInteger is negative (this - -that) = (this + that)
+ if(!this.isNegative && anInfiniteInteger.isNegative())
+ {
+ LInfiniteInteger tmp= new LInfiniteInteger(anInfiniteInteger.toString());
+ tmp.isNegative = false;
+ return this.plus(tmp);
+ }
+ //We are both negitive(-this - -that) = (-this + that)
+ if(this.isNegative && anInfiniteInteger.isNegative())
+ {
+ LInfiniteInteger tmp = new LInfiniteInteger(anInfiniteInteger.toString());
+ tmp.isNegative = false;
+ return this.plus(tmp);
+ }
+ //Figure out which number is lower in magnitude, and have that the the "fake" negitive, and maybe flip the signs back if nessessarry.
+ //Find both numbers without magnitude
+ LInfiniteInteger thisnomag = new LInfiniteInteger(this.toString());
+ thisnomag.isNegative = false;
+ LInfiniteInteger thatnomag = new LInfiniteInteger(anInfiniteInteger.toString());
+ thatnomag.isNegative = false;
+ boolean negateAtEnd = false;
+ LInfiniteInteger bigger = null;
+ LInfiniteInteger smaller = null;
+ if(thisnomag.compareTo(thatnomag) > 0)
+ {
+ //We are bigger
+ negateAtEnd = false;
+ bigger = new LInfiniteInteger(this.toString());
+ smaller = new LInfiniteInteger(anInfiniteInteger.toString());
+ }
+ else if(thisnomag.compareTo(thatnomag) < 0)
+ {
+ //The other guy is bigger
+ //System.out.println("[ME] " + anInfiniteInteger.toString() + " is bigger");
+ negateAtEnd = true;
+ bigger = new LInfiniteInteger(anInfiniteInteger.toString());
+ smaller = new LInfiniteInteger(this.toString());
+ }
+ else
+ {
+ //They are the same magnitude.
+ bigger = new LInfiniteInteger(this.toString());
+ smaller = new LInfiniteInteger(this.toString());
+ }
+ //System.out.println("[ME] Bigger number is " + bigger.toString() + " and smaller is " + smaller.toString() + " negateing at end: " + negateAtEnd);
+ //For each digit in the bigger, subtract the bigger - the smaller, borrowing where needed
+ Node tmp1 = bigger.lastNode;
+ Node tmp2 = smaller.lastNode;
+ LInfiniteInteger answer = new LInfiniteInteger("");
+ while(tmp1 != null)
+ {
+ //Get the numbers
+ int bignum = tmp1.data;
+ int smallnum = 0;
+ if(tmp2 != null)
+ {
+ smallnum = tmp2.data;
+ }
+ else
+ {
+ smallnum = 0;
+ }
+ //If the smaller number's digit is larger than the larger number's digit, we need to barrow
+ if(smallnum > bignum)
+ {
+ //System.out.println("[ME] Needing to barrow because " + smallnum + " > " + bignum);
+ barrow(tmp1.previous);
+ //System.out.println("[ME] After barrowing, number is " + bigger.toString());
+ bignum = bignum+10;
+ }
+ //Then do regular subtraction
+ //System.out.println("[ME] Adding " + (bignum - smallnum) + " to answer, answer is " + answer.toString() + " bigger is " + bigger.toString() + " smaller is " + smaller.toString());
+ answer.addfirst(bignum - smallnum);
+ tmp1 = tmp1.previous;
+ if(tmp2 != null)
+ {
+ tmp2 = tmp2.previous;
+ }
+ }
+ answer.removeLast(); // remove the 0
+ //Remove any 0's at the beginning.
+ for(Node tmp = answer.firstNode; tmp != null && tmp != answer.lastNode; tmp = tmp.next)
+ {
+ if(tmp.data == 0)
+ {
+ answer.removeFirst();
+ }
+ else
+ {
+ break;
+ }
+ }
+ //Retun the answer
+ answer.isNegative = negateAtEnd;
+ return answer;
+ }
+ private void barrow(Node n)
+ {
+ //System.out.println("[ME] Barrowing from " + n.data);
+ if(n.data != 0)
+ {
+ //System.out.println("[ME] Found something to barrow from: " + n.data);
+ n.data--;
+ }
+ else
+ {
+ barrow(n.previous);
+ n.data = 9;
+ }
+ }
+ /**
+ * Generates a string representing this infinite integer. If this infinite integer
+ * is a negative number a minus symbol should be in the front of numbers. For example,
+ * "-12345678901234567890". But if the infinite integer is a positive number, no symbol
+ * should be in the front of the numbers (e.g., "12345678901234567890").
+ * @return a string representing this infinite integer number.
+ */
+ public String toString()
+ {
+ String output = "";
+ if(isNegative)
+ {
+ output += "-";
+ }
+ for(Node tmp = firstNode; tmp != null; tmp = tmp.next)
+ {
+ //System.out.println("[ME] this node has " + tmp.data + " and it's next node is " + tmp.next);
+ output += tmp.data;
+ }
+ //System.out.println("[ME] Returning " + output);
+ return output;
+ }
+
+ /**
+ * Compares this infinite integer with anInfiniteInteger
+ * @return either -1, 0, or 1 as follows:
+ * If this infinite integer is less than anInfiniteInteger, return -1.
+ * If this infinite integer is equal to anInfiniteInteger, return 0.
+ * If this infinite integer is greater than anInfiniteInteger, return 1.
+ */
+ public int compareTo(InfiniteIntegerInterface anInfiniteInteger)
+ {
+ //System.out.println("[ME] Compareing " + this.toString() + " with " + anInfiniteInteger.toString());
+ //If they're different signs, we're done.
+ if(this.isNegative() && !anInfiniteInteger.isNegative())
+ {
+ return -1;
+ }
+ if(!this.isNegative() && anInfiniteInteger.isNegative())
+ {
+ return 1;
+ }
+ //Otherwise, they're the same sign, and we need to compare digits.
+ //If one has more digits than the other, we're also done.
+ if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits())
+ {
+ return 1;
+ }
+ if(anInfiniteInteger.getNumberOfDigits() > this.getNumberOfDigits())
+ {
+ return -1;
+ }
+ Node thattemp = new LInfiniteInteger(anInfiniteInteger.toString()).firstNode;
+ Node thistemp = this.firstNode;
+ for(; thistemp != null || thattemp != null;)
+ {
+ if(thistemp.data > thattemp.data)
+ {
+ return 1;
+ }
+ if(thistemp.data < thattemp.data)
+ {
+ return -1;
+ }
+ if(thistemp != null)
+ {
+ thistemp = thistemp.next;
+ }
+ if(thattemp != null)
+ {
+ thattemp = thattemp.next;
+ }
+ }
+ return 0;
+ }
+
+ /**
+ * Calculates the result of this infinite integer multiplied by anInfiniteInteger
+ * @param anInfiniteInteger the multiplier.
+ * @return a NEW infinite integer representing the result of this
+ * infinite integer multiplied by anInfiniteInteger.
+ */
+ public InfiniteIntegerInterface multiply(final InfiniteIntegerInterface anInfiniteInteger)
+ {
+ //Figure out if we're supposed to be negative or positive
+ boolean negateAtEnd = false;
+ //If we are negative and param is not, (- * +) answer will be negative
+ if(this.isNegative && !anInfiniteInteger.isNegative())
+ {
+ negateAtEnd = true;
+ }
+ //If we are positive, and param is negitive (+ * -) answer will be negative
+ if(!this.isNegative && anInfiniteInteger.isNegative())
+ {
+ negateAtEnd = true;
+ }
+ //Otherwise, we will be positive (+ * +) or (- * -)
+ //Unless one of the numbers is 0, in which case we can just return 0.
+ LInfiniteInteger thisusigned = new LInfiniteInteger(this.toString());
+ LInfiniteInteger thatusigned = new LInfiniteInteger(anInfiniteInteger.toString());
+ if((thisusigned.compareTo(new LInfiniteInteger("")) == 0) || (thatusigned.compareTo(new LInfiniteInteger("")) == 0))
+ {
+ return new LInfiniteInteger("");
+ }
+ //Find the number with more digits, and the one with fewer digits
+ LInfiniteInteger larger = null;
+ LInfiniteInteger smaller = null;
+ if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits())
+ {
+ larger = new LInfiniteInteger(this.toString());
+ smaller = new LInfiniteInteger(anInfiniteInteger.toString());
+ }
+ if(anInfiniteInteger.getNumberOfDigits() >= this.getNumberOfDigits())
+ {
+ larger = new LInfiniteInteger(anInfiniteInteger.toString());
+ smaller = new LInfiniteInteger(this.toString());
+ }
+ //For every digit, multiply each other number by the digit, in order, and multiply the next answer by 10 (or just add a 0 to it)
+ LInfiniteInteger answer = new LInfiniteInteger("");
+ int mult = 0; //Number to multiply the answers by
+ //System.out.println("[ME] Multiplying " + larger.toString() + " with " + smaller.toString());
+ for(Node n = smaller.lastNode; n != null; n = n.previous)
+ {
+ LInfiniteInteger thismult = new LInfiniteInteger("");
+ int carry = 0;
+ for(Node d = larger.lastNode; d != null; d = d.previous)
+ {
+ int product = n.data * d.data;
+ product += carry;
+
+ if(product > 9)
+ {
+ carry = product/10;
+ }
+ else
+ {
+ carry = 0;
+ }
+ product %= 10;
+ //System.out.println("[ME] Product is " + product + " carry is " + carry);
+ thismult.addfirst(product);
+ }
+ if(carry > 0)
+ {
+ thismult.addfirst(carry);
+ }
+
+ //Shift the mult over, so it will add up correctly
+ for(int i = 0; i < mult; i++)
+ {
+ thismult.addlast(0);
+ }
+ thismult.removeLast();
+ //System.out.println("[ME] Attempting to add " + answer.toString() + " with " + thismult.toString());
+ answer = (LInfiniteInteger) answer.plus(thismult);
+ mult++;
+ }
+ //answer.removeLast();
+ answer.isNegative = negateAtEnd;
+ //If it's -something * 0, answer will be -0, just make it +0
+ return answer;
+ }
+
+ private class Node
+ {
+ private int data;
+ private Node next;
+ private Node previous;
+
+ private Node(Node previousNode, int aData, Node nextNode)
+ {
+ previous = previousNode;
+ data = aData;
+ next = nextNode;
+ }
+
+ private Node(int aData)
+ {
+ this(null, aData, null);
+ }
+ }
+}
diff --git a/projects/project2_LInfiniteInteger/LInfiniteIntegerTester.java b/projects/project2_LInfiniteInteger/LInfiniteIntegerTester.java
new file mode 100644
index 0000000..503932d
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/LInfiniteIntegerTester.java
@@ -0,0 +1,1136 @@
+import java.util.Scanner;
+
+public class LInfiniteIntegerTester
+{
+ public static void main(String[] args)
+ {
+ int option = 1; // 1 to stop right after a FAIL
+ int numErrors = 0; // number of errors
+
+ System.out.println("Welcome to AInfiniteInteger Tester");
+ System.out.println("There are two options:");
+ System.out.println(" 1. Stop right after a FAIL");
+ System.out.println(" 2. Do not stop after a FAIL");
+ System.out.print("Please select an option (1 or 2): ");
+
+ @SuppressWarnings("resource")
+ Scanner in = new Scanner(System.in);
+
+ do
+ {
+ option = 1;//in.nextInt();
+ if(option != 1 && option != 2)
+ {
+ System.out.print("Invalid option. Please select an option (1 or 2): ");
+ }
+ }
+ while(option != 1 && option != 2);
+
+ // Testing the method getNumberOfDigits()
+
+ System.out.print("Checking the method getNumberOfDigits() of a positive integer: ");
+
+ InfiniteIntegerInterface gd1 = new LInfiniteInteger("123456789");
+
+ if(gd1.getNumberOfDigits() != 9)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd1 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("The integer 123456789 contains 9 digits.");
+ System.out.println("But your gd1.getNumberOfDigits() returns " + gd1.getNumberOfDigits() + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method getNumberOfDigits()
+
+ System.out.print("Checking the method getNumberOfDigits() of a negative integer: ");
+
+ InfiniteIntegerInterface gd2 = new LInfiniteInteger("-123456789");
+
+ if(gd2.getNumberOfDigits() != 9)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer -123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd2 = new LInfiniteInteger(\"-123456789\");");
+ System.out.println("The integer -123456789 contains 9 digits.");
+ System.out.println("But your gd2.getNumberOfDigits() returns " + gd2.getNumberOfDigits() + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method getNumberOfDigits()
+
+ System.out.print("Checking the method getNumberOfDigits() of a positive integer with leading 0s: ");
+
+ InfiniteIntegerInterface gd3 = new LInfiniteInteger("000123456789");
+
+ if(gd3.getNumberOfDigits() != 9)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd3 = new LInfiniteInteger(\"000123456789\");");
+ System.out.println("The integer 123456789 contains 9 digits.");
+ System.out.println("But your gd3.getNumberOfDigits() returns " + gd3.getNumberOfDigits() + ".");
+ System.out.println("Did you handle leading 0s?");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method getNumberOfDigits()
+
+ System.out.print("Checking the method getNumberOfDigits() of a negative integer with leading 0s: ");
+
+ InfiniteIntegerInterface gd4 = new LInfiniteInteger("-000123456789");
+
+ if(gd4.getNumberOfDigits() != 9)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer -123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd4 = new LInfiniteInteger(\"-000123456789\");");
+ System.out.println("The integer -123456789 contains 9 digits.");
+ System.out.println("But your gd4.getNumberOfDigits() returns " + gd4.getNumberOfDigits() + ".");
+ System.out.println("Did you handle leading 0s?");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method getNumberOfDigits()
+
+ System.out.print("Checking the method getNumberOfDigits() of 0: ");
+
+ InfiniteIntegerInterface gd5 = new LInfiniteInteger("0");
+
+ if(gd5.getNumberOfDigits() != 1)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 0 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd5 = new LInfiniteInteger(\"0\");");
+ System.out.println("The integer 0 contains 1 digits.");
+ System.out.println("But your gd5.getNumberOfDigits() returns " + gd5.getNumberOfDigits() + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method getNumberOfDigits()
+
+ System.out.print("Checking the method getNumberOfDigits() of 0 with leading 0s: ");
+
+ InfiniteIntegerInterface gd6 = new LInfiniteInteger("0000");
+
+ if(gd6.getNumberOfDigits() != 1)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 0 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd5 = new LInfiniteInteger(\"0000\");");
+ System.out.println("The integer 0 contains 1 digits.");
+ System.out.println("But your gd6.getNumberOfDigits() returns " + gd6.getNumberOfDigits() + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method toString()
+
+ System.out.print("Checking the method toString() of a positive integer: ");
+
+ if(!gd1.toString().equals("123456789"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd1 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("But your gd1.toString() returns " + gd1 + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method toString()
+
+ System.out.print("Checking the method toString() of a negative integer: ");
+
+ if(!gd2.toString().equals("-123456789"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer -123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd2 = new LInfiniteInteger(\"-123456789\");");
+ System.out.println("But your gd2.toString() returns " + gd2 + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method toString()
+
+ System.out.print("Checking the method toString() of a positive integer with leading 0s: ");
+
+ if(!gd3.toString().equals("123456789"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd3 = new LInfiniteInteger(\"000123456789\");");
+ System.out.println("But your gd3.toString() returns " + gd3 + ".");
+ System.out.println("Did you handle leading 0s?");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method toString()
+
+ System.out.print("Checking the method toString() of a negative integer with leading 0s: ");
+
+ if(!gd4.toString().equals("-123456789"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer -123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd4 = new LInfiniteInteger(\"-000123456789\");");
+ System.out.println("But your gd4.toString() returns " + gd4 + ".");
+ System.out.println("Did you handle leading 0s?");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method toString()
+
+ System.out.print("Checking the method toString() of a 0: ");
+
+ if(!gd5.toString().equals("0"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 0 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd5 = new LInfiniteInteger(\"0\");");
+ System.out.println("But your gd5.toString() returns " + gd5 + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method toString()
+
+ System.out.print("Checking the method toString() of a 0 with leading 0s: ");
+
+ if(!gd6.toString().equals("0"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 0 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface gd6 = new LInfiniteInteger(\"0000\");");
+ System.out.println("But your gd6.toString() returns " + gd6 + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method compareTo()
+
+ System.out.print("Checking the method compareTo(): ");
+
+ InfiniteIntegerInterface cp1 = new LInfiniteInteger("987654321");
+
+ if(cp1.compareTo(cp1) != 0)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 987654321 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp1 = new LInfiniteInteger(\"987654321\");");
+ System.out.println("cp1.compareTo(cp1) should return 0.");
+ System.out.println("But your cp1.compareTo(cp1) returns " + cp1.compareTo(cp1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method compareTo()
+
+ System.out.print("Checking the method compareTo(): ");
+
+ InfiniteIntegerInterface cp2 = new LInfiniteInteger("123456789");
+
+ if(cp1.compareTo(cp2) != 1)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 987654321 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp1 = new LInfiniteInteger(\"987654321\");");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp2 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("cp1.compareTo(cp2) should return 1.");
+ System.out.println("But your cp1.compareTo(cp2) returns " + cp1.compareTo(cp2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method compareTo()
+
+ System.out.print("Checking the method compareTo(): ");
+
+ if(cp2.compareTo(cp1) != -1)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 987654321 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp1 = new LInfiniteInteger(\"987654321\");");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp2 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("cp2.compareTo(cp1) should return -1.");
+ System.out.println("But your cp2.compareTo(cp1) returns " + cp2.compareTo(cp1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method compareTo()
+
+ System.out.print("Checking the method compareTo(): ");
+
+ InfiniteIntegerInterface cp3 = new LInfiniteInteger("123456789");
+
+ if(cp2.compareTo(cp3) != 0)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp2 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp3 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("cp2.compareTo(cp3) should return 0.");
+ System.out.println("But your cp2.compareTo(cp3) returns " + cp2.compareTo(cp3) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method compareTo()
+
+ System.out.print("Checking the method compareTo(): ");
+
+ InfiniteIntegerInterface cp4 = new LInfiniteInteger("-123456789");
+
+ if(cp3.compareTo(cp4) != 1)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp3 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("The integer -123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp4 = new LInfiniteInteger(\"-123456789\");");
+ System.out.println("cp3.compareTo(cp4) should return 1.");
+ System.out.println("But your cp3.compareTo(cp4) returns " + cp3.compareTo(cp4) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Testing the method compareTo()
+
+ System.out.print("Checking the method compareTo(): ");
+
+ if(cp4.compareTo(cp3) != -1)
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp3 = new LInfiniteInteger(\"123456789\");");
+ System.out.println("The integer -123456789 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface cp4 = new LInfiniteInteger(\"-123456789\");");
+ System.out.println("cp4.compareTo(cp3) should return -1.");
+ System.out.println("But your cp4.compareTo(cp3) returns " + cp4.compareTo(cp3) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS\n");
+ }
+
+ // Testing the method plus()
+
+ System.out.println("Testing the method plus()");
+ System.out.println("=========================");
+
+ System.out.println("The range of the infinite integer x is [-500,500].");
+ System.out.println("The range of the infinite integer y is [-500,500].");
+ System.out.print("Checking all possible values of x + y: ");
+
+ for(int i = -500; i <= 500; i++)
+ {
+ for(int j = -500; j <= 500; j++)
+ {
+ InfiniteIntegerInterface x = new LInfiniteInteger(i);
+ InfiniteIntegerInterface y = new LInfiniteInteger(j);
+
+ if(!((i + j) + "").equals(x.plus(y).toString()))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger x was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface x = new LInfiniteInteger(" + i + ");");
+ System.out.println("InfiniteInteger y was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface y = new LInfiniteInteger(" + j + ");");
+ System.out.println("The result of " + i + " + " + j + " should be " + (i + j) + ".");
+ System.out.println("But your x.plus(y) returns " + x.plus(y) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ }
+ }
+
+ System.out.println("PASS\n");
+
+ System.out.print("Calculating 12345678901234567890 + 12345678901234567890: ");
+ InfiniteIntegerInterface tp1 = new LInfiniteInteger("12345678901234567890");
+ if(!tp1.plus(tp1).toString().equals("24691357802469135780"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 12345678901234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("The result of 12345678901234567890 + 12345678901234567890 should be 24691357802469135780.");
+ System.out.println("But your tp1.plus(tp1) returns " + tp1.plus(tp1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating 12345678901234567890 + (-12345678901234567890): ");
+
+ InfiniteIntegerInterface tp2_1 = new LInfiniteInteger("12345678901234567890");
+ InfiniteIntegerInterface tp2_2 = new LInfiniteInteger("-12345678901234567890");
+ if(!tp2_1.plus(tp2_2).toString().equals("0"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 12345678901234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp2_1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("The integer -12345678901234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp2_2 = new LInfiniteInteger(\"-12345678901234567890\");");
+ System.out.println("The result of 12345678901234567890 + (-12345678901234567890) should be 0.");
+ System.out.println("But your tp2_1.plus(tp2_2) returns " + tp2_1.plus(tp2_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating 100000000000000000000 + (-1): ");
+ InfiniteIntegerInterface tp3_1 = new LInfiniteInteger("100000000000000000000");
+ InfiniteIntegerInterface tp3_2 = new LInfiniteInteger("-1");
+ if(!tp3_1.plus(tp3_2).toString().equals("99999999999999999999"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 100000000000000000000 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp3_1 = new LInfiniteInteger(\"100000000000000000000\");");
+ System.out.println("The integer -1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp3_2 = new LInfiniteInteger(\"-1\");");
+ System.out.println("The result of 100000000000000000000 + (-1) should be 99999999999999999999.");
+ System.out.println("But your tp3_1.plus(tp3_2) returns " + tp3_1.plus(tp3_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating (-1) + 100000000000000000000: ");
+ InfiniteIntegerInterface tp4_1 = new LInfiniteInteger("-1");
+ InfiniteIntegerInterface tp4_2 = new LInfiniteInteger("100000000000000000000");
+ if(!tp4_1.plus(tp4_2).toString().equals("99999999999999999999"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer -1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp4_1 = new LInfiniteInteger(\"-1\");");
+ System.out.println("The integer 100000000000000000000 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp4_2 = new LInfiniteInteger(\"100000000000000000000\");");
+ System.out.println("The result of (-1) + 100000000000000000000 should be 99999999999999999999.");
+ System.out.println("But your tp4_1.plus(tp4_2) returns " + tp4_1.plus(tp4_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating 1 + (-100000000000000000000): ");
+ InfiniteIntegerInterface tp5_1 = new LInfiniteInteger("1");
+ InfiniteIntegerInterface tp5_2 = new LInfiniteInteger("-100000000000000000000");
+ if(!tp5_1.plus(tp5_2).toString().equals("-99999999999999999999"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp5_1 = new LInfiniteInteger(\"1\");");
+ System.out.println("The integer -100000000000000000000 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp5_2 = new LInfiniteInteger(\"-100000000000000000000\");");
+ System.out.println("The result of 1 + (-100000000000000000000) should be -99999999999999999999.");
+ System.out.println("But your tp5_1.plus(tp5_2) returns " + tp5_1.plus(tp5_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating (-100000000000000000000) + 1: ");
+ InfiniteIntegerInterface tp6_1 = new LInfiniteInteger("-100000000000000000000");
+ InfiniteIntegerInterface tp6_2 = new LInfiniteInteger("1");
+ if(!tp6_1.plus(tp6_2).toString().equals("-99999999999999999999"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer -100000000000000000000 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp6_1 = new LInfiniteInteger(\"-100000000000000000000\");");
+ System.out.println("The integer 1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tp6_2 = new LInfiniteInteger(\"1\");");
+ System.out.println("The result of (-100000000000000000000) + 1 should be -99999999999999999999.");
+ System.out.println("But your tp6_1.plus(tp6_2) returns " + tp6_1.plus(tp6_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS\n");
+ }
+
+ // Testing the method minus()
+
+ System.out.println("Testing the method minus()");
+ System.out.println("==========================");
+
+ System.out.println("The range of the infinite integer x is [-500,500].");
+ System.out.println("The range of the infinite integer y is [-500,500].");
+ System.out.print("Checking all possible values of x - y: ");
+
+ for(int i = -500; i <= 500; i++)
+ {
+ for(int j = -500; j <= 500; j++)
+ {
+ InfiniteIntegerInterface x = new LInfiniteInteger(i);
+ InfiniteIntegerInterface y = new LInfiniteInteger(j);
+
+ if(!((i - j) + "").equals(x.minus(y).toString()))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger x was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface x = new LInfiniteInteger(" + i + ");");
+ System.out.println("InfiniteInteger y was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface y = new LInfiniteInteger(" + j + ");");
+ System.out.println("The result of " + i + " - " + j + " should be " + (i - j) + ".");
+ System.out.println("But your x.minus(y) returns " + x.minus(y) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ }
+ }
+
+ System.out.println("PASS\n");
+
+ InfiniteIntegerInterface tm1 = new LInfiniteInteger("12345678901234567890");
+
+ System.out.print("Calculating 12345678901234567890 - 12345678901234567890: ");
+ if(!tm1.minus(tm1).toString().equals("0"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 12345678901234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tm1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("The result of 12345678901234567890 - 12345678901234567890 should be 0.");
+ System.out.println("But your tm1.minus(tm1) returns " + tm1.minus(tm1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ InfiniteIntegerInterface tm2_1 = new LInfiniteInteger("12345678901234567890");
+ InfiniteIntegerInterface tm2_2 = new LInfiniteInteger("1234567890");
+
+ System.out.print("Calculating 12345678901234567890 - 1234567890: ");
+ if(!tm2_1.minus(tm2_2).toString().equals("12345678900000000000"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 12345678901234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tm2_1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("The integer 1234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tm2_2 = new LInfiniteInteger(\"1234567890\");");
+ System.out.println("The result of 12345678901234567890 - 1234567890 should be 12345678900000000000.");
+ System.out.println("But your tm2_1.minus(tm2_2) returns " + tm2_1.minus(tm2_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ InfiniteIntegerInterface tm3_1 = new LInfiniteInteger("1234567890");
+ InfiniteIntegerInterface tm3_2 = new LInfiniteInteger("12345678901234567890");
+
+ System.out.print("Calculating 1234567890 - 12345678901234567890: ");
+ if(!tm3_1.minus(tm3_2).toString().equals("-12345678900000000000"))
+ {
+ System.out.println("FAIL");
+ System.out.println("The integer 1234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tm3_1 = new LInfiniteInteger(\"1234567890\");");
+ System.out.println("The integer 12345678901234567890 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tm3_2 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("The result of 1234567890 - 12345678901234567890 should be -12345678900000000000.");
+ System.out.println("But your tm3_1.minus(tm3_2) returns " + tm3_1.minus(tm3_2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS\n");
+ }
+
+ // Testing the method multiply()
+
+ System.out.println("Testing the method multiply()");
+ System.out.println("=============================");
+
+ System.out.println("The range of the infinite integer x is [-500,500].");
+ System.out.println("The range of the infinite integer y is [-500,500].");
+ System.out.print("Checking all possible values of x * y: ");
+
+ for(int i = -500; i <= 500; i++)
+ {
+ for(int j = -500; j <= 500; j++)
+ {
+ InfiniteIntegerInterface x = new LInfiniteInteger(i);
+ InfiniteIntegerInterface y = new LInfiniteInteger(j);
+
+ if(!((i * j) + "").equals(x.multiply(y).toString()))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger x was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface x = new LInfiniteInteger(" + i + ");");
+ System.out.println("InfiniteInteger y was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface y = new LInfiniteInteger(" + j + ");");
+ System.out.println("The result of " + i + " * " + j + " should be " + (i * j) + ".");
+ System.out.println("But your x.multiply(y) returns " + x.multiply(y) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ }
+ }
+
+ System.out.println("PASS\n");
+
+ System.out.print("Calculating 12345678901234567890 * 12345678901234567890: ");
+
+ InfiniteIntegerInterface tml1 = new LInfiniteInteger("12345678901234567890");
+ String tml1Result = "152415787532388367501905199875019052100";
+
+ if(!tml1.multiply(tml1).toString().equals(tml1Result))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger tml1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("The result of " + tml1 + " * " + tml1 + " should be " + tml1Result + ".");
+ System.out.println("But your tml1.multiply(tml1) returns " + tml1.multiply(tml1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating 12345678901234567890 * -12345678901234567890: ");
+
+ InfiniteIntegerInterface tml2 = new LInfiniteInteger("-12345678901234567890");
+
+ if(!tml1.multiply(tml2).toString().equals("-" + tml1Result))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger tml1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("InfiniteInteger tml2 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml2 = new LInfiniteInteger(\"-12345678901234567890\");");
+ System.out.println("The result of " + tml1 + " * " + tml2 + " should be -" + tml1Result + ".");
+ System.out.println("But your tml1.multiply(tml2) returns " + tml1.multiply(tml2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating -12345678901234567890 * 12345678901234567890: ");
+
+ if(!tml2.multiply(tml1).toString().equals("-" + tml1Result))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger tml1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml1 = new LInfiniteInteger(\"12345678901234567890\");");
+ System.out.println("InfiniteInteger tml2 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml2 = new LInfiniteInteger(\"-12345678901234567890\");");
+ System.out.println("The result of " + tml2 + " * " + tml1 + " should be -" + tml1Result + ".");
+ System.out.println("But your tml2.multiply(tml1) returns " + tml2.multiply(tml1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Calculating -12345678901234567890 * -12345678901234567890: ");
+
+ if(!tml2.multiply(tml2).toString().equals(tml1Result))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger tml2 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml2 = new LInfiniteInteger(\"-12345678901234567890\");");
+ System.out.println("The result of " + tml2 + " * " + tml2 + " should be " + tml1Result + ".");
+ System.out.println("But your tml2.multiply(tml2) returns " + tml2.multiply(tml2) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Multiply by 0
+
+ System.out.print("Calculating 12345678901234567890 * 0: ");
+
+ InfiniteIntegerInterface tml0 = new LInfiniteInteger("0");
+
+ if(!tml1.multiply(tml0).toString().equals("0"))
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger tml1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml1 = new LInfiniteInteger(\"-12345678901234567890\");");
+ System.out.println("InfiniteInteger tml0 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml0 = new LInfiniteInteger(\"0\");");
+ System.out.println("The result of " + tml1 + " * " + tml0 + " should be " + tml1Result + ".");
+ System.out.println("But your tml1.multiply(tml0) returns " + tml1.multiply(tml0) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Checking equivalent to zero (compareTo): ");
+
+ InfiniteIntegerInterface tml3 = tml1.multiply(tml0);
+
+ if(tml3.compareTo(tml0) != 0)
+ {
+ System.out.println("FAIL");
+ System.out.println("InfiniteInteger tml1 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml1 = new LInfiniteInteger(\"-12345678901234567890\");");
+ System.out.println("InfiniteInteger tml0 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml0 = new LInfiniteInteger(\"0\");");
+ System.out.println("InfiniteInteger tml3 was constructed using the following statement:");
+ System.out.println(" InfiniteIntegerInterface tml3 = tml1.multiply(tml0);");
+ System.out.println("The result of calling tml3.compareTo(tml0) should be 0.");
+ System.out.println("But your tml3.compareTo(tml0) returns " + tml3.compareTo(tml0) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+
+ System.out.println();
+
+ // Crazy Tests
+
+ System.out.println("Let's go crazy.");
+ System.out.println("Calculating values of 2^1, 2^2, 2^4, 2^8, ..., 2^1024 using loops of additions");
+
+ int resultNumber = 0;
+ int[] resultNumDigits = new int[11];
+ String[] resultStrings = new String[11];
+ resultNumDigits[0] = 1;
+ resultStrings[0] = "2";
+ resultNumDigits[1] = 1;
+ resultStrings[1] = "4";
+ resultNumDigits[2] = 2;
+ resultStrings[2] = "16";
+ resultNumDigits[3] = 3;
+ resultStrings[3] = "256";
+ resultNumDigits[4] = 5;
+ resultStrings[4] = "65536";
+ resultNumDigits[5] = 10;
+ resultStrings[5] = "4294967296";
+ resultNumDigits[6] = 20;
+ resultStrings[6] = "18446744073709551616";
+ resultNumDigits[7] = 39;
+ resultStrings[7] = "340282366920938463463374607431768211456";
+ resultNumDigits[8] = 78;
+ resultStrings[8] = "115792089237316195423570985008687907853269984665640564039457584007913129639936";
+ resultNumDigits[9] = 155;
+ resultStrings[9] = "13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096";
+ resultNumDigits[10] = 309;
+ resultStrings[10] = "179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216";
+
+ System.out.println("This test is done by first construct the infinit integer x using the followoing statement:");
+ System.out.println(" InfiniteIntegerInterface x = new LInfiniteInteger(\"1\");");
+ System.out.println("Then repeatedly executing the following statement:");
+ System.out.println(" x = x.plus(x);\n");
+
+ for(int n = 1; n <= 1024; n = n + n)
+ {
+ InfiniteIntegerInterface x = new LInfiniteInteger("1");
+
+ for(int i = 1; i <= n; i++)
+ {
+ x = x.plus(x);
+ }
+ System.out.print("2^" + n + " is " + x + " containing " + x.getNumberOfDigits() + " digits: ");
+ if(x.getNumberOfDigits() != resultNumDigits[resultNumber] || !x.toString().equals(resultStrings[resultNumber]))
+ {
+ System.out.println("FAIL");
+ System.out.println("2^" + n + " should be " + resultStrings[resultNumber] + " which contains " + resultNumDigits[resultNumber] + " digits.");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ resultNumber++;
+ }
+
+ System.out.println();
+
+ int numDigitsGoal = 1000;
+ String crazyResultString = "1313879718456104225921995528497555855625152414017051819746489965175418594035237238181967239932841927943368022817082758088019245450978689926961358127631787144754373062120106902504163460853000843067796699510293924385333858122456855065858902248896798237625003273316840048212276610565777594650220087971045508055611314300070786176626378923084556948528977153135635206046615173944871186308702998089733796437598174864856900497873863589167218546523213052953471290292740839713119247294213720517263787354623928731924441813068650485005632246166869508621489664962699281739398162579362377493635567561343072827446535980932480283909225368443393486477711655376434350643031227524710815041165924570415486219444700695031601993251625411314037854648482400238147123249264167061961509379444920959953800364778020888142773659393688842351560111550969115614788364312559110854239694359378947557812483113318119934088060561737965196192041096299817733313038159648080136912212919336690293080257579650807503654066749005924284432908288";
+ int resultOfx = 3319;
+
+ System.out.println("Let's go to the extreme.");
+ System.out.println("Let's find the number x where 2^x contains " + numDigitsGoal + " digits.");
+
+ InfiniteIntegerInterface crazy = new LInfiniteInteger("1");
+ int result = 0;
+ while(crazy.getNumberOfDigits() < numDigitsGoal)
+ {
+ crazy = crazy.plus(crazy);
+ result++;
+ }
+
+ System.out.println("2^" + result + " is " + crazy + ".");
+ System.out.println("2^" + result + " contains " + numDigitsGoal + " digits.");
+
+ System.out.print("Crazy test: ");
+
+ if(resultOfx == result && crazyResultString.equals(crazy.toString()))
+ {
+ System.out.println("PASS");
+ }
+ else
+ {
+ System.out.println("FAIL");
+ System.out.println("The answer should be 2^" + resultOfx + ".");
+ System.out.println("Where 2^" + resultOfx + " is equal to " + crazyResultString + ".");
+ if(option == 1)
+ {
+ return;
+ }
+ else
+ {
+ numErrors++;
+ }
+ }
+
+ // Show number of errors
+
+ if(numErrors != 0)
+ {
+ System.out.println("\nThere are " + numErrors + " errors detected.");
+ }
+ else
+ {
+ System.out.println("\nThere are no errors detected. Good Job!!!");
+ }
+ }
+}
diff --git a/projects/project2_LInfiniteInteger/LStack.java b/projects/project2_LInfiniteInteger/LStack.java
new file mode 100644
index 0000000..15e9bed
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/LStack.java
@@ -0,0 +1,66 @@
+
+public class LStack<T> implements StackInterface<T>
+{
+ private Node firstNode;
+
+ public LStack()
+ {
+ firstNode = null;
+ }
+
+ public void push(T anEntry)
+ {
+ Node newNode = new Node(anEntry, firstNode);
+ firstNode = newNode;
+ }
+
+ public T pop()
+ {
+ T result = null;
+
+ if(firstNode != null)
+ {
+ result = firstNode.data;
+ firstNode = firstNode.next;
+ }
+
+ return result;
+ }
+
+ public T peek()
+ {
+ if(firstNode != null)
+ {
+ return firstNode.data;
+ }
+
+ return null;
+ }
+
+ public boolean isEmpty()
+ {
+ return firstNode == null;
+ }
+
+ public void clear()
+ {
+ firstNode = null;
+ }
+
+ private class Node
+ {
+ private T data;
+ private Node next;
+
+ private Node(T aData)
+ {
+ this(aData, null);
+ }
+
+ private Node(T aData, Node nextNode)
+ {
+ data = aData;
+ next = nextNode;
+ }
+ }
+}
diff --git a/projects/project2_LInfiniteInteger/StackInterface.java b/projects/project2_LInfiniteInteger/StackInterface.java
new file mode 100644
index 0000000..1007f13
--- /dev/null
+++ b/projects/project2_LInfiniteInteger/StackInterface.java
@@ -0,0 +1,9 @@
+
+public interface StackInterface<T>
+{
+ public void push(T anEntry);
+ public T pop();
+ public T peek();
+ public boolean isEmpty();
+ public void clear();
+}