Menu

[Solved]Java Add Subtract Two Numbers Using Stack Adt Solving Sum Difference Using Data Structure Q37157281

JAVA
Add / subtract two numbers using a Stack ADT
Solving a sum and difference using a data structureapproach

Use a Stack ADT with the implementation of Link) 1. Read two integer numbers from the user. Hint: You dont need to use an

public class LLNode<T> {

   private T data;
   private LLNode<T> nextNode;

   public LLNode(T data) {
       setData(data);
       nextNode = null;
   }

   public T getData() {
       return data;
   }

   public void setData(T data) {
       this.data = data;
   }

   public LLNode<T> getNextNode() {
       return nextNode;
   }

   public void setNextNode(LLNode<T> nextNode){
       this.nextNode = nextNode;
   }
  
}

public interface StackInterface<T> {

   public T pop() throws StackUnderflowException;

   public T peek() throws StackUnderflowException;

   public boolean isEmpty();

   public int size();

}

public interface UnboundedStackInterface<T> extendsStackInterface<T> {

   void push(T element);
  
}

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

}

public class StackOverflowException extends RuntimeException{
  
   public StackOverflowException() {
       super();
   }
  
   public StackOverflowException(String message) {
       super(message);
   }
  
}

public class LinkedStackCalc<T> implementsUnboundedStackInterface<T> {

   @Override
   public T pop() throws StackUnderflowException {
       return null;
   }

   @Override
   public T peek() throws StackUnderflowException {
       return null;
   }

   @Override
   public boolean isEmpty() {
       return false;
   }

   @Override
   public int size() {
       return 0;
   }

   @Override
   public void push(T element) throwsStackOverflowException {
      
   }
}

Use a Stack ADT with the implementation of Link) 1. Read two “integer” numbers from the user. Hint: You don’t 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 staclk Ask user for the maximum length of the digits if necessary (depends on your structure’s boundedness) Use the algorithm 2. 3. to simulate the addition of the two numbers 4. 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 Display result of sum Handle having numbers of different digit lengths-if you are popping elements from number stacks of different sizes, your checks should take that into account e. f. Develop a new algorithm to simulate the subtraction of the two numbers 5. The algorithm will be somewhat similar. Assume you will always subtract number2 from number1 Assume that number1 will always be larger than number2, your result will always be positive Display the result of the difference Handle having numbers of different digit lengths – number stacks of different sizes, your checks should take that into account. For the simplified subtraction, only number1 may have more digits than number2. a. b. C. d. if you are popping elements from e. 6. Allow user to repeat multiple times, either to add or subtract more numbers Show transcribed image text Use a Stack ADT with the implementation of Link) 1. Read two “integer” numbers from the user. Hint: You don’t 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 staclk Ask user for the maximum length of the digits if necessary (depends on your structure’s boundedness) Use the algorithm 2. 3. to simulate the addition of the two numbers 4. 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 Display result of sum Handle having numbers of different digit lengths-if you are popping elements from number stacks of different sizes, your checks should take that into account e. f. Develop a new algorithm to simulate the subtraction of the two numbers 5. The algorithm will be somewhat similar. Assume you will always subtract number2 from number1 Assume that number1 will always be larger than number2, your result will always be positive Display the result of the difference Handle having numbers of different digit lengths – number stacks of different sizes, your checks should take that into account. For the simplified subtraction, only number1 may have more digits than number2. a. b. C. d. if you are popping elements from e. 6. Allow user to repeat multiple times, either to add or subtract more numbers

Expert Answer


Answer to JAVA Add / subtract two numbers using a Stack ADT Solving a sum and difference using a data structure approach public cl… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *