Write a Java program to read and simulate the execution of a program written in a highly simplified programming language. Each line in the input contains one statement. There are no declaration statements; all values are of type double. Statements in the language are in the following forms: Simple Statements Meaning variable = expression The expression consists of variables, constants, and the simple arithmetic operators (+, -, *, /) with blanks as delimiters. It is evaluated left to right and the result is placed in the variable. Note that there is no hierarchy among the operators and parentheses are not allowed. PRINT variable The value of the variable should be displayed with two digits to the right of the decimal point. GOTO n Execution moves to the nth line of the program. The first line in the program is line 1. Conditional Statement Meaning IF variable IS value THEN simpleStatement If the variable is equal to value (a variable or a constant), the simpleStatement is performed. End of Program Marker Meaning END This is the last statement in a program. There are no statements following this. When execution reaches this statement, the program is terminated. The GUI will include a “Next Program” button which allows the user to select a file containing a program to be executed. The program in the selected file is then executed and the output (result of the PRINT statements) should be displayed in a scrollable text area. NOTE Use of an array is NOT acceptable in this assignment. As in Lab 1, you will be given code for the GUI. Also, error checking is required
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Lab2 extends JFrame implements ActionListener {
JButton open = new JButton("Next Program");
JTextArea result = new JTextArea(20,40);
JLabel errors = new JLabel();
JScrollPane scroller = new JScrollPane();
public Lab2() {
setLayout(new java.awt.FlowLayout());
setSize(500,430);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
add(open); open.addActionListener(this);
scroller.getViewport().add(result);
add(scroller);
add(errors);
}
public void actionPerformed(ActionEvent evt) {
result.setText("");
//clear TextArea for next program
errors.setText("");
processProgram();
}
public static void main(String[] args) {
Lab2 display = new Lab2();
display.setVisible(true);
}
String getFileName() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
return fc.getSelectedFile().getPath();
else
return null;
}
/************************************************************************/
/* Put your implementation of the processProgram method here. */
/* Use the getFileName method to allow the user to select a program. */
/* Then simulate the execution of that program. */
/* You may add any other methods that you think are appropriate. */
/* However, you should not change anything in the code that I have */
/* written. */
/************************************************************************/
}