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 5 of 5

Thread: Calculator Code, Need Help Please

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator Code, Need Help Please

     import java.awt.*;
     
    import java.applet.*;
     
    import javax.swing.*;
     
    import java.awt.event.*;
     
    public class Calc7 extends JApplet implements ActionListener
     
    {
     
      // ---------------------------------------------------------
     
      // Attributes:
     
      //    TextArea total displays running total of calculations
     
      //    Label output shows current number being entered
     
      //    doubles memVal and curVal are used for calculations
     
      // ---------------------------------------------------------
     
      JTextArea total  = new JTextArea(", 5, 25);
     
      JScrollPane scrollingArea = new JScrollPane(total);
     
      JLabel output;
     
      double  memVal    = 0;
     
      double  curVal    = 0;
     
      Container content = this.getContentPane();
     
     
     
      // ---------------------------------------------------------
     
      // The init() method sets the layout and color.
     
      // It then calls the makeButtons() to do the actual layout
     
      // for the calculator
     
      // ---------------------------------------------------------
     
      public void init()
     
      {
     
        content.setLayout(new BorderLayout(5,5));
     
        content.setBackground(Color.lightGray);
     
     
     
        content.add("Center", makeButtons()); }
     
      }
     
     
     
      public boolean actionPerformed(ActionEvent ae)
     
      {
     
        // Local variables
     
        JButton btn = (JButton) ae.getSource();
     
    	String arg = btn.getText(); // Key that was pressed
     
        char   c    = arg.charAt(0);      // First char of key caption
     
        String s    = output.getText();   // Current value of output
     
     
     
        // "Special" keys operate on current value
     
        if (arg.equals("Backspace"))    backSpace();
     
        else if (arg.equals("C"))       clearAll();
     
        else if (arg.equals("CE"))      clearEntry();
     
        else if (arg.equals("sqrt"))    setCurVal(Math.sqrt(curVal));
     
        else if (arg.equals("1/x"))     setCurVal(1.0/curVal);
     
        else if (arg.equals("+/-"))     setCurVal(-curVal);
     
     
     
        // Digit keys are always added to current value
     
        else if (c >= '0' && c <= '9')  setCurVal(s + c);
     
     
     
        // Decimal point added only if not already in output
     
        else if (c == '.')
     
        {
     
          if (s.indexOf(c) < 0) setCurVal(s + c);
     
        }
     
     
     
        // Handle all of the operator keys
     
        else
     
        {
     
          switch (c)
     
          {
     
            case '/':
     
              memVal /= curVal;
     
              break;
     
            case '*':
     
              memVal *= curVal;
     
              break;
     
            case '-':
     
              memVal -= curVal;
     
              break;
     
            case '+':
     
              memVal += curVal;
     
              break;
     
            case '%':
     
              memVal %= curVal;
     
              break;
     
            case '=':
     
              memVal = curVal;
     
              total.append(padText("=============\n",25));
     
              break;
     
          }
     
     
     
          // Display results on the TextArea named total
     
          String memstr  = "" + curVal + " " + c + " \n";
     
          total.append(padText(memstr,25));
     
     
     
          String memstr  = "" + memVal + "   \n";
     
          total.append(padText(memstr,25));
     
     
     
          // Clear the output Label
     
          curVal = 0;
     
          output.setText("");
     
        }
     
        return true;
     
      }
     
     
     
      // ----------------------------------------------------------
     
      // These are utility methods
     
      // ----------------------------------------------------------
     
      Sting padText(String s, int size)
     
      {
     
        String temp = "                                       " + s;
     
        return temp.substring(temp.length() - size);
     
      }
     
     
     
      // Handles the backspace key
     
      void backSpace()
     
      {
     
        s = s.substring(0, s.length()-1);
     
        setCurVal(s);
     
      }
     
     
     
      // Handles the 'C' [Clear All] key
     
      void clearAll()
     
      {
     
        total.setText("");
     
        output.setText("");
     
        curVal = memVal = 0;
     
      }
     
     
     
      // Handles the 'CE' [Clear Entry] key
     
      void clearEntry()
     
      {
     
        output.setText("");
     
        curkVal = 0;
     
      }
     
     
     
      // Sets the current value, using a String
     
      void setCurVal(String s)
     
      {
     
        output.setText(s);
     
        curVal = (new Double(s)).doubleValue();
     
      }
     
     
     
      // Sets the current value, using a number
     
      void setCurVal(double newValue)
     
      {
     
        curVal = nwValue;
     
        output.setText( newValue);
     
      }
     
     
     
      // ---------------------------------------------------------
     
      // This lays out the appearance of the applet, using
     
      // BorderLayout and GridLayout
     
      // The colors used for the buttons may, or may not, appear
     
      // on your copy, depending upon your Web browser
     
      // ---------------------------------------------------------
     
      JPanel makeButtons()
     
      {
     
        // Create 4 JPanels [p1 through p4]
     
        JPanel p   = new JPanel(new BorderLayout(5,5));
     
        JPanel p1  = new JPanel(new BorderLayout(5,5));
     
        JPanel p2  = new JPanel(new BorderLayout(5,5));
     
        JPanel p3  = new JPanel(new GridLayout(1,3,5,5));
     
        JPanel p4  = new JPanel(new GridLayout(4,5,5,5));
     
        JPanel p5  = new JPanel(new BorderLayout(5,5));
     
     
     
        //  Add Backspace, CE, and C buttons to p3
        JButton Backspace = new JButton("Backspace");
        JButton CE = new JButton("CE");
        JButton Clear = new JButton("C");
     
        Backspace.addActionListener(this);
        CE.addActionListener(this);
        Clear.addActionListener(this);
     
        p3.setForeground(new Color(232,0,0));
     
        p3.setFont(new Font("Dialog", Font.PLAIN, 12));
     
        p3.add(Backspace);
     
        p3.add(CE);
     
        p3.add(Clear);
     
     
     
        // Add Number buttons to p4
     
        JButton zero = new JButton("0");
        JButton one = new JButton("1");
        JButton two = new JButton("2");
        JButton three = new JButton("3");
        JButton four = new JButton("4");
        JButton five = new JButton("5");
        JButton six = new JButton("6");
        JButton seven = new JButton("7");
        JButton eight = new JButton("8");
        JButton nine = new JButton("9");
        JButton addition = new JButton("+");
        JButton subtract = new JButton("-");
        JButton multiply = new JButton("*");
        JButton divide = new JButton("/");
        JButton sqrt = new JButton("sqrt");
        JButton xOver1 = new JButton("1/x");
        JButton plusOrMinus = new JButton("+/-");
        JButton decimal = new JButton(".");
        JButton eql = new JButton("eql");
     
        zero.addActionListener(this);
        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        nine.addActionListener(this);
        addition.addActionListener(this);
        subtract.addActionListener(this);
        multiply.addActionListener(this);
        divide.addActionListener(this);
        sqrt.addActionListener(this);
        xOver1.addActionListener(this);
        plusOrMinus.addActionListener(this);
        decimal.addActionListener(this);
        eql.addActionListener(this);
     
     
        p4.setForeground(new Color(0, 0, 235));
     
        p4.setFont(new Font("Dialog", Font.PLAIN, 14));
     
        p4.add(zero);
        p4.add(one);
        p4.add(two);
        p4.add(three);
        p4.add(four);
        p4.add(five);
        p4.add(six);
        p4.add(seven);
        p4.add(eight);
        p4.add(nine);
        p4.add(addition);
        p4.add(subtract);
        p4.add(multiply);
        p4.add(divide);
        p4.add(sqrt);
        p4.add(xOver1);
        p4.add(plusOrMinus);
        p4.add(decimal);
        p4.add(eql);
     
     
     
        // Add and initialize output and total
     
        // [JLabel and JTextArea for output]
     
        output.setBackground(Color.white);
     
        output.setForeground(Color.black);
     
        output.setFont(new Font("Courier", Font.BOLD, 18));
     
     
     
        total.setBackground(new Color(255, 255, 128));
     
        total.setForeground(Color.black);
     
        total.setFont(new Font("Courier", Font.BOLD, 14));
     
     
     
        p5.add("Center", scrollingArea);
     
        p5.add("South",  output);
     
     
     
     
     
        // Hook up the panels
     
        p2.add("North", p3);
     
        p2.add("Center", p4);
     
     
     
        // Add some spacing around outside
     
        p1.add("North",  p5);
     
        p1.add("Center", p2);
     
     
     
        p.setFont(new Font("Helvetica", Font.PLAIN, 6));
     
        p.add("North", new Label(" "));
     
        p.add("East",  new Label(" "));
     
        p.add("West",  new Label(" "));
     
        p.add("South", new Label(" "));
     
        p.add("Center", p1);
     
     
     
        return p;
     
      }
     
    }

    I need some help with finding the errors in this calculator. I tried for 3 hours and only found some. Could someone please help thanks


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Calculator Code, Need Help Please

    Feed the code into some IDE such as NetBeans or Eclipse and you'll see all the errors.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Calculator Code, Need Help Please

    Welcome sparkyz

    Try to compile it.
    Look at the first error.
    Fix it.
    Try to compile again.
    Repeat fixing syntax errors until it compiles.
    If you run into an error you can not correct, post the full text of the error message with the current version of the code and your question(s).

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator Code, Need Help Please

    Ok i narrowed it down to this part


    String padText(String s, int size)
     
      {
     
        String temp = " " + s;
     
        return temp.substring(temp.length() - size);
     
      }
     
     
     
      // Handles the backspace key
     
      void backSpace()
     
      {
     
        s = s.substring(0, s.length()-1);
     
        setCurVal(s);
     
      }

    it says


    --------------------Configuration: <Default>--------------------
    C:\Users\Brandon\Documents\Calc7.java:222: error: cannot find symbol
    s = s.substring(0, s.length()-1);
    ^
    symbol: variable s
    location: class Calc7
    C:\Users\Brandon\Documents\Calc7.java:222: error: cannot find symbol
    s = s.substring(0, s.length()-1);
    ^
    symbol: variable s
    location: class Calc7
    C:\Users\Brandon\Documents\Calc7.java:222: error: cannot find symbol
    s = s.substring(0, s.length()-1);
    ^
    symbol: variable s
    location: class Calc7
    C:\Users\Brandon\Documents\Calc7.java:224: error: cannot find symbol
    CurVal(s);
    ^
    symbol: variable s
    location: class Calc7
    4 errors

    Process completed.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Calculator Code, Need Help Please

    Where is s declared?
    Why is there access to s outside the scope of s?

Similar Threads

  1. Slight problem with fraction calculator code
    By Jampolo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2013, 04:01 PM
  2. Tax Refund Calculator......Need help finishing this code!!
    By txaviermars in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 25th, 2012, 11:16 AM
  3. I need help with my Netbeans calculator code
    By geto81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2012, 12:53 PM
  4. having bad time with basic calculator code
    By hashey100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 08:29 PM
  5. Help needed with calculator code
    By andys in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2011, 08:02 PM