Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help to reconstruct my code to compete this task, its acode for simple java GUI calculator , cheers here is the code: /* CalculatorPanel.java

need help to reconstruct my code to compete this task, its acode for simple java GUI calculator, cheers

imageimage

here is the code:

/* CalculatorPanel.java */

import javax.swing.*;import java.awt.*;import java.awt.event.*;

/*** The calculator panel* @author*/public class CalculatorPanel extends JPanel {private static final long serialVersionUID = 1L;private static final int CALC_WIDTH=250;private static final int CALC_HEIGHT=325;

public CalculatorPanel() {setLayout(new BorderLayout());setBackground(Color.lightGray); // create the input/outputpanelresult = 0; // result variable of labellastCommand = "=";start = true;

// add the displayresultLabel = new JLabel("0", SwingConstants.RIGHT); // set theJLabel to RIGHTresultLabel.setFont(new Font("Helvetica", Font.BOLD, 40)); // setthe font sizeadd(resultLabel, BorderLayout.NORTH); // set the border layout toNORTH// Action listener createsActionListener insert = new InsertAction();ActionListener command = new CommandAction();

// CREATE A LAYOUT OF THE BUTTONS 4X4 gridpanel = new JPanel();panel.setLayout(new GridLayout(4, 4)); //set the 4x4 gridpanel.setBackground(Color.DARK_GRAY); //set the backgroundcolor

// add the button text and assign the actionaddButton("7", insert);addButton("8", insert);addButton("9", insert);addButton("/", command);addButton("4", insert);addButton("5", insert);addButton("6", insert);addButton("*", command);addButton("1", insert);addButton("2", insert);addButton("3", insert);addButton("-", command);

addButton("0", insert);addButton(".", insert);addButton("=", command);addButton("+", command);JButton clear = new JButton("Clear");clear.setFont(new Font("Helvetica", Font.BOLD, 15)); // set thefont sizeclear.setForeground(Color.BLUE); //set the font color to blueclear.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {resultLabel.setText("0");}});add(clear,BorderLayout.SOUTH);add(panel, BorderLayout.CENTER); // set the panel to center}// add buttonprivate void addButton(String label, ActionListener listener){JButton button = new JButton(label);button.setPreferredSize(new Dimension(55,30)); //set the buttonsizebutton.setFont(new Font("Helvetica", Font.BOLD, 15)); // set thefont sizebutton.setForeground(Color.BLUE); //set the font color tobluebutton.addActionListener(listener); // add the actionlistenerpanel.add(button); // add the button to the panel}

private class InsertAction implements ActionListener {public void actionPerformed(ActionEvent event) {String input = event.getActionCommand();if (start) {resultLabel.setText("");start = false;} if(!resultLabel.getText().equals("0"))resultLabel.setText(resultLabel.getText() + input); //display thetext to the label when no 0else{resultLabel.setText( input); //display the text to the label when0}}}

private class CommandAction implements ActionListener{public void actionPerformed(ActionEvent event) {String command = event.getActionCommand();

if (start) {

if (command.equals("-")) {resultLabel.setText(command);start = false;} elselastCommand = command;} else {calculate(Double.parseDouble(resultLabel.getText()));lastCommand = command;start = true;}}}

// method that calculate the result of two inputspublic void calculate(double x) {if (lastCommand.equals("+"))result += x; // addelse if (lastCommand.equals("-"))result -= x; // subtractelse if (lastCommand.equals("*"))result *= x; // multiplyelse if (lastCommand.equals("/"))result /= x; // divideelse if (lastCommand.equals("="))result = x; // equalsresultLabel.setText("" + result); // display the result to thelabel}

//variablesprivate JLabel resultLabel;private JPanel panel;private double result;private String lastCommand;private boolean start;

}

/* CalculatorDriver.java*/

import javax.swing.JFrame;import javax.swing.*;import java.awt.event.*;/** Driver Class for the Calcultor** @author*/public class CalculatorDriver {public static void main(String[] args) {// create JFrame objectJFrame frame=new JFrame("Simple Calculator");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// create object of CalculatorPanel classCalculatorPanel panel=new CalculatorPanel();frame.getContentPane().add(panel); // add panel into frameframe.pack();frame.setResizable(false); // frame should not be resizableframe.setLocation(400,300); // set location of frameframe.setVisible(true); // make frame visible}}

For this task you need to restructure your code to make use of arrays to represent the buttons as well as implement the memory functionality for the calculator. 1. You should declare two 10 item arrays of type JButton: one should be for numbers and the other should be for operators. You should create another array of Strings ops for the symbols of your operator buttons: "*"! "+" 9 "="1 "C", 11_11 "mc". "m+" " " 9 2 'm-"1 2 "mr These arrays should be usable throughout your CalculatorPanel class. 2. Within your constructor define a for loop that will cycle through the ten number buttons and create each button, associate a listener to each button and set the button's preferred size. 3. Do the same for the operator buttons however for each iteration of the loop you should retrieve the next element from the ops array to label the JButtons. Note as well that the 5th element of the operators buttons should have a red label while all the others should be blue. 4. In the actionPerformed method in the ButtonListener class you will need to retrieve the label from the JButtons to determine what action to perform. To retrieve the label you can retrieve one of the properties of the ActionEvent object. By using the getAction Command method you can retrieve the label as a String. Once you have that string you need to retrieve the first character and by using a switch determine what action to perform. 5. Something to consider: How will you differentiate each of the memory buttons?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To restructure your code as described in the task follow these steps 1 Declare the arrays for numbers and operators Add the memory buttons to the operators array as well 2 In the constructor of Calcul... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Explain factors governing recruitment?

Answered: 1 week ago