[Solved] Last Homework Assignment Course Meant Show Small Complete Gui Application Structured Make Q37293767
This is the last homework assignment in this course, and it ismeant to show you how a small but complete GUI application isstructured. We will make our GUI actually control our memorycalculator. To do this, you will need to create an instance ofMemoryCalc in the CalcGUI and write event Handlers to passinformation back and forth between the
calculator and the GUI. There are many possible ways to achievethis, but the easiest is probably to develop four different eventHandlers:
DigitHandler – This event Handler is added to the 0-9 and . (dot)keys. If the equals button was just pressed, this event Handlerwill overwrite the total shown in the text field with the value ofthe key (its label) that was just pressed. If the equals button wasnot just pressed, this event Handler will append the current key’slabel onto the end of the string shown in the text field.
OperatorHandler – This event Handler is added to the +, -, *, and /keys. It sets the value of a class field to the operator that theuser has chosen.
ClearHandler – This event Handler is added to the C key. It callsthe calculator’s clear method and sets the value of the text fieldback to 0.0.
EqualsHandler – This event Handler is added to the = key. It readsthe current value from the text field, converts it to a double, andcalls the appropriate calculator method based on the currentoperator, passing in the double value. The calculator will computethe answer, and then this event Handler will update the value inthe text field to the calculator’s current total.
That is all that required for the homework assignment, however Ialso encourage you to play around with the different types ofcalculators we have written in this course, perhaps after thiscourse ends. For instance, you could add functionality forgeometric operations like sine and cosine. Or you could create aSuper Calculator that has
different views for scalar, vector, and matrix calculations.Whatever you come up with, I suggest you clean it up and documentit. Potential employers always look favorably on being able toreview code you have written and ask you questions about it.
Hints:
You can use the ActionEvent’s getSource() method to get a referenceto the button that was pressed, and you can use Button’s getText()method to find out the label of that button.
When the EqualsHandler is converting the text shown in the textfield into a double value in order to pass it to the calculator, becareful to handle the case where the text field contains an invalidvalue, such as 6..72. In this case you should catch the exception,display an error message to the user, and abort the call to thecalculator (just restore the current total to the text field). Theuser should only be able to enter numbers between when an operatorhas been selected and when the equals button has been pushed. TheDigitHandler should ignore any button presses when the GUI is notin this state.
I AM HAVING COMPILATION ISSUES:
package guicontrolscalc
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class guicontrolscalc extends JFrame {
private static final long serialVersionUID = 1L;
boolean addS = false;
boolean subtractS = false;
boolean divideS = false;
boolean multiplyS = false;
boolean decimalS = false;
boolean newNumberEntered = false;
MemoryCalculator calc = new MemoryCalculator();
private JButton equals = new JButton(“=”);
private JTextField topText = new JTextField(“0.0”);
private JButton[] buttons = new JButton[12];
private String[] buttonNames = { “1”, “2”, “3”,
“4”, “5”, “6”,
“7”, “8”, “9”,
“0”, “.”, “C”,
};
private JButton plus = new JButton(” + “);
private JButton minus = new JButton(“-“);
private JButton multiply = new JButton(“*”);
private JButton divide = new JButton(“/”);
private String topNumber = “”;
public guicontrolscalc() {
setLayout(new BorderLayout(10, 10));
Color white = new Color(255, 255, 255);
topText.setEditable(false);
topText.setBackground(white);
Font topTextFont = new Font(“monospace”, Font.BOLD, 20);
topText.setFont(topTextFont);
add(topText, BorderLayout.NORTH);
add(equals, BorderLayout.SOUTH);
equals.addActionListener(new EqualsListener());
JPanel pCenter = new JPanel();
pCenter.setLayout(new GridLayout(4, 3, 10, 10));
for (int i = 0; i < 10; i++)
{
buttons[i] = new JButton(buttonNames[i]);
pCenter.add(buttons[i]);
buttons[i].addActionListener(new DigitListener());
}
buttons[10] = new JButton(buttonNames[10]);
pCenter.add(buttons[10]);
buttons[10].addActionListener(new DecimalListener());
buttons[11] = new JButton(buttonNames[11]);
pCenter.add(buttons[11]);
buttons[11].addActionListener(new CancelListener());
add(pCenter, BorderLayout.CENTER);
JPanel pEast = new JPanel();
pEast.setLayout(new GridLayout(4, 1, 10, 10));
pEast.add(plus);
pEast.add(minus);
pEast.add(multiply);
pEast.add(divide);
plus.addActionListener(new OperatorListener());
minus.addActionListener(new OperatorListener());
multiply.addActionListener(new OperatorListener());
divide.addActionListener(new OperatorListener());
add(pEast, BorderLayout.EAST);
}
class DigitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
topNumber += ((AbstractButton) e.getSource()).getText();
topText.setText(topNumber);
newNumberEntered = true;}}
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!addS && !subtractS && !divideS &&!multiplyS) {
calc.setCurrentValue(Double.parseDouble(topText.getText()));
topNumber = “”;
decimalS = false;
newNumberEntered = false;
} else {
if (newNumberEntered == true)
{ equals.doClick();
newNumberEntered = false;}}
if (e.getSource() == plus)
{
addS = true;
subtractS = false; divideS = false; multiplyS = false;
}
else if (e.getSource() == minus){
subtractS = true;
addS = false; divideS = false; multiplyS = false;
}else if (e.getSource() == divide){
divideS = true;
addS = false; subtractS = false; multiplyS = false;}
else if (e.getSource() == multiply){
multiplyS = true;
addS = false; subtractS = false; divideS = false;
}}}
class EqualsListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double answer;
calc.setOp2Value(Double.parseDouble(topText.getText()));
if (addS)
calc.add(calc.getOp2Value());
else if (subtractS)
calc.subtract(calc.getOp2Value());
else if (multiplyS)
calc.multiply(calc.getOp2Value());
else if (divideS)
calc.divide(calc.getOp2Value());
topText.setText(String.valueOf(calc.getCurrentValue()));
decimalS = false;
addS = false;
subtractS = false;
multiplyS = false;
divideS = false;
topNumber = “”;}}
class DecimalListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!decimalS) {
topNumber += “.”;
topText.setText(topNumber);
decimalS = true;}}}
class CancelListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
addS = false;
subtractS = false;
multiplyS = false;
divideS = false;
decimalS = false;
calc.setCurrentValue(0);
topNumber = “”;
topText.setText(topNumber);}}
public static void main(String[] args) {
JFrame guicontrolscalc = new guicontrolscalc();
guicontrolscalc.setTitle(“Calculator”);
guicontrolscalc.setSize(400, 250);
guicontrolscalc.setResizable(false);
guicontrolscalc.setLocationRelativeTo(null);
guicontrolscalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guicontrolscalc.setVisible(true);}
MemoryCalculator.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class MemoryCalculator {
private double currentValue;
private double op2; public void add(double op2) {
currentValue += op2;}
public void subtract(double op2) {
currentValue -= op2;}
public void multiply(double op2) {
currentValue *= op2;}
public void divide(double op2) {
if (op2 == 0) {
currentValue = Double.NaN;
} else {
currentValue /= op2;} }
public double getCurrentValue() {
return currentValue; }
public double getOp2Value() {
return op2;}
public void setCurrentValue(double currentValue) {
this.currentValue = currentValue;}
public void setOp2Value(double op2) {
this.op2 = op2;}}
Expert Answer
Answer to This is the last homework assignment in this course, and it is meant to show you how a small but complete GUI applicatio… . . .
OR

