Trying Finish Prefix Calculator Programming Challenge Write Program Allows User Enter Pref Q25404474

I am trying to finish up a prefix calculator programmingchallenge. Write a program that allows the user to enter prefixexpressions in a textField. The program reads the expression,evaluates it, and displays the value in a suitable GUI component.Asssume that the user enter expressions that use only positiveintegers and the two operators + and *. Your program shouldinternally convert the prefix expression into a binary tree beforeevaluating it. this is what I have so far:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.*;

import javax.swing.*;


* This program demonstrates a Prefix Calculator.

* It will allow the user to enter a prefix expression

* using only positive integers and + and * operators.

* This program will display a GUI component for the

* user to enter their prefix expression, evaluate it,

* and recieve its value.


public class PrefixCalc extends JFrame


// Create text fields to hold

// the expression of the user’s input.

private JTextField inputTextField;

private JTextField resultTextField;

private class Node


String value;

Node left, right;

Node(String val, Node l, Node r)


value = val;

left = l;

right = r;



private class CmdTextListener implements ActionListener


public void actionPerformed(ActionEvent evt)


String prefixExpr = inputTextField.getText();

Node exprTree = buildExpr(new Scanner(prefixExpr));

int value = eval(exprTree); // Produces the value of the

// expression.

// Display the result.



private Node buildExpr(Scanner scanner)


String str =;

if (isNumber(str))

return new Node(str, null, null);



// str is an operator, so build a non-leaf tree.

Node leftOp = buildExpr(scanner);

Node rightOp = buildExpr(scanner);

return new Node(str, leftOp, rightOp);



private boolean isNumber(String str) {

// TODO Auto-generated method stub

return false;



* The eval method is a recursive method

* processes a binary tree to get a value.

* @param tree the value of the binary tree

* @return


private int eval(Node tree)


// If the expression is evaluated that

// contains a single leaf node, the string

// value is converted and then the resulting

// number is returned.

if (tree.left == null && tree.right

== null)

// This return makes sure the string

// in the Node is a number.

return Integer.parseInt(tree.value);

// Otherwise, the left and right subexpressions

// are evaluated, and then their values are

// either multiplied or added; this depends on

// the operator was in addition or multiplication.



int leftOp = eval(tree.left);

int rightOp = eval(tree.right);

if (tree.value.equals(“*”))

return leftOp * rightOp;

if (tree.value.equals(“+”))

return leftOp + rightOp;


return 0; // Will never be reached.



public static void main(String[] args)


new PrefixCalc();



"We Offer Paper Writing Services on all Disciplines, Make an Order Now and we will be Glad to Help"
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.