Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on reverse polish notation calculator assignment in java for school

ID: 3859300 • Letter: I

Question

I am working on reverse polish notation calculator assignment in java for school. Here are my instructions:

Implement a stack class named ArrayStack that "has-a" private array of double.

Implement the methods:

public ArrayClass(int size) - constructor that creates an ArrayClass instance containing an array of the specified size. Remember that in Java, arrays are indexed from 0!

public push(double item) – standard stack action; throws an exception if the array bounds are exceeded.

public double pop() – standard stack action; throws an exception if the array bounds are exceeded.

public boolean isEmpty() – returns true if the stack is empty, and false otherwise.

public double peek(int n) – returns the value of the item located at the specified position on the stack; throws an exception if the array bounds are exceeded or if a nonexistent element is requested; peek(0) will return the top element of the stack.

public int count() - returns the number of items currently pushed onto the stack.

Implement a console application class named TestArrayStack containing a main method that tests each of the above methods of the ArrayStack by creating an instance of ArrayStack and calling each of the methods to test that each functions normally as described and fails with exceptions as described.

When a failure occurs, print a descriptive error message to the console and stop.
Keep adding tests and correcting bugs until all tests pass and you have "covered" all aspects of the specification. When this happens, print out "SUCCESS" and stop.

And

This is what I have so far, which is a very basic stack. My question is how can I change or add the private double instead of the String I am using now?

package source;

public class Stack {

  

   int maxSize;

   int top;

   String arr[];

  

   public Stack(int n)

   {

       maxSize = n;

       arr = new String[maxSize];

       top = 0;

   }

  

   public boolean empty()

   {

       if(top == 0)

       {

           return true;

       }else {

           return false;

       }

   }

  

   public void push(String str)

   {

       if (top < maxSize)

       {

           arr[top] = str;

           top++;

       }

       else{

           System.out.println("Stack overflow!!");

       }

   }

  

   public String pop()

   {

       if(!this.empty())

       {

           String temp = this.peek();

           arr[top-1] = null;

           top--;

           return temp;

       }

       else {

           return null;

       }

   }

  

   public String peek()

   {

       if (top > 0)

       {

           return arr[top-1];

       }else {

           return null;

       }

   }

}

Explanation / Answer

Answer:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int input_stack[5];
int peek;
public:
stack()
{
peek=-1;
}
void push(int x)
{
if(peek > 4)
{
cout <<"stack over flow";
return;
}
input_stack[++peek]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(peek <0)
{
cout <<"stack under flow";
return;
}
cout <<"removed" <<input_stack[peek--];
}
void display()
{
if(peek<0)
{
cout <<" stack is empty";
return;
}
for(int i=peek;i>=0;i--)
cout <<input_stack[i] <<" ";
}
};

main()
{
int value;
stack st;
while(1)
{
cout <<" 1.push 2.pop 3.display 4.exit Enter ur valueoice";
cin >> value;
switvalue(value)
{
case 1: cout <<"enter the element";
cin >> value;
st.push(value);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote