Question

Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition...

Description:
\\ implement in Java


Based on Chapter 6, programming project 1

Hewlett-Packard has a tradition of creating stack-based calculators. Rather than using standard algebraic notation ( 1 + 1 = ), the user would enter the values, then the operator. The calculator had an “enter” key to push each value onto a stack, then pop the stack when an operation key (e.g. “+” or “-”) was pressed.

Assignment:


Create a Calculator class that implements the provided StackCalculator interface

Requirement
Constructor:
+ Calculator()
   Precondition: none
   Postcondition: a new calculator with an empty number stack

Methods:
+ public void enter(String entry)
   Precondition: entry is either
   a double value (operand)
   a recognized operator [ +, -, *, /]
   Postcondition: if the entry is a double, the value is pushed onto the calculator’s number stack. If an operator, the top two values are popped from the stack, the operation performed, and the result pushed onto the stack. Note: the second operand is popped first.

+ double peek()
Precondition: number stack is not empty, otherwise throws EmptyStackException
Looks at the number at the top of this stack without removing it from the stack.

+ double pop()
Precondition: number stack is not empty, otherwise throws EmptyStackException
Removes the number at the top of this stack and returns that value.

+ void clear()
Removes all numbers from this stack. The stack will be empty after this call returns

+ boolean isEmpty
Tests If the number stack of this calculator is empty

+ int size()
Returns the number of values in this calculators number stack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//StackCalculator.java

public interface StackCalculator {

       /*

       * Precondition: entry is either

             a double value (operand)

             a recognized operator [ +, -, *, /]

             Postcondition: if the entry is a double, the value is pushed onto the calculator’s number stack.

             If an operator, the top two values are popped from the stack, the operation performed, and the result pushed onto the stack.

             Note: the second operand is popped first.

       */

       public void enter(String entry);

       /*

       * Precondition: number stack is not empty, otherwise throws EmptyStackException

             Looks at the number at the top of this stack without removing it from the stack.

       */

       double peek();

       /*

       * Precondition: number stack is not empty, otherwise throws EmptyStackException

              Removes the number at the top of this stack and returns that value.

       */

       double pop();

       /*

       * Removes all numbers from this stack. The stack will be empty after this call returns

       */

       void clear();

       /*

       * Tests If the number stack of this calculator is empty

       */

       boolean isEmpty();

       /*

       * Returns the number of values in this calculators number stack.

       */

       int size();

}

//end of StackCalculator.java

//Calculator.java

import java.util.EmptyStackException;

import java.util.Stack;

public class Calculator implements StackCalculator{

      

       private Stack<Double> numberStack;

       /*

       * Precondition: none

             Postcondition: a new calculator with an empty number stack

       */

       public Calculator()

       {

             numberStack = new Stack<Double>();

       }

      

       @Override

       public void enter(String entry) {

            

             if(entry.equalsIgnoreCase("+") || entry.equalsIgnoreCase("-") || entry.equalsIgnoreCase("*") || entry.equalsIgnoreCase("/"))

             {

                    double op2 = pop();

                    double op1 = pop();

                    switch(entry)

                    {

                    case "+":

                           numberStack.push(op1 + op2);

                           break;

                    case "-":

                           numberStack.push(op1 - op2);

                           break;

                    case "*":

                           numberStack.push(op1*op2);

                           break;

                    case "/":

                           numberStack.push(op1/op2);

                           break;

                    }

             }else {

                    numberStack.push(Double.parseDouble(entry));

             }

       }

       @Override

       public double peek() {

             if(isEmpty())

                    throw new EmptyStackException();

             else

                    return numberStack.peek();

       }

      

       @Override

       public double pop() {

             if(isEmpty())

                    throw new EmptyStackException();

             else

                    return numberStack.pop();

       }

       @Override

       public void clear() {

             while(!isEmpty())

             {

                    numberStack.pop();

             }

       }

       @Override

       public boolean isEmpty() {

             return(numberStack.isEmpty());

       }

       @Override

       public int size() {

             return numberStack.size();

       }

}

//end of Calculator.java

Add a comment
Know the answer?
Add Answer to:
Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT