Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Calculator Square Root Function

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Calculator Square Root Function

    Hi all new to java made a simple calculator for my uni project i 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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Calculator Square Root Function

    Sorry, but saying something "doesn't work" is as useful a question as "then fix it" is a useful answer. What doesn't work about it? Do you see an Exception? Strange behavior? What did you expect to happen? What happens instead?

    Also, when posting code, please use the code or highlight tags. Nobody wants to read through unformatted code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Calculator Square Root Function

    It turns out I was wasting my time, as the OP is already receiving help elsewhere:

    This thread has been cross posted here:

    http://www.java-forums.org/advanced-java/38311-java-calculator-square-root-pleas-help.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Calculator Square Root Function

    im sorry i thought these were differnt forums and whats an OP? and i signed up 2 both as the teahcer recomeneded them for if we got stuck all i wnted was to get my calculator working as before trying to add the square root function it worked fine and now it dosent atall sorry for any troubles caused

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java Calculator Square Root Function

    Quote Originally Posted by laser1992 View Post
    im sorry i thought these were differnt forums and whats an OP? and i signed up 2 both as the teahcer recomeneded them for if we got stuck all i wnted was to get my calculator working as before trying to add the square root function it worked fine and now it dosent atall sorry for any troubles caused
    Don't worry you are obviousally new to this..

    Cross posting can be an issue for a number of reasons. Mainly because if you don't tell us you have posted somewhere else, we will spend our valuable time attempting to help you, only to find out your question has been solved elsewhere.

    Please read: http://www.javaprogrammingforums.com...s-posting.html

    When posting on the forums, it is always best to give as much information about the problem as possible.
    Just like Kevin said, What doesn't work about it? Do you see an Exception? Strange behavior? What did you expect to happen? What happens instead?

    Ultimately this will improve your chances of a quality response.

    We are always here to help

    Oh and OP means original poster.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Calculator Square Root Function

    ah i see your point and well first year of university were learning basic java we were asked to make a calculator so rather than just download the source code to try and gain a better understanding i watched a YouTube tutorial and made a working calculator. then we were asked to add square root button i did some research to try and find out what needed 2 be added the above code is what i came up with however when ran no errors show when compiling but now when run none of the functions such as + or – only typing in numbers works. All i wnated 2 do was add a square root button to my already working calculator i added a new button and tired 2 follow how the other buttons worked to make the square root one work however it didnt
    Last edited by laser1992; February 3rd, 2011 at 10:24 AM.

Similar Threads

  1. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 28th, 2012, 12:42 AM
  2. Square root not working
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 06:25 PM
  3. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  4. Java - URL connection accessing Java Scrpit Function
    By virtual void in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2010, 11:38 AM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM