Question
Hello, Below is the instructions for my assingment, and below that I have posted my code up to the point I got stuck. I am
Hello,
Below is the instructions for my assingment, and below that I have posted my code up to the point I got stuck. I am getting an error in my code at the public double peek (int n) part that says "This method must return a result of type double." I am confused by this and need help fixing my code. Also I am not entirely sure how to test this array. I know I need another class and I must add the same package from my array stack class to it, but other then that I am lost on how to properly test it. If someone could please help I would appreicate it.
Thanks!
Directions -->
Reverse Polish (HP) Style Calculator - Part 1
The purpose of this assignment is to use array as stack to begin constructing a business calculator.
Throughout the remainder of the course, you will complete a series of programming functions that will allow you to develop an emulator for a Hewlett-Packard business calculator that uses the Reverse Polish Notation (RPN) discussed in the Topic Materials.
Reverse Polish Notation is also the basis for a computer programming language for embedded applications called Forth, which you read about in the Topic Materials. As part of this project, you will develop a simplified Forth interpreter.
The heart of a RPN calculator is a data structure called a stack. Review the Topic Materials video tutorials and textbook sections 14.3 and 14.5 and study the examples related to stack prior to completing the programming steps below.
Implement a stack class named ArrayStack that "has-a" privatearray of double.
Implement the methods:
publicArrayClass(int size) - constructor that creates an ArrayClass instance containing an array of the specified size. Remember that in Java, arrays are indexed from 0!
publicpush(double item) standard stack action; throws an exception if the array bounds are exceeded.
publicdouble 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.
publicdouble 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.
publicint 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.
Note: The details of the methods specified above may be somewhat different from the discussion in the text and the videos. You will have to think about adapting the ideas in these sources, as is usual in programming. Available examples are only "good approximations" of what you need to do. You need to add the creative energy to make the adaptations.
Use the debugging features of Eclipse to step though the program to find and correct bugs.
After thoroughly testing the program, submit the ArrayStack.java and TestArrayStack.java files to the instructor.
My Code --->
package array;
class ArrayStack {
private double[] array;
private int size;
private int num;
public ArrayStack(int a){
array = new double[a];
size = a;
num = 0; }
public void push(double a){
if (num < size){
array[num] = a;
num++; }
else {
System.out.println("Stack is full"); } }
public double pop(){
if (num > 0){
num--;
return array[num] }
else {
System.out.println("Stack is empty");
return -1; } }
public boolean isEmpty(){
return (num == 0); }
public double peek(int n){
try {
if (num > 0){
if (n < 0 || n >= num)
return -1;
else
return (int) array[num-1-n]; }
else {
System.out.println("Stack is empty");
return -1; } }
catch(ArrayIndexOutOfBoundsException e ){
e.printStackTrace(); }
} public int count(){
return num; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started