Hi, please could someone be of assistance with this assignment for me. Thank you
The objective of this assignment is to practice the use of
control objects in GUI design.
You are required to create a 4- function calculator. See pic
below.
Calculator.png
The calculator should have buttons for all 10 numbers, the decimal
point, the four operations (+,-,/,*), an equal “=” button, a "main
display" (TextBox or RichTextBot), a backspace button and a clear
“C” button.
When you create your controls, you need to give them proper names
(e.g. ButtonOne, ButtonResult, ButtonClear,etc)
The only programming in this assignment is
import java.awt.event.*;
import javax.swing.*;
public class CalcGUI extends JFrame implements ActionListener
{
static JFrame frame;
//for result display
static JTextField textField;
String str0, str1, str2;
CalcGUI()
{
str0 = str1 = str2 = "";
}
//driver programme
public static void main(String args[])
{
frame = new
JFrame("CalcGUI");
CalcGUI calc = new CalcGUI();
textField = new
JTextField(16);
textField.setEditable(false);
//for buttons
JButton button0, button1, button2,
button3, button4, button5, button6, button7, button8, button9,
ButtonPlus, ButtonSubstract, ButtonDivition, ButtonMultiplication,
ButtonDot, ButtonClear, buttonEqual;
//intilizing buttons
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonEqual = new
JButton("=");
ButtonPlus = new
JButton("+");
ButtonSubstract = new
JButton("-");
ButtonDivition = new
JButton("/");
ButtonMultiplication = new
JButton("*");
ButtonClear = new
JButton("C");
ButtonDot = new JButton(".");
JPanel panel = new JPanel();
//adding action listeners to the
buttons
ButtonMultiplication.addActionListener(calc);
ButtonDivition.addActionListener(calc);
ButtonSubstract.addActionListener(calc);
ButtonPlus.addActionListener(calc);
button9.addActionListener(calc);
button8.addActionListener(calc);
button7.addActionListener(calc);
button6.addActionListener(calc);
button5.addActionListener(calc);
button4.addActionListener(calc);
button3.addActionListener(calc);
button2.addActionListener(calc);
button1.addActionListener(calc);
button0.addActionListener(calc);
ButtonDot.addActionListener(calc);
ButtonClear.addActionListener(calc);
buttonEqual.addActionListener(calc);
//adding buttons to the panel
panel.add(textField);
panel.add(ButtonPlus);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(ButtonSubstract);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(ButtonMultiplication);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(ButtonDivition);
panel.add(ButtonDot);
panel.add(button0);
panel.add(ButtonClear);
panel.add(buttonEqual);
frame.add(panel);
frame.setSize(200, 220);
frame.show();
}
//this is the function which will perform when user
clicks on it
public void actionPerformed(ActionEvent e)
{
String actionCommand =
e.getActionCommand();
//if user press other than
operators we need to append
if ((actionCommand.charAt(0) >=
'0' && actionCommand.charAt(0) <= '9') ||
actionCommand.charAt(0) == '.') {
if
(!str1.equals(""))
str2 = str2 + actionCommand;
else
str0 = str0 + actionCommand;
textField.setText(str0 + str1 + str2);
}
//this is for clear button
else if (actionCommand.charAt(0) ==
'C') {
str0 = str1 =
str2 = "";
textField.setText(str0 + str1 + str2);
}
//this is the for calculation
part
else if (actionCommand.charAt(0) ==
'=') {
double
result;
if
(str1.equals("+"))
result = (Double.parseDouble(str0) +
Double.parseDouble(str2));
else if
(str1.equals("-"))
result = (Double.parseDouble(str0) -
Double.parseDouble(str2));
else if
(str1.equals("/"))
result = (Double.parseDouble(str0) /
Double.parseDouble(str2));
else
result = (Double.parseDouble(str0) *
Double.parseDouble(str2));
textField.setText(str0 + str1 + str2 + "=" + result);
str0 =
Double.toString(result);
str1 = str2 =
"";
}
else {
if
(str1.equals("") || str2.equals(""))
str1 = actionCommand;
else {
double result;
if (str1.equals("+"))
result =
(Double.parseDouble(str0) + Double.parseDouble(str2));
else if (str1.equals("-"))
result =
(Double.parseDouble(str0) - Double.parseDouble(str2));
else if (str1.equals("/"))
result =
(Double.parseDouble(str0) / Double.parseDouble(str2));
else
result =
(Double.parseDouble(str0) * Double.parseDouble(str2));
str0 = Double.toString(result);
str1 = actionCommand;
str2 = "";
}
textField.setText(str0 + str1 + str2);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.