Question

The objectives of this assignment are to: Further enhance your knowledge and skill in Java. Gain...

The objectives of this assignment are to:

  1. Further enhance your knowledge and skill in Java.
  2. Gain an understanding and experience with stacks.
  3. Gain further experience using generics.
  4. Continue to practice good programming techniques.

Create a stack class named MyStack that stores generics with the methods shown below. Write a test program that thoroughly tests your stack implementation.

void push(E item)

push item onto top of stack

E peek()

return item on top of stack

E pop()

return item on top of stack and remove it from the stack

boolean isEmpty()

returns whether or not stack is empty

Once the stack is working, create a class EvalPostfix that receives a postfix expression in String format, uses the MyStack class to evaluate the expression, and returns the resulting value. You may assume that all operands and operators (+ - * /) will be separated by a single space. Operands will be integers and may contain several digits. The class will require the following functions to be implemented:

EvalPostfix(String post)

constructor that sets the attribute containing the postfix string to be evaluated

int eval()

returns the result of the postfix expression

Write a test program named Prog5 that gets a postfix expression from the keyboard, uses EvalPostfix to evaluate the expression, and displays the result.

Requirements

  • Your source code should abide by the Java programming standards for the course.
  • Your source code should be documented using Javadoc style according to the course standards.
  • Use the default package in your project; do not specify a package in your code.
  • Use any file and class names specified in the assignment.
  • Your test program must be named Prog5.

Preparation for submission:

After you have completed the program, put all of the .java files (Prog5.java, MyStack.java, and EvalPostfix.java) into a zip file named YourLastName5.zip with no subdirectory structure.

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

Done. Please have a look and incase of any doubt leave a comment.

//////////////////////////////////////MyStack.java/////////////////////////////////

public class MyStack<T> {
   private T t[];

   private final static int DEFAULT_SIZE = 10;
   private int top;

   // default constructor sets the array to default capacity
   public MyStack() {
       this(DEFAULT_SIZE);
   }

   // constructor with specified size sets the size of the array to the given size
   public MyStack(int size) {
       t = (T[]) new Object[size];
       top = -1;
   }

   /**
   *
   * @return true if stack is empty otherwise false
   *
   */
   public boolean isEmpty() {
       return (top == -1);
   }

   /**
   * The method checks if the stack is not empty and then gets the value.
   *
   * @return return the top item and removes it from stack (if not empty)
   */
   public T pop() {
       if (top == -1)
           return null;
       return t[top--];
   }

   /**
   * Adds the item to the top of the stack
   *
   * @param elem
   */
   public void push(T elem) {
       t[++top] = elem;
   }

   /**
   *
   *
   * @return returns the total number of elements in the stack
   */
   public int size() {
       return (top + 1);
   }
}

/////////////////////////////////////ENd MyStack.java////////////////////////////////////////////

/////////////////////////////////////EvalPostfix.java///////////////////////////////////////////////

import java.util.Scanner;

//Class which is responsible for evaluating postFIx expression by putting elements on stack one by one
public class EvalPostfix {
   // string to evaluate
   private String str;

   // Constructor that intialise the string
   public EvalPostfix(String s) {
       this.str = s;
       int ans = evalPf();
       System.out.println("The result after evaluatins: " + s + " is = " + ans);
   }

   /**
   *
   * @return
   */
   public int evalPf() {
       // create new stack
       MyStack<Integer> myStack = new MyStack<Integer>();

       Scanner scanner = new Scanner(this.str);
       // scanner reads number by number seperated by white space to put the numbers in
       // to the stack
       while (scanner.hasNext()) {
           if (scanner.hasNextInt()) {
               myStack.push(scanner.nextInt());
               continue;
           }
           // take out first two values from the stack
           int operand1 = myStack.pop();
           int operand2 = myStack.pop();

           char op = scanner.next().charAt(0);
           // push the result it into the stack
           if (op == '+')
               myStack.push(operand2 + operand1);
           else if (op == '-')
               myStack.push(operand2 - operand1);
           else if (op == '*')
               myStack.push(operand2 * operand1);
           else if (op == '/')
               myStack.push(operand2 / operand1);
           else if (op == '%')
               myStack.push(operand2 % operand1);
       }

       scanner.close();
       return myStack.pop();
   }
}

/////////////////////////////////////End EvalPostfix.java///////////////////////////////////////////////

//////////////////////////////////////////Prog5.java///////////////////////////////////////////////////////

import java.util.Scanner;

//This is the program to test stacks and EvalPostfix program
public class Prog5 {
   public static void main(String args[]) {
       char c = 'y';
       Scanner sc = new Scanner(System.in);
       do {
           System.out.println("Please enter a experssion to Evaulate");
           // get user input
           String str = sc.nextLine();
           //call construtor
           EvalPostfix eval = new EvalPostfix(str);
           System.out.println("Do you want to evaluate more expressions");
           c = sc.nextLine().charAt(0);
       } while (c == 'y' || c == 'Y');
       sc.close();
   }
}

//////////////////////////////////////////End Prog5.java///////////////////////////////////////////////////////

Output Screenshot

Add a comment
Know the answer?
Add Answer to:
The objectives of this assignment are to: Further enhance your knowledge and skill in Java. Gain...
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
  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • 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....

  • I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....

    I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...

  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I...

    (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix. Here's the postFix class and its stack class (https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class (https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class Your calculator should have: Text field for the postfix expression Text field for the result Two buttons: Evaluate and Clear You can start with the code written below and improvise to work with this...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

    C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....

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