# Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

###### Question:

Dynamic Implementation of Stack -

The purpose is to use our dynamic implementation of stack. The application will be to add large numbers.

Remember that we can use stacks to safely add integer values that overflow the int data type

• g. in Java, the maximum possible int value Integer.MAX_VALUE is:
2147483647
• so any int addition larger than this will overflow and fail

Using stacks to add large numbers safely

Will actually represent the large integers to be added as strings of characters, to avoid any overflow problems here. Then will use three(!) stacks of datatype Integer to do the addition safely.

The idea is to add each pair of digits from the numbers, from right to left. The units part of this total is pushed to the results stack, the tens part is the carry digit, added in to the next pair.

The addition algorithm in pseudocode is something like:

$//push digits from numbers into stacks in appropriate order loop for the digits in the first number push digit to first stack loop for the digits in the second number push digit to second stack //pop stacks, add digits and push result to result stack. Careful with carry while (!stacks are empty) pop digits from stacks and add push units part to result stack tens part is the carry to be added in next iteration //print the result from the result stack while (!result stack is empty) pop result stack$

Open the project and see Tester::main(). You must write code in the Tester::main() method that uses the stack implementation to add the pairs of numbers given.

Format your output so that it shows the first and second numbers, and your result (I've used '?' below to show where your result will go). Run your program three times to test each of the pairs of numbers provided e.g.

$1 + 2147483647 = ? 2147483647 + 1 = ? 2147483647 + 2147483647 = ?$

public class Tester
{
public static void main()
{
//first shorter
String first = "1";
String second = "2147483647";

//second shorter
//String first = "2147483647";
//String second = "1";

//same length
//String first = "2147483647";
//String second = "2147483647";

//you must write all of your code here }
}

public class StackUnderflowException extends RuntimeException
{
public StackUnderflowException()
{
super();
}

public StackUnderflowException(String message)
{
super(message);
}
}

{
private Item<T> top;

{
top = null;
}

public void push(T element)
{
Item<T> item = new Item<T>(element);

if (!isEmpty())
item.next = top;

top = item;
}

public T pop() throws StackUnderflowException
{
if (isEmpty())
throw new StackUnderflowException("Pop attempted on empty stack");
else {
T info = top.info;
top = top.next;
return info;
}
}

public boolean isEmpty()
{
}
}

public interface StackInterface<T>
{
void push(T element);

T pop() throws StackUnderflowException;

boolean isEmpty();
}

public class Item<T>
{
protected T info;
protected Item<T> next;

public Item()
{
info = null;
next = null;
}

public Item(T info)
{
this.info = info;
next = null;
}
}

#### Similar Solved Questions

##### 11.57 on Instrumentation and Measurement (Direct, Fast, and Accurate Measurement of VT arnd K of MOS...
11.57 on Instrumentation and Measurement (Direct, Fast, and Accurate Measurement of VT arnd K of MOS Transistor Using r Sift Circuit, 1991, Vol. 40, pp. 951-955) described the use of a simple linear rearession model to express drairn current y in milliamperes) as a function af ground-to-source volta...
##### When testing the hypothesized difference between two population means, the implied hypothesis is H0: µ1 =...
When testing the hypothesized difference between two population means, the implied hypothesis is H0: µ1 = 0 H0: µ1 - µ2 = 0 H0: µ2 = 0 H0: µ1 - µ2 ≠ 0...
##### Discuss how the Supreme Court has used the Fourteenth Amendment to apply the Bill of Rights...
Discuss how the Supreme Court has used the Fourteenth Amendment to apply the Bill of Rights to the States....
##### The authors of a paper titled "Age and Violent Content Labels Make Video Games Forbidden Fruits...
The authors of a paper titled "Age and Violent Content Labels Make Video Games Forbidden Fruits for Youth" carried out an experiment to determine if restrictive labels on video games actually increased the attractiveness of the game for young game players. Participants read a description of...
##### Please help with this problem Ext-2.2 (35 points) An insulated, rigid tank is divided into two...
please help with this problem Ext-2.2 (35 points) An insulated, rigid tank is divided into two compartments by a frictionless, thermally conducting piston. One compartment initially contains 0.6 m' of saturated water vapor at 200 °C and the other compartment contains 0.8 m' of superhe...
##### Consider a disc of radius R centered the origin in space and rotating about the z...
Consider a disc of radius R centered the origin in space and rotating about the z axis in some inertial reference frame O. (a) What is the maximum angular velocity w of the disc allowed by Special Relativity? Recall angular velocity is 2 times the number of rotations per second. (b) Consider an obse...
##### The description of a cell energy process is listed below. . produces carbon dioxide .occurs in...
The description of a cell energy process is listed below. . produces carbon dioxide .occurs in a cell's mitochondria involves the formation of the electron carriers NADH and FADHa produces a small amount of ATP Which process is described? o the electron transport chain O glycolysis O the citric ...
##### Need help 3. For the circuit given, determine the average power dissipated in the 100 k2...
need help 3. For the circuit given, determine the average power dissipated in the 100 k2 resistor. (5 pts) 100 S2 1:10 + w 50 V rms V2 100 k2...
##### I am new to Python and am having trouble coming up with writing code to the...
I am new to Python and am having trouble coming up with writing code to the following problem... The program must: Prompt for a file name Opens that file and reads through the file Displays a custom error message if the file does not exist You can pull the hour out from the 'From ' line by ...
A firm has a return on equity of 23 percent. The total asset turnover is 2.2 and the profit margin is 6 percent. The total equity is $5,600. What is the net income? Multiple Choice$739 $2,834$336 $1,288$585...