You will create a calculator that computes addition, substraction, multiplicatio
ID: 3766435 • Letter: Y
Question
You will create a calculator that computes addition, substraction, multiplication, and division as follows.
Specifications:
1. The program should be set to run at 250 and 300 pixels for width and height respectively.
2. Your application should not be resizable.
3. Un-editable textfield.
4. Tab-key enabled.
5. Keyboard filtering.
6. The interface should contain two sections: the first for input and output, the second for a button. 7. It should contain the four fundamental arithmetic operations.
8. On clicking the compute button,
o read two inputs from the textfield
o convert the strings into numeric
o read the selected operator
o compute the value as the operator selection
o change the text for the output label with the result
o limit the precision to be 8 digits.
Explanation / Answer
Answer :
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CalculatorApplication extends Applet
implements ActionListener
{
String msg=" ";
int firstinput,secondinput,result;
TextField buttoninput;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,EQ;
char operator;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
buttoninput=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
clear=new Button("clear");
EQ=new Button("EQ");
buttoninput.addActionListener(this);
add(buttoninput);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
buttoninput.setText(buttoninput.getText()+str);
else
if(str.equals("add"))
{
firstinput=Integer.parseInt(buttoninput.getText());
operator='+';
buttoninput.setText("");
}
else if(str.equals("sub"))
{
firstinput=Integer.parseInt(buttoninput.getText());
operator='-';
buttoninput.setText("");
}
else if(str.equals("mul"))
{
firstinput=Integer.parseInt(buttoninput.getText());
operator='*';
buttoninput.setText("");
}
else if(str.equals("div"))
{
firstinput=Integer.parseInt(buttoninput.getText());
operator='/';
buttoninput.setText("");
}
if(str.equals("EQ"))
{
secondinput=Integer.parseInt(buttoninput.getText());
if(operator=='+')
result=firstinput+secondinput;
else if(operator=='-')
result=firstinput-secondinput;
else if(operator=='*')
result=firstinput*secondinput;
else if(operator=='/')
result=firstinput/secondinput;
buttoninput.setText(""+result);
}
if(str.equals("clear"))
{
buttoninput.setText("");
}
}
}
applet code
<html>
<head></head>
<body>
<applet CODE="CalculatorApplication.class" width="300" height="250">
</applet>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.