Results 1 to 3 of 3

Thread: Java calculator using GUI-javax.swing

  1. #1
    Join Date
    Dec 2013
    Posts
    2

    Java calculator using GUI-javax.swing

    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();


    }

    }

  2. #2
    Join Date
    Dec 2007
    Posts
    1,736

    Re: Java calculator using GUI-javax.swing

    At the time you are adding or removing or changing a Java GUI, then you will have to call invalidate() or revalidate() to let java re-create the GUI. Check if calling invalidate() before setVisible(true) solves the issue or not.

  3. #3
    Join Date
    Dec 2013
    Posts
    2

    Re: Java calculator using GUI-javax.swing

    Quote Originally Posted by james_911 View Post
    At the time you are adding or removing or changing a Java GUI, then you will have to call invalidate() or revalidate() to let java re-create the GUI. Check if calling invalidate() before setVisible(true) solves the issue or not.
    I have to create the calculator by using only java.swing in this form:
    Develop a program via GUI.
    Use an object-oriented approach to separate the functions of the calculator from the GUI.
    Develop the following:
    Show the menu in a window where the user can insert the choice in a textfield.
    Once the user makes the choice, show windows to read the values from the user.
    Show the result of the operation in a window
    Show again the main menu window

Similar Threads

  1. Print in java using Swing
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 09:50 AM
  2. Java Swing and MySQL
    By shelton141 in forum Software Development
    Replies: 1
    Last Post: 22-09-2009, 08:50 AM
  3. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  4. Message Box example for Java Swing
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 05:30 PM
  5. Java Swing Calculator Tutorial
    By hemanthjava in forum Software Development
    Replies: 0
    Last Post: 15-10-2006, 11:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,065,642.71499 seconds with 18 queries