Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

P-6.35 Programming exercises 1) Complete exercise P-6.35 from the text with the

ID: 3587437 • Letter: P

Question

P-6.35

Programming exercises 1) Complete exercise P-6.35 from the text with the following modifications The program must be an application named EvaluatePostfixFromFile The program must use a Scanner object to ask the user for a filename that contains postfix operations in text format If the file does not exist, the program must exit with an error message indicating what went wrong. Note this does not mean that the program crashes due to a thrown IOException, but exits "gracefully" Reads in each full line the file and processes it as a postfix operation (see below) Prints the result of the postfix operation for each line. The output should indicate the line number and the result separately. Processing the file can either be done line by line, or token by token. By default, a Scanner separates each entry by whitespace. Our entries will be separated both by whitespace and by newline characters. It may be easiest to have one Scanner that processes the file line by line, and a separate one that processes each line token by token. You must use a stack to process the expression from left to right. Determining what type of information you'll use to store the data from the file is critical. When we read it back from the stack to process, we'll need to know what type of data it is. I've included a standard algorithm* for processing the data below: for each token in the postfix expression if token is an operator: operand-1 pop from the stack operand-2 pop from the stack result evaluate token with operand-1 and operand-2 push result back onto the stack else if token is an operand: push token onto the stack result pop from the stack * Source: https://en.wikipedia.org/wik. Reverse Polish notation#Postfix evaluation algorithm

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package EvaluatePostfixFromFile;

/**
*
* @author abhayeka
*/
import java.io.File;
import java.util.Scanner;
import java.util.Stack;

public class NewClass {

public static int calculate(String token, int o1, int o2){
if("+".equals(token)){
return o1 + o2;
}
if("-".equals(token)){
return o1 - o2;
}
if("*".equals(token)){
return o1 * o2;
}
return o1 / o2;
}
  
public static void main(String[] args) {
try {
File file = new File("C:\Users\username\Desktop\TestCode\sample.txt");// use your own file path
Scanner scanner = new Scanner(file);
Stack<String> tokens = new Stack<String>();
  
while(scanner.hasNext()){
String token = scanner.next().trim();
  
if("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token)){
  
int operand1 = Integer.valueOf(tokens.pop().trim());
int operand2 = Integer.valueOf(tokens.pop().trim());
tokens.push(Integer.toString(calculate(token, operand1, operand2)));
}else{
tokens.push(token);
}
}
System.out.println(tokens.pop());
  
} catch (Exception e) {
System.out.println(e);
}
}
}