Menu

[Solved]Java Gui Assignment Skeleton Program Gui Written Must Add Functionality Must Hand Single F Q37094432

Java GUI Assignment

Here is a skeleton of a program. The GUI is written you must addthe functionality.

You must hand in a single file named SimpleCalc.java for yourproject-11 submission.

It will not, cannot, be script graded. It will be checked forcompilation success at handin time.

This skeleton program must be enhanced as follows:

  • Your implementation of actionListener will contain your codethat responds to button clicks or text entries on the calculator’ssurface.
  • You must allow entries using either the keys or by typing textinto the expression text field.
  • The CE button must clear the last char of the operand enteredwhile the C button must clear the entire expression textfield
  • The equals button must calculate the result and write theresulting value into the result textfield. You must write theevaluation function. We will discuss this in class. Your evaluationmust evaluate each operator for it’s true precedence. This littlebit of logic will take some work!
  • You must recognized a badly formed expression and put somethinglike “INVALID EXPRESSION” into the result text field.
  • Here is an illustration of how you can extract theoperators/operands from an expression String and store them intotwo ARrayLists named operators and operands respectively:Tokenize.java
  • Once you have an ArrayList of operands and an ArrayList ofoperators you can evaluate the expression with the followingalgorithm:WHILE THERE ARE ANY * OR / OPERATORS REMAINING FIND THE FIRST OCCURANCE OF * OR / AT INDEX INDEX THE OPERANDS WILL BE AT INDEX I AND I+1 PERFORM THE * OR / OPERATION ON THOSE 2 OPERANDS REPLACE THOSE TWO OPERANDS WITH THE RESULT REMOVE THE OPERATOR YOU JUST PEOCESSED FROM THE OPERATORS LISTEND WHILEGO BACK AND DO THE EXACT SAME LOOP AS ABOVE BUT PROCESS + AND – OPERATORSIF YOUR EXPRESSION WAS VALID THEN THE FOLLOWING WILL BE TRUE OTHERWISE YOU HAVE BOGUS EXPRESSION A) YOU WILL BE LEFT WITH AN EMPTY OPERATORS LIST B) THERE WILL BE ONE SOLITARY OPERAND REMAINING IN THE OPERANDS LIST. THAT LAST OPERAND IS THE RESULT OF THE EVALUATIONIF THE ABOVE TWO CONDITIONS ARE NOT TRUE THEN YOUR EXPRESSION WAS BOGUS
  • You are free to use any other algorithm that YOU FIGURED OUTAND WROTE. (shunting yard etc.)
  • DO NOT COPY FROM STACK OVERFLOW.
  • DO NOT USE ANY PRE-WRITTEN METHOD SUCH AS THE JAVASCRIPTEXPRESSION EVAL ENGINE.

// 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.swing.*;public class 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 // if it was the == button click then // result.setText( evaluate() ); } String evaluate() { return “”;/* if ( !isValid( expression.getText() ) return “INVALID”; // WRITE A ISVALID method TOKENIZE using the code I linked on the project-11 page. IT WILL PRODUCE THESE TWO ARARAYLISTS ArrayList<String> operators and ArrayList<Double> operands LOOP #1 FROM LEFT TO RIGHT SCAN THE OPERATORS FOR * or / IF “*” AT INDEX I THEN OVERWRITE OPERANDS[I] WITH OPERANDS[I] * OPERANDS[I+1] REMOVE OPERANDS[I+1] REMOVE OPERATORS[I] ELSE IF “/” DO THE VERY SAME BUT PERFORM / ON I, I+1 LOOP #2 DO IDENTICAL LOOP AS ABOVE BUT PROCCESS + AND – AFTER LOOP2 FINISHES THERE SHOULD BE NO OPERATORS LEFT AND ONLY ONE OPERAND LEFT RETURN “” + OPERAND AT [0]*/ } boolean isValid( ) // dont pass in expr it’s global anyway {// test for no chars other than 0123456789+-*/// no operator at fornt of back of expr// no two ops in a row// no divide by zero return false; } } // END BUTTON LISTNER public static void main(String [] args) { new SimpleCalc(); }}

Tokenize:

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<String> operatorList = new ArrayList<String>(); ArrayList<Double> operandList = new ArrayList<Double>(); // 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); }}

Expert Answer


Answer to Java GUI Assignment Here is a skeleton of a program. The GUI is written you must add the functionality. You must hand in… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *