JAVA programming question using Data Structures

User Generated

ChcclOnex

Programming

CIS 22C

De Anza College

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. 1. Initialize empty stack (of Strings)
  2. 2. For every token in the postfix expression list:
    1. 1. If the token is an operator:
      1. 1. Check if the stack contains the sufficient number of values (usually two) for given operator
      2. 2. If there are not enough values, return a String with "error"
      3. 3. Remove the top 2 items (pop the right operand, then left operand)
      4. 4. Create a string with "("+left+ token+right+")"
      5. 5. Push the string on the stack
    2. 2. If the token is an operand (number), push it on the stack
    3. 3. If the stack contains only one value, return it as a final result of the conversion

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. 1. Initialize empty stack (LinkedStack of Doubles)
  2. 2. For every token in the postfix expression list:
  3. 1. If the token is an operator:
    1. 1. Check if the stack is empty (break from the loop it's in)
    2. 2. Get the value from the top of the stack and store in a variable for the right operand
    3. 3. Pop the top of the stack
    4. 4. Check if the stack is empty (break from the loop it's in)
    5. 5. Get the value from the top of the stack and store in a variable for the left operand
    6. 6. Pop the top of the stack
    7. 7. Evaluate the operator using the left and right operand values and push the single result on the stack (SHOULD USE A SWITCH)
  4. 2. If the token is an operand (number), push it on the stack (converted to a Double) (optional: check
  5. 3. If the stack contains only one value, the top value is assigned to the result and return true
  6. 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

5.0 -7.0 * 4.0 30. / 2 + 10. 20. 30. * 25. 5. / 3. 3. * -54.32 3.5 0.4 + 11.1 22.2 3.33 * - + 100.0 5. / 7.0 3.0 + 2.0 4.0 * - + 123.456 7.8 3 2 * 5 4 * + 9.8 12.3 -7.6 + / 4343.0 0.0 / 1928.3746 78.0 901.0 - 234.0 56.0 + * 33. 444.0 / 9. 3. + * 55. 6. * 77. 1010. / 8.8 7.7 6.6 5.5 - / 4.4 + * 10.0 -9.0 * 8.0 7.0 + -54321.01 Enter the input filename: HW1 Test Input1.txt The postfix expression: 123.456 7.8 The infix equivalent: ( 123.456 - 7.8 ) The result of the evaluation: 115.656 The postfix expression: 3 2 * 5 4 * + The infix equivalent: ( ( 3 * 2 ) + ( 5 * 4 ) ) The result of the evaluation: 26.0 The postfix expression: 9.8 12.3 -7.6 + / The infix equivalent: ( 9.8 / ( 12.3 + -7.6 ) ) The result of the evaluation: 2.085106382978723 The postfix expression: 4343.0 0.0 / The infix equivalent: ( 4343.0 / 0.0 ) The result of the evaluation: Infinity The postfix expression: 1928.3746 The infix equivalent: 1928.3746 The result of the evaluation: 1928.3746 The postfix expression: 78.0 901.0 - 234.0 56.0 + * The infix equivalent: ( ( 78.0 - 901.0 ) * ( 234.0 + 56.0 ) ) The result of the evaluation: -238670.0 Testing the ArrayStack: Pushed A 1, size is now= Pushed B 1, size is now= Pushed C 1, size is now= Pushed D 1, size is now= Pushed E 1, size is now= Pushed F 1, size is now= Pushed G 1, size is now= Pushed H 1, size is now= Pushed I 1, size is now= Pushed J 1, size is now= Trying to push A 2 Trying to push B 2 Trying to push C 2 Trying to push D 2 Trying to push E 2 Trying to push F 2 Out of space, removing E Pushed F 2 Trying to push G 2 Out of space, removing F Pushed G 2 Trying to push H 2 Out of space, removing G Pushed H 2 Trying to push I 2 Out of space, removing H Pushed I 2 Trying to push J 2 1 2 3 4 5 6 7 8 9 10 2 2 2 2 Out of space, removing I 2 Pushed J 2 The size of the ArrayStack is now 15 Popping J 2 Popping D 2 Popping C 2 Popping B 2 Popping A 2 The size of the ArrayStack is now 10 Pushed A 3, size is now= 11 Popping A 3 Popping J 1 Popping I 1 Popping H 1 Popping G 1 Popping F 1 Popping E 1 Popping D 1 Popping C 1 Popping B 1 Popping A 1 The size of the ArrayStack is now 0 Testing the The size of Popping J 4 Popping I 4 Popping H 4 Popping G 4 Popping F 4 The size of Popping E 4 Popping D 4 Popping C 4 Popping B 4 Popping A 4 The size of LinkedStack: the LinkedStack is 10 the LinkedStack is now 5 the LinkedStack is now 0 Enter the input filename: HW1 Test Input2.txt The postfix expression: 33. 444.0 / The infix equivalent: ( 33. / 444.0 ) The result of the evaluation: 0.07432432432432433 9. 3. + *: NOT A VALID POSTFIX EXPRESSION The postfix expression: 55. 6. * 77. The infix equivalent: ( ( 55. * 6. ) - 77. ) The result of the evaluation: 253.0 1010. /: NOT A VALID POSTFIX EXPRESSION The postfix expression: 8.8 7.7 6.6 5.5 - / 4.4 + * The infix equivalent: ( 8.8 * ( ( 7.7 / ( 6.6 - 5.5 ) ) + 4.4 ) ) The result of the evaluation: 100.32000000000002 10.0 -9.0 * 8.0 7.0 +: NOT A VALID POSTFIX EXPRESSION The postfix expression: -54321.01 The infix equivalent: -54321.01 The result of the evaluation: -54321.01 Testing the ArrayStack: Pushed A 1, size is now= Pushed B 1, size is now= Pushed C 1, size is now= Pushed D 1, size is now= Pushed E 1, size is now= Pushed F 1, size is now= Pushed G 1, size is now= Pushed H 1, size is now= Pushed I 1, size is now= Pushed J 1, size is now= Trying to push A 2 Trying to push B 2 Trying to push C 2 Trying to push D 2 Trying to push E 2 Trying to push F 2 Out of space, removing E Pushed F 2 Trying to push G 2 Out of space, removing F Pushed G 2 Trying to push H 2 Out of space, removing G Pushed H 2 Trying to push I 2 Out of space, removing H Pushed I 2 Trying to push J 2 Out of space, removing I Pushed J 2 1 2 3 4 5 6 7 8 9 10 2 2 2 2 2 The size of Popping J 2 Popping D 2 Popping C 2 Popping B 2 Popping A 2 The size of Pushed A 3, Popping A 3 Popping J 1 Popping I 1 Popping H 1 Popping G 1 Popping F 1 Popping E 1 Popping D 1 Popping C 1 Popping B 1 Popping A 1 The size of the ArrayStack is now 15 Testing the The size of Popping J 4 Popping I 4 Popping H 4 Popping G 4 Popping F 4 The size of Popping E 4 Popping D 4 Popping C 4 Popping B 4 Popping A 4 The size of LinkedStack: the LinkedStack is 10 the ArrayStack is now 10 size is now= 11 the ArrayStack is now 0 the LinkedStack is now 5 the LinkedStack is now 0
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

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


Anonymous
Really useful study material!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags