We will create a program to add and subtract arbitrarily large integers. This program will be performed in two different ways. When your program starts, it will accept two strings of numbers (large integers - possibly with a minus sign) from the user, separated by either a (+) or (-) sign. Your program will then either add them together or subtract them. This will be done with the following approach:

1. Pad the shorter string with "0" characters on the left. You might want to pad the longer string also, to remove any special case with the high-order digits.

2. Push all the characters, one at a time, onto separate stacks, starting at the high-order digits (the left-hand side of the string). This will result in the low-order digits at the top of each of the two stacks.

3. Repeat: Pop the characters off one at a time from each stack, perform the appropriate operation, and push the resulting character onto a third stack. Set a carry (if adding) or a borrow (if subtracting) if necessary for inclusion in the next items. Repeat this until done.

4. Pop the characters off the result stack and concatenate into a String. Display the string in order to display the result.

Note that you might come out with a negative result. You must account for this possibility and place the minus at the start of your answer.

The two ways to implement this program will come from the fact that we will use two different versions of stacks for this project:

  • We will create a stack using java.util.LinkedList. Test your stack for LIFO behavior with a driver program.
  • We will create a linked stack in which we actually create the nodes, push and pop, using the coding approach pertaining to linked lists.

When your programs run, they should read a file named "addsAndSubtracts.txt." The programs should show each expression to be evaluated and the answer for each expression in the file.

Both your programs should have identical interfaces. Your program that adds and subtracts the numbers should run identically regardless of whether it is using the stack you create with java.util.LinkedList or the one you create with your own linked stack.

Notes

  • Submit two separate subdirectories, one for each solution
  • Arithmetic review: 8 - (-3) = 11; -8 - (-3) = -5; -8 - (3) = -11;
  • Attacking the problem: Figure out which is the larger number and set "bigger" and "smaller". Decide if you need to add or subtract. Decide if you need a (-) sign on the result.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.