Question: Here's my current code : // Demonstrates JPanel, GridLayout and a few additional useful graphical features. // adapted from an example by john ramirez (cs

Here's my current code :

// Demonstrates JPanel, GridLayout and a few additional useful graphical features.

// adapted from an example by john ramirez (cs prof univ pgh)

import java.awt.*;

import java.awt.event.*;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.script.ScriptException;

import javax.swing.*;

public class SimpleCalc

{

public static void main(String [] args)

{

new SimpleCalc();

}

JFrame window; // the main window which contains everything

Container content ;

JButton[] digits = new JButton[12];

JButton[] ops = new JButton[4];

JTextField expression;

JButton equals;

JTextField result;

public SimpleCalc()

{

window = new JFrame( "Simple Calc");

content = window.getContentPane();

content.setLayout(new GridLayout(2,1)); // 2 row, 1 col

ButtonListener listener = new ButtonListener();

// top panel holds expression field, equals sign and result field

// [4+3/2-(5/3.5)+3] = [3.456]

JPanel topPanel = new JPanel();

topPanel.setLayout(new GridLayout(1,3)); // 1 row, 3 col

expression = new JTextField();

expression.setFont(new Font("verdana", Font.BOLD, 16));

expression.setText("");

equals = new JButton("=");

equals.setFont(new Font("verdana", Font.BOLD, 20 ));

equals.addActionListener( listener );

result = new JTextField();

result.setFont(new Font("verdana", Font.BOLD, 16));

result.setText("");

topPanel.add(expression);

topPanel.add(equals);

topPanel.add(result);

// bottom panel holds the digit buttons in the left sub panel and the operators in the right sub panel

JPanel bottomPanel = new JPanel();

bottomPanel.setLayout(new GridLayout(1,2)); // 1 row, 2 col

JPanel digitsPanel = new JPanel();

digitsPanel.setLayout(new GridLayout(4,3));

for (int i=0 ; i<10 ; i++ )

{

digits[i] = new JButton( ""+i );

digitsPanel.add( digits[i] );

digits[i].addActionListener( listener );

}

digits[10] = new JButton( "C" );

digitsPanel.add( digits[10] );

digits[10].addActionListener( listener );

digits[11] = new JButton( "CE" );

digitsPanel.add( digits[11] );

digits[11].addActionListener( listener );

JPanel opsPanel = new JPanel();

opsPanel.setLayout(new GridLayout(4,1));

String[] opCodes = { "+", "-", "*", "/" };

for (int i=0 ; i<4 ; i++ )

{

ops[i] = new JButton( opCodes[i] );

opsPanel.add( ops[i] );

ops[i].addActionListener( listener );

}

bottomPanel.add( digitsPanel );

bottomPanel.add( opsPanel );

content.add( topPanel );

content.add( bottomPanel );

window.setSize( 640,480);

window.setVisible( true );

}

// We are again using an inner class here so that we can access

// components from within the listener. Note the different ways

// of getting the int counts into the String of the label

class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

Component whichButton = (Component) e.getSource();

// how to test for which button?

// this is why our widgets are 'global' class members

// so we can refer to them in here

for (int i=0 ; i<10 ; i++ )

if (whichButton == digits[i])

expression.setText( expression.getText() + i );

// need to add tests for other controls that may have been

// click that got us in here. Write code to handle those

// String[] opCodes = {"+"}

for (int i = 0; i<4; i++)

{

if (whichButton == ops[i])

{

expression.setText( expression.getText());

}

}

//Create objects for the classes

ScriptEngineManager manageobj = new ScriptEngineManager();

//Evaluate the expressions

ScriptEngine scriptObj = manageobj.getEngineByName("JavaScript");

//get the expression through the get?Text( method)

String evaluateExp = expression.getText();

// TEST FOR C

if (whichButton == digits[10])

{

expression.setText("");

result.setText("");

}

// Test for CE

if (whichButton == digits[11])

{

expression.setText(expression.getText().substring(0, expression.getText().length()-1));

result.setText("");

}

try

{

//when ever the button equals the get values

//and operator and set the result to the text

if (whichButton == equals)

{

result.setText((scriptObj.eval(evaluateExp)).toString());

}

}

catch (ScriptException exp1)

{

result.setText("INVALID EXPRESSION");

}

}

}

}

It works but my question is how do i make it use a Tokenizer like the one list below

import java.util.*; import java.io.*; public class Tokenize { public static void main( String[] args) { // Stolen directly from stackoverflow with just a few mods :) String expr="4+5-12/3.5-5.4*3.14"; // replace with any expression to test System.out.println( "expr: " + expr ); ArrayList operatorList = new ArrayList(); ArrayList operandList = new ArrayList(); // StringTokenizer is like an infile and calling .hasNext() StringTokenizer st = new StringTokenizer( expr,"+-*/", true ); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("+-/*".contains(token)) operatorList.add(token); else operandList.add( Double.parseDouble( token) ); } System.out.println("Operators:" + operatorList); System.out.println("Operands:" + operandList); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!