Description
HW1 Java Code File:
Use the following files from the Stack Code Files folder as well as the code below:
- • StackInterface.java
- • ArrayStack.java (but with the methods given in the Answer to Lab Exercise 2.1, AND as specified on the assignment)
- • LinkedStack.java (but the FIXED version as shown in the Answer to Lab Exercise 2.2)
// YOU WRITE THE WHOLE PostfixExpression.java class as described on the assignment
// Use this method for tokenizing the postfix expression String
// (remember, it's a private instance method in the PostfixExpression class):
// tokens is the name of my instance ArrayList of Strings variable
private void tokenize()
{
String [] tokenArray = wholeExpr.split("[ \t]+");
tokens.clear(); // clear the ArrayList
for(int i=0; i < tokenArray.length; ++i)
{
tokens.add(tokenArray[i]); // add the next token to the ArrayList
}
} // end tokenize
ALGORITHM FOR CONVERTING A POSTFIX EXPRESSION TO AN INFIX EXPRESSION (IF IMPLEMENTED CORRECTLY, THIS WILL NOT ALLOW TOO MANY OPERANDS OR TOO MANY OPERATORS):
- 1. Initialize empty stack (of Strings)
- 2. For every token in the postfix expression list:
- 1. If the token is an operator:
- 1. Check if the stack contains the sufficient number of values (usually two) for given operator
- 2. If there are not enough values, return a String with "error"
- 3. Remove the top 2 items (pop the right operand, then left operand)
- 4. Create a string with "("+left+ token+right+")"
- 5. Push the string on the stack
- 2. If the token is an operand (number), push it on the stack
- 3. If the stack contains only one value, return it as a final result of the conversion
- 1. If the token is an operator:
Otherwise, return a String with "error"
ALGORITHM FOR EVALUATION THE POSTFIX EXPRESSION (this method returns a boolean), GIVEN A LIST OF Strings (IF IMPLEMENTED CORRECTLY, THIS WILL NOT ALLOW TOO MANY OPERANDS OR TOO MANY OPERATORS):
- 1. Initialize empty stack (LinkedStack of Doubles)
- 2. For every token in the postfix expression list:
- 1. If the token is an operator:
- 1. Check if the stack is empty (break from the loop it's in)
- 2. Get the value from the top of the stack and store in a variable for the right operand
- 3. Pop the top of the stack
- 4. Check if the stack is empty (break from the loop it's in)
- 5. Get the value from the top of the stack and store in a variable for the left operand
- 6. Pop the top of the stack
- 7. Evaluate the operator using the left and right operand values and push the single result on the stack (SHOULD USE A SWITCH)
- 2. If the token is an operand (number), push it on the stack (converted to a Double) (optional: check
- 3. If the stack contains only one value, the top value is assigned to the result and return true
- 4. Otherwise, assign Double.NaN to the result and return false
//-----------------------------------------------------------------------------
// WRITE MAIN as described on the assignment and using the following method & variable:
//USE THE FOLLOWING IN YOUR MAIN FILE for opening the input file:
public static Scanner userScanner = new Scanner(System.in);
public static Scanner openInputFile()
{
String filename;
Scanner scanner=null;
System.out.print("Enter the input filename: ");
filename = userScanner.nextLine();
File file= new File(filename);
try{
scanner = new Scanner(file);
}// end try
catch(FileNotFoundException fe){
System.out.println("Can't open input file\n");
return null; // array of 0 elements
} // end catch
return scanner;
} // end openInputFile
// call this method as indicated in HW#1, and don't change the method NOR
//the size of your ArrayStack:
public static void stackTester()
{
ArrayStack<String> arrStack = new ArrayStack<>();
LinkedStack<String> linkStack = new LinkedStack<>();
String [] strArray = {"A","B","C","D","E","F","G","H","I","J"};
String temp;
// Testing ArrayStack
System.out.println("\nTesting the ArrayStack:");
for( int i=0; i < strArray.length; ++i )
{
temp = strArray[i]+" 1";
if(arrStack.push(temp))
System.out.println("Pushed "+temp+", size is now= "+arrStack.size());
else
System.out.println("Error: couldn't push "+ temp+", size is now= "
+arrStack.size());
}
for( int i=0; i < strArray.length; ++i )
{
temp = strArray[i]+" 2";
System.out.println("Trying to push "+temp);
if(!arrStack.push(temp))
{
System.out.println("Out of space, removing " +
arrStack.pop());
if(arrStack.push(temp))
System.out.println("Pushed " + temp);
else
System.out.println("Error pushing "+temp);
}
}
System.out.println("The size of the ArrayStack is now " + arrStack.size());
for( int i=0; i < strArray.length/2; ++i )
{
System.out.println("Popping "+ arrStack.pop());
}
System.out.println("The size of the ArrayStack is now " + arrStack.size());
temp = strArray[0] + " 3";
if(arrStack.push(temp))
System.out.println("Pushed "+temp+", size is now= "+arrStack.size());
else
System.out.println("Error: couldn't push "+ temp+", size is now= "
+arrStack.size());
while( !arrStack.isEmpty() )
{
System.out.println("Popping "+ arrStack.pop());
}
System.out.println("The size of the ArrayStack is now " + arrStack.size());
// testing LinkedStack
System.out.println("\nTesting the LinkedStack:");
for( int i=0; i < strArray.length; ++i )
linkStack.push(strArray[i] + " 4");
System.out.println("The size of the LinkedStack is " + linkStack.size());
for( int i=0; i < strArray.length/2; ++i )
{
System.out.println("Popping " + linkStack.pop());
}
System.out.println("The size of the LinkedStack is now " + linkStack.size());
while( !linkStack.isEmpty() )
{
System.out.println("Popping "+ linkStack.pop());
}
System.out.println("The size of the LinkedStack is now " + linkStack.size());
} // end stackTester
Unformatted Attachment Preview
Purchase answer to see full attachment
Explanation & Answer
Hey, here is the code and Snapshots.. It matches the Expected output 😃 Though there are some ambiguities in the question, such the "efficient" thing and also the conflict between these two statements:If the program gets closed when it's unable to open input file. Which means, stackTester can only run after if the file exists.
import java.util.ArrayList;
public class PostfixExpression {
//String for the whole postfix expression
private String wholeExpr;
//ArrayList of Strings for the tokenized postfix expression
private ArrayList tokens;
//double (for the result of the expression)
private double result;
/*
* default constructor (assigns an empty String to the String instance variable
* Double.NaN to the double instance variable,
* and a new ArrayList to the instance ArrayList variable)
*/
public PostfixExpression() {
wholeExpr = "";
tokens = new ArrayList();
result = Double.NaN;
}
/*
* constructor with a String parameter: call the default constructor,
* then call the mutator for the String instance variable
*/
public PostfixExpression(String exp) {
this();
setExpression(exp);
}
/*
* mutator for the String instance variable (whole expression),
* which not only assigns the parameter to the String instance variable,
* but also calls the private tokenize method, then return (in a return statement)
* the return value of the evaluate method.
*/
public boolean setExpression(String exp) {
wholeExpr = exp;
tokenize();
return evaluate();
}
public String getExpression() {
return wholeExpr;
}
public double getResult() {
return result;
}
//ALGORITHM FOR EVALUATION THE POSTFIX EXPRESSION
private boolean evaluate() {
//Initialize empty stack (LinkedStack of Doubles)
LinkedStack stack = new LinkedStack();
//For every token in the postfix expression list:
for (String token : tokens) {
// If the token is an operator
if (isOperator(token)) {
//Check if the stack is empty (break from the loop it's in)
if (stack.isEmpty()) {
break;
}
//Get the value from the top of the stack and store in a variable for the
right operand
//Pop the top of the stack
double rightOp = stack.pop();
//Check if the stack is empty (break from the loop it's in)
if (sta...