Can anyone help me with this java code written in Netbeans?!Something is not working properly ,but I can not understand which part….
import javax.swing.JOptionPane;
public class GuiCalculator
{
private int choice;
public GuiCalculator(int choiceAction)
{
choice=choiceAction;
}
public void setChoice(int choiceAction)
{
choice=choiceAction;
}
public int getChoice()
{
return choice;
}
public void Calculate()
{
GuiCalculator action=new GuiCalculator(-1);
String firstNumber;
String secondNumber;
double num1;
double num2;
double result;
String messagee=String.format("\n\n%-25s\n%-25s%-25s%-25s\n%-24s%-28s%-25s\n%-27s%-29s%-27s\n\n%s","Menu of calculator :","1->Addition","2->Multiplication","3->Division","4->Subtraction","5->Power","6->Sin","7->Cos","8->Log","-1->Exit","Choose an action :");
JOptionPane.showInputDialog(null,messagee);
action.setChoice(Integer.parseInt(messagee));
if (action.getChoice()!=-1)
{
switch(action.getChoice())
{
case 1:
firstNumber=JOptionPane.showInputDialog("Enter the first number :");
secondNumber=JOptionPane.showInputDialog("Enter the second number :");
num1=Double.parseDouble(firstNumber);
num2=Double.parseDouble(secondNumber);
result=num1+num2;
String message=String.format("The addition is %.2f.", result);
JOptionPane.showMessageDialog(null,message);
break;
case 2:
firstNumber=JOptionPane.showInputDialog("Enter the first number :");
secondNumber=JOptionPane.showInputDialog("Enter the second number :");
num1=Double.parseDouble(firstNumber);
num2=Double.parseDouble(secondNumber);
result=num1*num2;
String message1=String.format("The multiplication is %.2f", result);
JOptionPane.showMessageDialog(null,message1);
break;
case 3:
firstNumber=JOptionPane.showInputDialog("Enter the first number :");
secondNumber=JOptionPane.showInputDialog("Enter the second number :");
num1=Double.parseDouble(firstNumber);
num2=Double.parseDouble(secondNumber);
if (num2==0)
{
String message3=String.format("Incorrect operation.");
JOptionPane.showMessageDialog(null,message3);
}
else
{
result=num1/num2;
String message4=String.format("The division is %.2f", result);
JOptionPane.showMessageDialog(null,message4);
}
break;
case 4:
firstNumber=JOptionPane.showInputDialog("Enter the first number :");
secondNumber=JOptionPane.showInputDialog("Enter the second number :");
num1=Double.parseDouble(firstNumber);
num2=Double.parseDouble(secondNumber);
result=num1-num2;
String message5=String.format("The substraction is %.2f", result);
JOptionPane.showMessageDialog(null,message5);
break;
case 5:
firstNumber=JOptionPane.showInputDialog("Enter the first number :");
secondNumber=JOptionPane.showInputDialog("Enter the second number :");
num1=Double.parseDouble(firstNumber);
num2=Double.parseDouble(secondNumber);
String number=JOptionPane.showInputDialog("If you want the first number raised to the power of second number press 1."+"If you want the second number raised to the power of first number press 2.\n");
int yourChoice=Integer.parseInt(number);
if (yourChoice==1)
{
result=Math.pow(num1,num2);
String message6=String.format("The power is %.2f", result);
JOptionPane.showMessageDialog(null,message6);
}
else
{
result=Math.pow(num2,num1);
String message7=String.format("The power is %.2f", result);
JOptionPane.showMessageDialog(null,message7);
}
break;
case 6:
firstNumber=JOptionPane.showInputDialog("Enter the number :");
num1=Double.parseDouble(firstNumber);
result=Math.sin(num1);
String message8=String.format("The sin is %.2f", result);
JOptionPane.showMessageDialog(null,message8);
break;
case 7:
firstNumber=JOptionPane.showInputDialog("Enter the number :");
num1=Double.parseDouble(firstNumber);
result=Math.cos(num1);
String message9=String.format("The cos is %.2f", result);
JOptionPane.showMessageDialog(null,message9);
break;
case 8:
firstNumber=JOptionPane.showInputDialog("Enter the number :");
num1=Double.parseDouble(firstNumber);
if (num1<0)
{
String message11=String.format("Incorrect operation.");
JOptionPane.showMessageDialog(null,message11);
}
else
{
result=Math.log(num1);
String message12=String.format("The natyral logarithm of %.2f is %.2f",firstNumber, result);
JOptionPane.showMessageDialog(null,message12);
}
break;
default:
}
}
}
}
The other file is :
public class GuiCalculatorTest
{
public static void main (String[] args)
{
GuiCalculator myCalculator=new GuiCalculator(0);
myCalculator.Calculate();
}
}
Bookmarks