Results 1 to 2 of 2
  1. #1
    TommyR is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Java Calculator Help Would Be Greatly Appreciated

    Hi all new to java made a simple calculator for my uni project have now been asked to add a square root function i have tried but the function doesn’t work and now neither does the calculator please help :)

    Copy of my code:

    package calculator;

    //Calculator User Interface

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.lang.Math;


    public class calculatorinterface implements ActionListener {

    JFrame frame = new JFrame("Tom's Calculator");
    JPanel panel = new JPanel(new GridLayout(4,3));
    JPanel panel2 = new JPanel(new GridLayout(4,2));
    JPanel background = new JPanel(new BorderLayout());
    JTextArea text = new JTextArea(1,20);

    JButton but1 = new JButton("1");
    JButton but2 = new JButton("2");
    JButton but3 = new JButton("3");
    JButton but4 = new JButton("4");
    JButton but5 = new JButton("5");
    JButton but6 = new JButton("6");
    JButton but7 = new JButton("7");
    JButton but8 = new JButton("8");
    JButton but9 = new JButton("9");
    JButton but0 = new JButton("0");

    JButton butadd = new JButton("+");
    JButton butsub = new JButton("-");
    JButton butmulti = new JButton("*");
    JButton butdiv = new JButton("/");
    JButton buteq = new JButton("=");
    JButton butclear = new JButton("C");

    // scientific calculator
    JButton butsqrt = new JButton("sqrt");



    Double number1,number2,result;
    int addc=0,subc=0,multic=0,divc=0,sqrt=0;


    public void ui()
    {

    frame.setVisible(true);
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


    frame.add(background);
    background.add("Center",panel);
    background.add("East",panel2);

    background.add("North",text);

    panel.add(but1);
    panel.add(but2);
    panel.add(but3);
    panel.add(but4);
    panel.add(but5);
    panel.add(but6);
    panel.add(but7);
    panel.add(but8);
    panel.add(but9);
    panel.add(but0);
    panel.add(buteq);
    panel.add(butclear);

    panel2.add(butadd);
    panel2.add(butsub);
    panel2.add(butmulti);
    panel2.add(butdiv);
    panel2.add(butsqrt);

    but1.addActionListener(this);
    but2.addActionListener(this);
    but3.addActionListener(this);
    but4.addActionListener(this);
    but5.addActionListener(this);
    but6.addActionListener(this);
    but7.addActionListener(this);
    but8.addActionListener(this);
    but9.addActionListener(this);
    but0.addActionListener(this);
    butadd.addActionListener(this);
    butsub.addActionListener(this);
    butmulti.addActionListener(this);
    butdiv.addActionListener(this);
    buteq.addActionListener(this);
    butclear.addActionListener(this);
    butsqrt.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();
    if(source==butclear)
    {
    number1=0.0;
    number2=0.0;
    text.setText("");

    }



    if(source==but1)
    {
    text.append("1");
    }
    if(source==but2)
    {
    text.append("2");
    }
    if(source==but3)
    {
    text.append("3");
    }
    if(source==but4)
    {
    text.append("4");
    }
    if(source==but5)
    {
    text.append("5");
    }
    if(source==but6)
    {
    text.append("6");
    }
    if(source==but7)
    {
    text.append("7");
    }
    if(source==but8)
    {
    text.append("8");
    }
    if(source==but9)
    {
    text.append("9");
    }
    if(source==but0)
    {
    text.append("0");
    }
    if(source==butadd)
    {
    number1=number_reader();
    text.setText("");
    addc=1;
    subc=0;
    multic=0;
    divc=0;
    sqrt=0;

    }
    if(source==butsub)
    {
    number1=number_reader();
    text.setText("");
    addc=0;
    subc=1;
    multic=0;
    divc=0;
    sqrt=0;

    }
    if(source==butmulti)
    {
    number1=number_reader();
    text.setText("");
    addc=0;
    subc=0;
    multic=1;
    divc=0;
    sqrt=0;

    }
    if(source==butdiv)
    {
    number1=number_reader();
    text.setText("");
    addc=0;
    subc=0;
    multic=0;
    divc=1;
    sqrt=0;

    if (source==butsqrt)
    {
    number1=number_reader();
    addc=0;
    subc=0;
    multic=0;
    divc=0;
    sqrt=1;
    }



    if(source==buteq)
    {

    number2=number_reader();
    if(addc>0)
    {
    result=number1+number2;
    text.setText(Double.toString(result));

    }
    if(subc>0)
    {
    result=number1-number2;
    text.setText(Double.toString(result));

    }
    if(multic>0)
    {
    result=number1*number2;
    text.setText(Double.toString(result));

    }
    if(divc>0)
    {
    result=number1/number2;
    text.setText(Double.toString(result));


    if (sqrt>0)
    result=Math.sqrt(number1);
    text.setText(Double.toString(result));

    }
    }
    }





    }


    public double number_reader()
    {
    Double num1;
    String s;
    s=text.getText();
    num1=Double.valueOf(s);

    return num1;


    }


    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    if (sqrt>0)
    result=Math.sqrt(number1);
    text.setText(Double.toString(result));
    you missed a curly bracket ({) after the If statement, and if you have an extra closing bracket (}) then that would explain why your program stopped working.

Similar Threads

  1. Help with newb-project much appreciated
    By barr in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:14 PM
  2. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 06:33 AM
  3. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM
  4. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •