Question: Java Project 1 Add / subtract two numbers using a Stack ADT Solving a sum and difference using a data structure approach 1. Use a

Java

Project 1 Add / subtract two numbers using a Stack ADT

Solving a sum and difference using a data structure approach

1. Use a Stack ADT with the implementation of your choice (Array or Link), it should not make a difference

2. Read two integer numbers from the user. Hint: You dont need to use an int type when you read, it may be easier to parse the input as a string and convert to int as you push to the stack

3. Ask user for the maximum length of the digits if necessary (depends on your structures boundedness).

4. Use the algorithm discussed in class to simulate the addition of the two numbers

a. Push digits of number1 in stack 1

b. Push digits of number2 in stack 2

c. Numbers may or may not be of the same length. Also number2 may be longer or shorter. You should be able to handle all cases.

d. Rough algorithm below:

Repeat as long as there is something to pop

Pop digits and add (including the carry over if one exists)

Place result in output stack, one digit only, so if sum is 14, place the 4 in the output stack and

Carry over if necessary, in the example above, carry over the 1 and use it in the next addition

e. Display result of sum

f. Handle an empty stack appropriately handle and empty stack appropriately, remember one empty stack doesnt mean the addition process is over

5. Develop a new algorithm to simulate the subtraction of the two numbers

a. The algorithm will be somewhat similar.

b. Assume you will always subtract number2 from number1

c. Assume that number1 will always be larger than number2, your result will always be positive

d. Display the result of the difference

e. Handle an empty stack appropriately, the subtraction may continue even if the stack for number2 is empty

6. Allow user to repeat multiple times, either to add or subtract

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!