Question: Part 1: (10 points) Develop a new class , called MyStackYourname , to implement, among others, key stack operations [ push(e), pop(), peek(), size(), isEmpty(),

Part 1: (10 points)

Develop a new class, called MyStackYourname, to implement, among others, key stack operations [push(e), pop(), peek(), size(), isEmpty(), toString()]we discussed in the class. Again, class Stack needs to be defined as generic stack with type so that we can create stack objects that can hold data of different types, such as integer stack, string stack, char stack, double stack, etc.

You can inherits java.util.ArrayList or java.util.LinkedList to implement MyStackYourname.

You can use an java.util.ArrayList or java.util.LinkedList as your classs member for data container.

You can follow my implementation from the class lectures, but please write yourself.

MyStackYourname Class must have these methods

methodname(argument): returnType

isEmpty(): boolean Returns true if this stack is empty

size(): int Returns the number of elements in this stack

peek(): E Returns the top element in this stack

pop(): E Returns and removes the top element in this stack

push(E element): E Adds a new element to the top of this stack

toString(): String Returns the String with all elements in current stack

// based on your choice, toString will return the String with all elements in order [top to bottom] or [bottom to top]

You cannot change method names, return type and parameter types.

To evaluate your Classs methods, the instructor will use another Test Program and will invoke your methods based on the class definition above.

Next, develop a simple test program in a new class called TestStackYourname to test all stack methods(operation) listed above and defined in your class MyStackYourname.

This tester will not be graded if Part2/3/4 shows a successful result.

If Part2/3/4 isnt working correctly, the tester will be graded for extra partial points.

[Part 2&3&4 must be implemented as ONE Class]

Implement a class, call it ExprYourname, which will be used for transform the expressions(infix to postfix) and evaluate postfix.

Part 2: (10 points)

Write a method

public static String infixToPostfix(String infix){

// Write appropriate codes here to transform infix to postfix

// Return postfix as a String

}

Input is a String in infix, and the output must be corresponding postfix expression of the input.

Use your MyStackYourname to write a method for infix to postfix transformation. Use the algorithm of infix to postfix translation which is introduced in the lecture.

Assumptions:

Input infix string doesnt have any whitespaces.

All input number is one-digit number. (a positive digit 0 to 9)

Input infix can include parenthesis ( )

Only five types of operators + - * / ^ can be used.

The output postfix string doesnt have whitespaces.

Part 3: (10 points)

In ExprYourname above, implement another method, call it postfixEval, which prompts the user to enter a string value of postfix expression. Using class StackYourname, the program should evaluate the postfix expression and return the correct calculated result.

Write a method

public static double postfixEval(String postfix){

// Write appropriate codes here

// Return calculated result

}

Assumption:

Input postfix string doesnt have whitespaces.

All input number is one-digit number. (positive digit 0 to 9)

Operator + - * / ^ can be used

If postfix input is not calculatable, show the message (to say its an invalid postfix)

Part 4: (10 points)

Write a main method which tests above two methods (Part 2 and Part 3)

Write a method

public static void main(String[] args){

// What is the process of main method.

// - User will insert infix

// - Invoke infixToPostfix to transform it to postfix expression

// - Invoke postfixEval to evaluate postfix expression

// - Show the calculated result

// Write appropriate codes here

}

// Two examples

Enter an infix: (7+3)*(3-1)/2 //user input infix

Postfix Evaluation: 73+31-*2/

Result value: 10

Enter an infix: (5-6)/4*2-(5+2) //user input infix

Postfix Evaluation: 56-4/2*52+-

Result value: -7.5

//you can ignore how many numbers of digits after decimal point

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!