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.
//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
Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition...
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 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...
Reverse Polish (HP) Style Calculator - Part 3 The purpose of this assignment is to build the business calculator using supporting files built in Topics 4 and 5. Create a Java application file named RPN.java containing a main method by using the ForthStack.java and associated files from Topic 5. The application should have one text box for numeric data entry, one text box for numeric display, one text box for error display, and buttons labeled "+", "-", "*", "/", "dup",...
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 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....
Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...
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>
{
/** 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 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() = 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...