Having trouble writtng the following program: An expression is in prefix form wh
ID: 3584269 • Letter: H
Question
Having trouble writtng the following program: An expression is in prefix form when operators are written before their operations. Here are some examples of prefix expressions and the values they evaluate to: Expression Value 12 12 + 2 51 53 * 5 7 35 * +16 4 +3 1 80 An expression (such as 12) that begins with an integer is a prefix expression that evaluates to itself. Otherwise, an expression is a prefix expression if it begins with an operator and is followed by two prefix expressions. In this latter case, the value of the expression is recursively computed from the values of its constituent prefix sub-expressions. Write a program that allows the user to enter prefix expression in a text field. The program reads the expression, evaluates it, and displays the value in a suitable GUI component. Assume that the user enters expressions that use only positive integers and the two operators + and *. Your program should use a stack to store values of sub-expression as they are computed. This program should be written using [JAVA]Explanation / Answer
import java.awt.*; abstract public class Node { protected static final int SIZE = 10, SPACING = 25; // Class variable protected static int x; // Abstract method for drawing the proper binary tree abstract public Point draw(Graphics graphics, int depth); // Class method that must be called before drawing is begun public static void initDraw() { x = SPACING; } } // This is thepolynomial from my first botched program, that I should use for this program. Which I know I had some thing wrong in here. XML Code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.lang.Integer; import java.util.*; public class Polynomial implements Comparable { // Used to store the text representation of the polynomial private int[][] polydata; private int length; //private enum POLY_PART { COEFF, EXPONENT }; public Polynomial() { } // (This input doesn't return anything) public Polynomial input(BufferedReader reader) throws Exception public void input(BufferedReader reader) throws Exception { // System.out.println(reader.readLine()); Scanner snapped = new Scanner(reader.readLine()); int max = 0; int temp[]=new int[100], temp2=0; while( snapped.hasNextInt() ) { temp[max]=snapped.nextInt(); max++; } length = max/2; // This will check to see if the pairs or even, if not error will occur? if( max % 2 != 0 || max == 0 ) throw new Exception("Invalid line format!"); // Calculate the number of terms by dividing the pairs, which there are two // pairs in this case. polydata = new int[length][2]; // To be reused as a counter max = 0; // bufferedreader reset for (max=0;max 1 ) if(polydata[i][1]>0) sb.append(polydata[i][0]+"x^"); else sb.append(polydata[i][0]); //poly_part hard coded else if( polydata[i][POLY_PART.EXPONENT] == 1 ) // Print without ^ just x if (polydata[i][1]>0) sb.append(polydata[i][1]+ " "); } return sb.toString(); } public int compareTo(Polynomial omega) { int i=0,j=0; if (this.polydata[0][1] > omega.polydata[0][1]) { return 1; } //Check coefficients else if (polydata== omega.polydata) return 0; else for(i= 0; i omega.polydata[i][1]) { return 1; } else if (polydata[i][1]== omega.polydata[i][1]) { for(j=0;j omega.polydata[i][0]) return 1; } } return -1; } } :confused:Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.