I need help converting the number input from int to double, and add a decimal bu
ID: 3820883 • Letter: I
Question
I need help converting the number input from int to double, and add a decimal button to my code
//GUI Calculator Program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField displayText = new JTextField(30);
private JButton[] button = new JButton[16];
private String[] keys = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"};
private String numStr1 = "";
private String numStr2 = "";
private char op;
private boolean firstInput = true;
public Calculator()
{
setTitle("My Calculator");
setSize(230, 240);
Container pane = getContentPane();
pane.setLayout(null);
displayText.setSize(201, 30);
displayText.setLocation(10, 10);
pane.add(displayText);
int x, y;
x = 10;
y = 40;
for(int ind = 0; ind < 16; ind++)
{
button[ind] = new JButton(keys[ind]);
button[ind].addActionListener(this);
button[ind].setSize(50, 30);
button[ind].setLocation(x, y);
pane.add(button[ind]);
x = x + 50;
if((ind + 1) % 4 == 0)
{
x = 10;
y = y + 30;
}
}
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String resultStr; //Step 1
String str = String.valueOf(e.getActionCommand()); //Steps 1 and 2
char ch = str.charAt(0); //Steps 1 and 3
switch(ch) //Step 4
{
case '0': case '1': case '2': //Step 4a
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9': if(firstInput)
{
numStr1 = numStr1 + ch;
displayText.setText(numStr1);
}
else
{
numStr2 = numStr2 + ch;
displayText.setText(numStr2);
}
break;
case '+': case '-': case '*': //Step 4b
case '/': op = ch;
firstInput = false;
break;
case '=': resultStr = evaluate(); //Step 4c
displayText.setText(resultStr);
numStr1 = resultStr;
numStr2 = "";
firstInput = false;
break;
case 'C': displayText.setText(""); //Step 4c
numStr1 = "";
numStr2 = "";
firstInput = true;
}
}
private String evaluate()
{
final char beep = 'u0007';
try
{
int A = Integer.parseInt(numStr1);
int B = Integer.parseInt(numStr2);
int result = 0;
switch(op)
{
case '+': result = A + B;
break;
case '-': result = A - B;
break;
case '*': result = A * B;
break;
case '/': result = A / B;
}
return String.valueOf(result);
}
catch(ArithmeticException e)
{
System.out.print(beep);
return "E R R O R: " + e.getMessage();
}
catch(NumberFormatException e)
{
System.out.print(beep);
if(numStr1.equals(""))
return "E R R O R: Invalid First Number" ;
else
return "E R R O R: Invalid Second Number" ;
}
catch(Exception e)
{
System.out.print(beep);
return "E R R O R";
}
}
public static void main(String[] args)
{
Calculator C = new Calculator();
}
}
Explanation / Answer
Hi,
I have modified your code to accept double as input instead of int. I have commented some of the section in your code and added new code.
I have changed the actionPerformed method to accept Object 'e' every time the button is clicked instead of maintaining 2 numbers.
CODE :
package com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField displayText = new JTextField(30);
private JButton[] button = new JButton[20];
private String[] keys = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", " ", " ", " "};
// private String numStr1 = "";
// private String numStr2 = "";
// private char op;
// private boolean firstInput = true;
// New Variables added....
static double a=0,b=0,result=0;
static int operator=0;
// ----------------------
public Calculator()
{
setTitle("My Calculator");
setSize(230, 240);
Container pane = getContentPane();
pane.setLayout(null);
displayText.setSize(201, 30);
displayText.setLocation(10, 10);
pane.add(displayText);
int x, y;
x = 10;
y = 40;
for(int ind = 0; ind < 20; ind++)
{
button[ind] = new JButton(keys[ind]);
button[ind].addActionListener(this);
button[ind].setSize(50, 30);
button[ind].setLocation(x, y);
pane.add(button[ind]);
x = x + 50;
if((ind + 1) % 4 == 0)
{
x = 10;
y = y + 30;
}
}
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
// Modified the action performed method to accept the number (double precision..)
if(e.getActionCommand()== "1")
displayText.setText(displayText.getText().concat("1"));
if(e.getActionCommand()== "2")
displayText.setText(displayText.getText().concat("2"));
if(e.getActionCommand()== "3")
displayText.setText(displayText.getText().concat("3"));
if(e.getActionCommand()== "4")
displayText.setText(displayText.getText().concat("4"));
if(e.getActionCommand()== "5")
displayText.setText(displayText.getText().concat("5"));
if(e.getActionCommand()== "6")
displayText.setText(displayText.getText().concat("6"));
if(e.getActionCommand()== "7")
displayText.setText(displayText.getText().concat("7"));
if(e.getActionCommand()== "8")
displayText.setText(displayText.getText().concat("8"));
if(e.getActionCommand()== "9")
displayText.setText(displayText.getText().concat("9"));
if(e.getActionCommand()== "0")
displayText.setText(displayText.getText().concat("0"));
if(e.getActionCommand()== ".")
displayText.setText(displayText.getText().concat("."));
if(e.getActionCommand()=="+")
{
a=Double.parseDouble(displayText.getText());
operator=1;
displayText.setText("");
}
if(e.getActionCommand()=="-")
{
a=Double.parseDouble(displayText.getText());
operator=2;
displayText.setText("");
}
if(e.getActionCommand()=="*")
{
a=Double.parseDouble(displayText.getText());
operator=3;
displayText.setText("");
}
if(e.getActionCommand()=="/")
{
a=Double.parseDouble(displayText.getText());
operator=4;
displayText.setText("");
}
if(e.getActionCommand()=="=")
{
b=Double.parseDouble(displayText.getText());
switch(operator)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
default: result=0;
}
displayText.setText(""+result);
}
if(e.getActionCommand()=="C")
displayText.setText("");
}
/* private String evaluate()
{
final char beep = 'u0007';
try
{
double A = Double.parseDouble(numStr1);
double B = Double.parseDouble(numStr2);
double result = 0;
switch(op)
{
case '+': result = A + B;
break;
case '-': result = A - B;
break;
case '*': result = A * B;
break;
case '/': result = A / B;
}
return String.valueOf(result);
}
catch(ArithmeticException e)
{
System.out.print(beep);
return "E R R O R: " + e.getMessage();
}
catch(NumberFormatException e)
{
System.out.print(beep);
if(numStr1.equals(""))
return "E R R O R: Invalid First Number" ;
else
return "E R R O R: Invalid Second Number" ;
}
catch(Exception e)
{
System.out.print(beep);
return "E R R O R";
}
}*/
public static void main(String[] args)
{
Calculator C = new Calculator();
}
}
***********************************END**************************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.