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

    Default Java Calculator Square Root Pleas Help

    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 sorry if this is in the wrong section

    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
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    In your code you have to press the '=' buttton after you've pressed the 'sqrt' button, and even then: check your code in the '/' button handling: the nesting of your tests is incorrect. Your state machine is totally incorrect, sorry for saying. e.g. press '1', '+', '='. Do you like that behaviour?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TommyR is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    thanks for the reply so if i moved it before = would it then work? and i followed a youtube video to make the calculator as im completely new to java so thats why the coding is like tht.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TommyR View Post
    thanks for the reply so if i moved it before = would it then work? and i followed a youtube video to make the calculator as im completely new to java so thats why the coding is like tht.
    No need to apologize for anything; this is a nice project for learning and testing your Java skills. Think about the functionality: pressing the 'sqrt' button is like pressing the '+' button but it should calculate the square root of number1 and display the result. The '=' key has nothing to do with it.

    Why not give it a try and see what happens?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    TommyR is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    thanks i understand in principle wht ur saying about the functionality of the button how when clciked it should work like the plus button however im really not sure wht i need 2 change within my coding please could you tell me were ive gone wrong? ive tried moving a few things with no succes :( kind regads tom

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TommyR View Post
    thanks i understand in principle wht ur saying about the functionality of the button how when clciked it should work like the plus button however im really not sure wht i need 2 change within my coding please could you tell me were ive gone wrong? ive tried moving a few things with no succes :( kind regads tom
    Think of it: when you press the '=' button your calculator should do something; the same with the 'sqrt' button: you calculator should calculate and display the square root of number1. You should remove the code for the square root from the body of the if equal button and move it to somewhere else.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with Square root function and Overload(calc error)
    By racingoutbac in forum New To Java
    Replies: 3
    Last Post: 11-04-2010, 01:09 AM
  2. Find the square root with a particular method
    By roud9 in forum New To Java
    Replies: 2
    Last Post: 09-28-2010, 12:39 AM
  3. square root and prime numbers
    By roud9 in forum New To Java
    Replies: 16
    Last Post: 09-22-2010, 04:20 PM
  4. Simple square root problem!
    By nortski in forum New To Java
    Replies: 7
    Last Post: 04-01-2009, 03:11 PM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 02:21 PM

Posting Permissions

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