Menu

(Solved) : Java Testing Class Main Method Different Class Java File Perhaps Attempt Aclass Extends St Q37247593 . . .

Java

***** testing class with the MAIN method should be itsown different class .java file*******

perhaps attempt AClass<E> extendsStack<E>{}

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 (Arrayor Link), it should not make a difference

2. Read two “integer” numbers from the user. Hint: You don’tneed to use an int type when you read, it may be easier to parsethe input as a string and convert to int as you push to thestack

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

4. Use the algorithm discussed in class to simulate the additionof 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 number2may be longer or shorter. You should be able to handle allcases.

d. Rough algorithm below:

 Repeat as long as there is something to pop

• Pop digits and add (including the carry over if oneexists)

• 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 overthe 1 and use it in the next addition

e. Display result of sum

f. Handle an empty stack appropriately – handle and empty stackappropriately, remember one empty stack doesn’t mean the additionprocess is over

5. Develop a new algorithm to simulate the subtraction of thetwo 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, yourresult will always be positive

d. Display the result of the difference

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

6. Allow user to repeat multiple times, either to add orsubtract

Expert Answer


Answer to Java ***** testing class with the MAIN method should be its own different class .java file******* perhaps attempt AClass…

OR