Page 1 of 1

Part 3: (10 points) Implement a class, call it ExprYourname, which will be used for transforming the expressions (infix

Posted: Fri Jul 01, 2022 5:41 am
by answerhappygod
Part 3 10 Points Implement A Class Call It Expryourname Which Will Be Used For Transforming The Expressions Infix 1
Part 3 10 Points Implement A Class Call It Expryourname Which Will Be Used For Transforming The Expressions Infix 1 (87.24 KiB) Viewed 42 times
Part 3 10 Points Implement A Class Call It Expryourname Which Will Be Used For Transforming The Expressions Infix 2
Part 3 10 Points Implement A Class Call It Expryourname Which Will Be Used For Transforming The Expressions Infix 2 (63.28 KiB) Viewed 42 times
i have included MyStackYourname.java
public classMyStackYourname <E> {
public Node<E> head, tail;
public intsize;
//constructor method to create a stack
public voidStack() {
head = null;
tail = null;
size = 0;
}
//method #1: push
public voidpush(E data) {
Node<E> temp =new Node<E>(data);
if (head !=null) {
temp.next = head;
}
head = temp;
size++;
}
//method #2: pop
public E pop() {
if(isEmpty())
return null;
E data = head.data;
head = head.next;
size--;
return data;
}
//method #3: peek
public E peek() {
if(isEmpty())
return null;
return head.data;
}
// method #4: size
public intgetSize(){
return size;
}
// method #5: isEmpty
public booleanisEmpty(){
if(size==0)
return true;
returnfalse;
}
//method to print out the list
public voidprintList() {
Node<E> temp;
temp = head;
while (temp !=null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
//class to create nodes as objects
private classNode<E> {
private E data;//data field
privateNode<E> next; //link field
public Node(E item)//constructor method
{
data = item;
next =null;
}
Part 3: (10 points) Implement a class, call it ExprYourname, which will be used for transforming the expressions (infix to postfix) and evaluate postfix. Write a method public static String infixToPostfix (String infix) { } Use your MyStackYourname to write the procedure of transformation from infix to postfix. Use the algorithm of infix to postfix translation which is introduced in the lecture, translate infix expression to postfix expression. Assumption: // Write appropriate codes here // Return postfix as a returned String of this method Input infix string doesn't have whitespaces. All input number is one-digit number (positive digit 0 to 9) Input infix can include parenthesis () Operator +- */^ can be used The output postfix string doesn't have whitespaces. Part 4: (10 points) In the ExprYourname class, implement another method, call it postfixEval. Using class MyStackYourname, the postfixEval method should evaluate a postfix expression and return the correct calculated result. Write a method public static double postfixEval (String postfix) { // Write appropriate codes here // Return calculated result value Assumption: Input postfix string doesn't have whitespaces. All input number is one-digit number (positive digit 0 to 9) Operator + */^ can be used.
Part 5: (10 points) In the ExprYourname class, write a main method which tests above two methods (Part 3 and Part 4) 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 // Allow the user re-run the program } // Examples Enter an infix: (7+3)*(3-1)/2 73+31-*2/ Postfix Expression: Result value: 10.0 Do you want to continue? (Y/N) Y Enter an infix: Postfix Expression: Result value: (5-6)/4*2-(5+2) 56-4/2*52+- -7.5 Do you want to continue? (Y/N) Y Enter an infix: 3+2^(2+3) Postfix Expression: 3223+^+ Result value: 35.0 Do you want to continue? (Y/N) N //Red characters are user input.