Question: JAVA We will create a program to add and subtract arbitra We will create a program to add and subtract arbitrarily large integers. When your

JAVA

We will create a program to add and subtract arbitra

We will create a program to add and subtract arbitrarily large integers. When your program starts, it will repeatedly read arithmetic expressions with two operands and an operator (+ or -) from a file. Your program will then either add or subtract and present the results. 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 with one zero, 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 pair of items. Repeat this until done.

4. Pop the characters off the result stack and concatenate into a String. Display nicely formatted versions of all three strings in order to display the result. The result should go to the console.

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 arithmetic expressions to be calculated will be written one-per-line in this file. 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.

rily large integers. When your program starts, it will repeatedly read arithmetic expressions with two operands and an operator (+ or -) from a file. Your program will then either add or subtract and present the results. 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 with one zero, 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 pair of items. Repeat this until done. 4. Pop the characters off the result stack and concatenate into a String. Display nicely formatted versions of all three strings in order to display the result. The result should go to the console. 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 arithmetic expressions to be calculated will be written one-per-line in this file. 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!