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

Thread: Calculator app

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calculator app

    Hello,

    I have finally reached the final assignment of my Java course. First I will give a detailed description of the assignment:
    - The application should be a JApplet that also can be run as an application
    - The calculator should contain the following buttons: 1,2,3,4,5,6,7,8,9,0,MC,MR,MS,M+,BACKSPACE,CE,C,," ,",".",+,-,*,/,sgrt,%,1/x,=
    - Use javaDoc
    - Make html files that contain JavaDoc
    - Make an executable jar.

    The first thing I want to do is to figure out all of the classes I will have to create. Here starts my problem. Should I make a class for each button on the calculator (because a calculator HAS-A button)?
    And thats where I need a little push in the right direction. How can I determine of what entity to make a class?
    Because there are also the calculations, display in a JTextfield, etc.

    I really hope you can help me determine all of the necessary classes before I even start building.

    This is a crosspost to: Calculator app


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Calculator app

    Quote Originally Posted by bodylojohn View Post
    The first thing I want to do is to figure out all of the classes I will have to create.
    Firstly, since you want both an applet and a standalone "desktop" application, you should have a single panel (e.g. JPanel) that contains all your user-interface. Then it will be absolutely straightforward to insert this panel into a JApplet (for applet) or a JFrame (for desktop app).
    You should use inheritance, in other words, e.g.: CalculatorPanel (that extends JPanel), CalculatorApplet (that extends JApplet), and so on ....

    For a standalone app, you also need the classic main(String[]) method. You can certainly put the main into the class for the frame, but I suggest you to create an apposite, small, class for this.

    Quote Originally Posted by bodylojohn View Post
    Should I make a class for each button on the calculator (because a calculator HAS-A button)?
    No. At most you might want to use a single JButton subclass, e.g. CalculatorButton if all your buttons need to share some properties or behaviour (e.g. same border, font, etc...).
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    GregBrannon (March 14th, 2014)

  4. #3
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    @andbin:
    Thanks for the very detailed explanation.
    I will look into it and post back my progress.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,712
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Calculator app

    And I think beginners get hung up on "defining all of the classes I need" as a first step. It's important to have a design in mind before coding the first line, and it should be written down as much as possible. Rather than specific classes, the design should start at a functional level: the design should describe what the app does. Those functions can be grouped together. For a graphical program, the goal should be to separate the user interface from the data or business portion with a higher-level application manager that manages the communication between the two. (An MVC design.)

    Once the apps functions are defined and separated into User Interface (View), Business/Data (Model), and Control (Controller), THEN classes should begin to take shape.

  6. #5
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    Could anybody please help me in creating the gui (see image below).
    I already have 3 classes; CalcApplet, CalcApp and Calculator.

    Calculator contains some testcode thats shows a button.
    import java.awt.*;
    import javax.swing.*;
     
    public class Calculator extends JPanel {
    	public Calculator(){
    	    setLayout( new BorderLayout() );
    	    JPanel mainPanel = new JPanel();
    	    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
     
     
    	    JPanel pDisplay = new JPanel();
    	    pDisplay.setBackground(Color.BLUE);
     
    	    JPanel pNummeriek = new JPanel();
    	    pNummeriek.setBackground(Color.GRAY);
     
     
              mainPanel.add(pDisplay);
    	    mainPanel.add(pNummeriek);
    	    CalcButton t = new CalcButton("Test");
    	    CalcButton q = new CalcButton("Testq");
    	    pDisplay.add(t);
    	    pNummeriek.add(q);
     
    	    add( mainPanel, BorderLayout.CENTER );
    	}
    }

    CalcApplet runs Calculator as an JApplet
    import java.awt.*;
    import javax.swing.*;
     
    /**
     * <p>Title: 		CalcApplet </p>
     * <p>Description: 	Dit is de calculator welke als applet gerund wordt </p>
     * <p>Copyright: 	Copyright (c) 2014</p>
     * <p>Date: 		14-03-2014 </p>
     * <p>@author  		Johnny Hermes </p>
     * <p>@version		1.0 </p>
     * 
     **/
    public class CalcApplet extends JApplet {
     
    	/**
    	 * <p> Description: Inits the applet</p>
    	 * 
    	 **/	
    	public void init()
    	{
    		// Definieer de layout
    		setLayout( new BorderLayout() );
    		// Definieer de grootte
    		setSize(350, 310);
    		// Voeg een instance toe aan de applet van Calculator
    		add( new Calculator(), BorderLayout.CENTER );
     
    	}
     
     
    }

    CalcApp runs Calculator as an Application.
    /**
     * <p>Title: 		CalcApp </p>
     * <p>Description: 	Dit is de calculator welke als application gerund wordt </p>
     * <p>Copyright: 	Copyright (c) 2014</p>
     * <p>Date: 		14-03-2014 </p>
     * <p>@author  		Johnny Hermes </p>
     * <p>@version		1.0 </p>
     * 
     **/
     
    	import java.awt.*;
    	import java.awt.event.*;
     
    public class CalcApp {
    	/**
    	* <p> Description: the main method</p>
    	* @param args
    	*/
    	public static void main( String args[] )
    	{
    		// Create a frame
    	   Frame fr = new Frame();
    	   // set the properties of the frame
    	   fr.setTitle( "Awt Calculator" );
    	   fr.setSize( 350, 310 );
    	   fr.setResizable( false );
    	   // add a new instance of AwtCalc to the frame
    	   fr.add( new Calculator(), BorderLayout.CENTER );
    	   //Set the frame visible
    	   fr.setVisible( true );
    	   // add a windowlistener to the frame
    	   fr.addWindowListener( new WindowAdapter() 
    	   {
    	   		/**
    	   		 * Method: windowClosing
    	   		 * <p> Description: This method will close the frame
    	   		 */
    		   public void windowClosing( WindowEvent e )
    	   		{
    	   	   		System.exit( 1 );
    	   		}
    		   });
    		}
     
    	}

    Calculator.jpg

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,712
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Calculator app

    What's preventing you from building the GUI? What help do you need?

  8. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Calculator app

    Quote Originally Posted by bodylojohn View Post
    Could anybody please help me in creating the gui (see image below).
    I already have 3 classes; CalcApplet, CalcApp and Calculator.
    Your code is certainly a starting point. There are some things you can improve, for example instead to use java.awt.Frame, you can and should use javax.swing.JFrame (AWT is "old" nowadays). Another thing is a little quibble about applets: the init() is not invoked on the Event Dispatch Thread (this is known and by design), so it's generally a good practice to use SwingUtilities.invokeAndWait into the init() to execute the applet GUI creation on the EDT.

    Apart this, for the calculator user interface (buttons layout etc...) you have several options. One solution is the use of GridBagLayout layout manager. It's not easy to understand (if you have never used it before) but it's very flexible. See my Java Example "Virtual Keypad", it's not a calculator but a keypad. However it uses the GridBagLayout for the buttons.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  9. #8
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    I got the gui to (almost) look like the image.
    But I don't think I coded optimally.
    Before I continue I would like to optimize the code below.

    Any tips on how to do this?


    import java.awt.*;
    import javax.swing.*;
     
    public class Calculator extends JPanel {
    	JPanel mainPanel = new JPanel();
    	BoxLayout BoxLayout = new BoxLayout(mainPanel , 1);
    	JPanel displayPanel = new JPanel();
    	JTextField display = new JTextField();
        JPanel topButtonRow = new JPanel();
        JPanel calcPanel = new JPanel();
        GridLayout topButtonRowGrid = new GridLayout();
        GridLayout calcPanelGrid = new GridLayout();
        JButton bMC = new JButton("MC");
        JButton b0 = new JButton("0");
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bDelen = new JButton("/");
        JButton bSqrt = new JButton("sqrt");
        JButton bMR = new JButton("MR");
        JButton bVermenigvuldig = new JButton("*");
        JButton bProcent = new JButton("%");
        JButton bMS = new JButton("MS");
        JButton bMinus = new JButton("-");
        JButton bPrimitieveren = new JButton("1/x");
        JButton bMPlus = new JButton("M+");
        JButton bPlusMin = new JButton("+/-");
        JButton bPunt = new JButton(".");
        JButton bPlus = new JButton("+");
        JButton bIs = new JButton("=");
        JButton bEmpty = new JButton("");
        JButton bBackspace = new JButton("Backspace");
        JButton bCE = new JButton("CE");
        JButton bC = new JButton("C");
     
    	public Calculator(){
    		add( mainPanel);   
    	    mainPanel.setLayout(BoxLayout);
    	    mainPanel.add(displayPanel);
    	    displayPanel.add(display);
    	    display.setColumns(40);
    	    mainPanel.add(topButtonRow);
    	    topButtonRow.setLayout(topButtonRowGrid);
    	    topButtonRowGrid.setRows(1);
    	    topButtonRowGrid.setColumns(4);
    	    topButtonRow.add(bEmpty);
    	    //bEmpty.setText("");
    	    topButtonRow.add(bBackspace);
    	    topButtonRow.add(bCE);
    	    topButtonRow.add(bC);
    	    mainPanel.add(calcPanel);
    	    calcPanel.setName("JCalcPanel");
    	    calcPanel.setLayout(calcPanelGrid);
    	    calcPanelGrid.setRows(4);
    	    calcPanelGrid.setColumns(6);
    	    calcPanel.add(bMC);
    	    calcPanel.add(b7);
    	    calcPanel.add(b8);
    	    calcPanel.add(b9);
    	    calcPanel.add(bDelen);
    	    calcPanel.add(bSqrt);
    	    calcPanel.add(bMR);
    	    calcPanel.add(b4);
    	    calcPanel.add(b5);
    	    calcPanel.add(b6);
    	    calcPanel.add(bVermenigvuldig);
    	    calcPanel.add(bProcent);
    	    calcPanel.add(bMS);
    	    calcPanel.add(b1);
    	    calcPanel.add(b2);
    	    calcPanel.add(b3);
    	    calcPanel.add(bMinus);
    	    calcPanel.add(bPrimitieveren);
    	    calcPanel.add(bMPlus);
    	    calcPanel.add(b0);
    	    calcPanel.add(bPlusMin);
    	    calcPanel.add(bPunt);
    	    calcPanel.add(bPlus);
    	    calcPanel.add(bIs);
    	}
    }


    --- Update ---

    Now that I covered some of the gui stuff it's time to look at the actual functionality of the calculator.
    I suppose I have to create a string that will contain the inputs from the calculator, example: "2 + 6 - 4 *2 / 2"
    Or am I totally wrong here?

  10. #9
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Calculator app

    Quote Originally Posted by bodylojohn View Post
    I suppose I have to create a string that will contain the inputs from the calculator, example: "2 + 6 - 4 *2 / 2"
    Or am I totally wrong here?
    No, not necessarily. It's just only one possible option. And it's not even easy because a string like that must be parsed/interpreted. There are apposite libraries for this. If you want to do this yourself ... beware that it is quite hard to do (depending on features you want to handle).

    There is another option, more simple: the user enters a number N, when the user presses an operator (e.g. + o -), you convert the string to a numeric value and keep it somewhere. Keep also the operator. When the user enters another number M, convert to numeric value. Now you have 2 numbers and an operator. Do the operation and keep the result somewhere. The story continues in the same way for other numbers/operators.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  11. #10
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    I am just totally stuck and have no idea how to go further.
    Could anybody please provide me with an example?

    Thank you!

  12. #11
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    Ok....

    I played around with this and I added a new class called "Calculations":
    public class Calculations {
     
    	public Calculations(){
    	}
     
    	public double resultCalc(double beforeOp,double afterOp,String operator) {
    		double result = 0;
     
    		switch (operator) {
    		case "+":
    			result = beforeOp + afterOp;
    			break;
    		case "-":
    			result = beforeOp - afterOp;
    			break;		
    		case "/":
    			result = beforeOp / afterOp;
    			break;		
    		case "*":
    			result = beforeOp * afterOp;
    			break;		
    		}
    		return result;
    	}
    }

    In my Calculator class I added some actionlisteners and now my code looks like this (but I think my code should look much neater and smaller):
    //import java.awt.event.ActionListener;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class Calculator extends JPanel {
    	boolean numericValue,opperatorValue;
    	String value1 = "";
    	String valueAfterOp;
    	double result1;
    	String operator;
    	JPanel mainPanel = new JPanel();
    	BoxLayout boxLayout = new BoxLayout(mainPanel , 1);
    	JPanel displayPanel = new JPanel();
    	JTextField display = new JTextField();
        JPanel topButtonRow = new JPanel();
        JPanel calcPanel = new JPanel();
        GridLayout topButtonRowGrid = new GridLayout();
        GridLayout calcPanelGrid = new GridLayout();
        JButton bMC = new JButton("MC");
        JButton b0 = new JButton("0");
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bDivide = new JButton("/");
        JButton bSqrt = new JButton("sqrt");
        JButton bMR = new JButton("MR");
        JButton bMultiply = new JButton("*");
        JButton bPercent = new JButton("%");
        JButton bMS = new JButton("MS");
        JButton bMinus = new JButton("-");
        JButton Reciprocal = new JButton("1/x");
        JButton bMPlus = new JButton("M+");
        JButton bSign = new JButton("+/-");
        JButton bPoint = new JButton(".");
        JButton bAddition = new JButton("+");
        JButton bEquals = new JButton("=");
        JButton bEmpty = new JButton("");
        JButton bBackspace = new JButton("Backspace");
        JButton bCE = new JButton("CE");
        JButton bC = new JButton("C");
     
    	public Calculator(){
    		add( mainPanel);   
    	    mainPanel.setLayout(boxLayout);
    	    mainPanel.add(displayPanel);
    	    displayPanel.add(display);
    	    display.setColumns(40);
    	    display.setText("0");
    	    display.setHorizontalAlignment(display.RIGHT);
    	    mainPanel.add(topButtonRow);
    	    topButtonRow.setLayout(topButtonRowGrid);
    	    topButtonRowGrid.setRows(1);
    	    topButtonRowGrid.setColumns(4);
    	    topButtonRow.add(bEmpty);
    	    //bEmpty.setText("");
    	    topButtonRow.add(bBackspace);
    	    topButtonRow.add(bCE);
    	    topButtonRow.add(bC);
    	    mainPanel.add(calcPanel);
    	    calcPanel.setName("JCalcPanel");
    	    calcPanel.setLayout(calcPanelGrid);
    	    calcPanelGrid.setRows(4);
    	    calcPanelGrid.setColumns(6);
    	    calcPanel.add(bMC);
    	    calcPanel.add(b7);
    	    calcPanel.add(b8);
    	    calcPanel.add(b9);
    	    calcPanel.add(bDivide);
    	    calcPanel.add(bSqrt);
    	    calcPanel.add(bMR);
    	    calcPanel.add(b4);
    	    calcPanel.add(b5);
    	    calcPanel.add(b6);
    	    calcPanel.add(bMultiply);
    	    calcPanel.add(bPercent);
    	    calcPanel.add(bMS);
    	    calcPanel.add(b1);
    	    b1.addActionListener(new b1Listener());
    	    calcPanel.add(b2);
    	    b2.addActionListener(new b2Listener());
    	    calcPanel.add(b3);
    	    calcPanel.add(bMinus);
    	    calcPanel.add(Reciprocal);
    	    calcPanel.add(bMPlus);
    	    calcPanel.add(b0);
    	    calcPanel.add(bSign);
    	    calcPanel.add(bPoint);
    	    calcPanel.add(bAddition);
    	    bAddition.addActionListener(new bAdditionListener());
    	    calcPanel.add(bEquals);
    	    bEquals.addActionListener(new bEqualsListener());
    	  }
     
    	class b1Listener implements ActionListener{
    		// Button 1
    		public void actionPerformed(ActionEvent event){
    			//numericValue = true;
    			//opperatorValue = false;
    			value1 += 1;
    			display.setText(value1);
    		}
    	}
     
    	class b2Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			//numericValue = true;
    			//opperatorValue = false;
    			value1 += 2;
    			display.setText(value1);
    		}
    	}
     
    	class bAdditionListener implements ActionListener{
    		// Addition operator
    		public void actionPerformed(ActionEvent event){
    			//numericValue = false;
    			//opperatorValue = true;
    			valueAfterOp = display.getText();
    			value1 = "";
    			operator = "+";
    		}
    	}
     
    	class bEqualsListener implements ActionListener{
    		// the equals (=) button
    		public void actionPerformed(ActionEvent event){
    			//numericValue = false;
    			//opperatorValue = true;
     
    			Calculations calc = new Calculations();
    			display.setText(calc.resultCalc(Double.parseDouble(valueAfterOp), Double.parseDouble(value1),operator) + "");
    		}
    	}
     
    }

    Is this a step in the right direction?

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,712
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Calculator app

    Reread Andbin's second paragraph and explain what is there that confuses you. Ask specific questions.

  14. #13
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    @GregBrannon: I came a bit further as you can see in post #11
    But i am far from finished.

  15. #14
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    I took me a while but I finally got a working concept of the calculator. Although I am kinda proud that I got it in working condition I am sure there are things i've overseen.
    There probably is a lot of coding that is not up to standards. But....i've got a starting point and now I can start (with all of your help off course) to turn it into an application that conforms to the good programming principles.

    I would love to hear all of your comments, tips and tricks so I can optimize this whole application.
    I also think that I should use more classes. But I just can't specify them.
    Javadoc will be added along the way!

    So again...I am really looking forward to all of your comments so I can learn much more about java.

    I will post all of my classes below:

    Class CalcApp
    /**
     * <p>Title: 		CalcApp </p>
     * <p>Description: 	The calculator will be ran as an application </p>
     * <p>Copyright: 	Copyright (c) 2014</p>
     * <p>Date: 		14-03-2014 </p>
     * <p>@author  		John Hermes </p>
     * <p>@version		1.0 </p>
     * 
     **/
     
    	import java.awt.*;
    	import java.awt.event.*;
     
    public class CalcApp {
    	/**
    	* <p> Description: the main method</p>
    	* @param args
    	*/
    	public static void main( String args[] )
    	{
    		// Create a frame
    	   Frame fr = new Frame();
    	   // set the properties of the frame
    	   fr.setTitle( "Calculator" );
    	   fr.setSize( 500,250  );
    	   //fr.setResizable( false );
    	   // add a new instance of AwtCalc to the frame
    	   fr.add( new Calculator(), BorderLayout.CENTER );
    	   //Set the frame visible
    	   fr.setVisible( true );
    	   // add a windowlistener to the frame
    	   fr.addWindowListener( new WindowAdapter() 
    	   {
    	   		/**
    	   		 * Method: windowClosing
    	   		 * <p> Description: This method will close the frame
    	   		 */
    		   public void windowClosing( WindowEvent e )
    	   		{
    	   	   		System.exit( 1 );
    	   		}
    		   });
    		}
     
    	}

    Class CalcApplet
    import java.awt.*;
    import javax.swing.*;
     
    /**
     * <p>Title: 		CalcApplet </p>
     * <p>Description: 	The calculator will be ran as an applet </p>
     * <p>Copyright: 	Copyright (c) 2014</p>
     * <p>Date: 		14-03-2014 </p>
     * <p>@author  		John Hermes </p>
     * <p>@version		1.0 </p>
     * 
     **/
    public class CalcApplet extends JApplet {
     
    	/**
    	 * <p> Description: Inits the applet</p>
    	 * 
    	 **/	
    	public void init()
    	{
    		// Definieer de layout
    		setLayout( new BorderLayout() );
    		// Definieer de grootte
    		setSize(450, 450);
    		// Voeg een instance toe aan de applet van Calculator
    		add( new Calculator(), BorderLayout.CENTER );
    	}
    }

    Class Calculator
    //import java.awt.event.ActionListener;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
    public class Calculator extends JPanel {
    	boolean numericValue,opperatorValue;
    	String buttonText;
    	String value1 = "";
    	String memory;
    	String valueAfterOp;
    	double result1;
    	String operator;
    	JPanel mainPanel = new JPanel();
    	BoxLayout boxLayout = new BoxLayout(mainPanel , 1);
    	JPanel displayPanel = new JPanel();
    	JTextField display = new JTextField();
        JPanel topButtonRow = new JPanel();
        JPanel calcPanel = new JPanel();
        GridLayout topButtonRowGrid = new GridLayout();
        GridLayout calcPanelGrid = new GridLayout();
        JButton bMemoryClear = new JButton("MC");
        JButton b0 = new JButton("0");
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bDivide = new JButton("/");
        JButton bSqrt = new JButton("sqrt");
        JButton bMemoryRecall = new JButton("MR");
        JButton bMultiply = new JButton("*");
        JButton bPercent = new JButton("%");
        JButton bMemoryStore = new JButton("MS");
        JButton bMinus = new JButton("-");
        JButton bReciprocal = new JButton("1/x");
        JButton bMemoryAdd = new JButton("M+");
        JButton bSign = new JButton("+/-");
        JButton bPoint = new JButton(".");
        JButton bAddition = new JButton("+");
        JButton bEquals = new JButton("=");
        JButton bDisplayMem = new JButton("");
        JButton bBackspace = new JButton("Backspace");
        JButton bCE = new JButton("CE");
        JButton bC = new JButton("C");
     
    	public Calculator(){
    		add( mainPanel);   
    	    mainPanel.setLayout(boxLayout);
    	    mainPanel.add(displayPanel);
    	    displayPanel.add(display);
    	    display.setColumns(40);
    	    display.setText("0");
    	    display.setHorizontalAlignment(SwingConstants.RIGHT);
    	    mainPanel.add(topButtonRow);
    	    topButtonRow.setLayout(topButtonRowGrid);
    	    topButtonRowGrid.setRows(1);
    	    topButtonRowGrid.setColumns(4);
    	    topButtonRow.add(bDisplayMem);
    	    //bEmpty.setText("");
    	    topButtonRow.add(bBackspace);
    	    bBackspace.addActionListener(new bBackspaceListener());
    	    topButtonRow.add(bCE);
    	    bCE.addActionListener(new bCeListener());
    	    topButtonRow.add(bC);
    	    bC.addActionListener(new bCListener());
    	    mainPanel.add(calcPanel);
    	    calcPanel.setName("JCalcPanel");
    	    calcPanel.setLayout(calcPanelGrid);
    	    calcPanelGrid.setRows(4);
    	    calcPanelGrid.setColumns(6);
    	    calcPanel.add(bMemoryClear);
    	    bMemoryClear.addActionListener(new bMemoryClearListener());
    	    calcPanel.add(b7);
    	    b7.addActionListener(new b7Listener());
    	    calcPanel.add(b8);
    	    b8.addActionListener(new b8Listener());
    	    calcPanel.add(b9);
    	    b9.addActionListener(new b9Listener());
    	    calcPanel.add(bDivide);
    	    bDivide.addActionListener(new bDivideListener());
    	    calcPanel.add(bSqrt);
    	    bSqrt.addActionListener(new bSqrtListener());
    	    calcPanel.add(bMemoryRecall);
    	    bMemoryRecall.addActionListener(new bMemoryRecallListener());
    	    calcPanel.add(b4);
    	    b4.addActionListener(new b4Listener());
    	    calcPanel.add(b5);
    	    b5.addActionListener(new b5Listener());
    	    calcPanel.add(b6);
    	    b6.addActionListener(new b6Listener());
    	    calcPanel.add(bMultiply);
    	    bMultiply.addActionListener(new bMultiplyListener());
    	    calcPanel.add(bPercent);
    	    bPercent.addActionListener(new bPercentListener());
    	    calcPanel.add(bMemoryStore);
    	    bMemoryStore.addActionListener(new bMemoryStoreListener());
    	    calcPanel.add(b1);
    	    bAddition.addActionListener(new bAdditionListener());
    	    b1.addActionListener(new b1Listener());
    	    calcPanel.add(b2);
    	    b2.addActionListener(new b2Listener());
    	    calcPanel.add(b3);
    	    b3.addActionListener(new b3Listener());
    	    calcPanel.add(bMinus);
    	    bMinus.addActionListener(new bMinusListener());
    	    calcPanel.add(bReciprocal);
    	    bReciprocal.addActionListener(new bReciprocalListener());
    	    calcPanel.add(bMemoryAdd);
    	    bMemoryAdd.addActionListener(new bMemoryAddListener());
    	    calcPanel.add(b0);
    	    b0.addActionListener(new b0Listener());
    	    calcPanel.add(bSign);
    	    bSign.addActionListener(new bSignListener());
    	    calcPanel.add(bPoint);
    	    bPoint.addActionListener(new bPointListener());
    	    calcPanel.add(bAddition);
    	    bAddition.addActionListener(new bAdditionListener());
    	    calcPanel.add(bEquals);
    	    bEquals.addActionListener(new bEqualsListener());
    	  }
     
    	public void displayNumbers(String valueNum) {
    		value1 += valueNum;
    		display.setText(value1);
    		numericValue = true;
    		opperatorValue = false;
    	}
     
    	//Handles operators 
    	public void operator (String getOparator) {
    		numericValue = false;
    		opperatorValue = true;
    		valueAfterOp = display.getText();
    		value1 = "";
    		operator = getOparator;
    	}
     
    	class bMemoryClearListener implements ActionListener{
    		// Button 1/x divides 1 by x
    		public void actionPerformed(ActionEvent event){
    			memory = "";
    			bDisplayMem.setText("");
    		}
    	}
     
    	class bMemoryRecallListener implements ActionListener{
    		// Button 1/x divides 1 by x
    		public void actionPerformed(ActionEvent event){
    			display.setText(memory);
    			value1 = memory;
    		}
    	}
     
    	class bMemoryStoreListener implements ActionListener{
    		// Button 1/x divides 1 by x
    		public void actionPerformed(ActionEvent event){
    			bDisplayMem.setText("M");
    			memory = display.getText();
    		}
    	}
     
    	class bMemoryAddListener implements ActionListener{
    		// Button 1/x divides 1 by x
    		public void actionPerformed(ActionEvent event){
    			double result;
    			result = Double.parseDouble(memory) + Double.parseDouble(display.getText());
    			//memory += display.getText();
    			memory = result + "";
    		}
    	}
     
     
    	class bReciprocalListener implements ActionListener{
    		// Button 1/x divides 1 by x
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.divideByX(display);
    			display.setText(value1);
    		}
    	}
     
    	class bSqrtListener implements ActionListener{
    		// Sqrt
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.sqrt(display);
    			display.setText(value1);
    		}
    	}
     
    	class bPercentListener implements ActionListener{
    		// Percent
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.percent(display, valueAfterOp);
    			display.setText(value1);
    		}
    	}
     
    	class bCListener implements ActionListener{
    		// Button C Clears all 
    		public void actionPerformed(ActionEvent event){
    			value1 = "";
    			display.setText("0");
    			valueAfterOp = "";
    			operator = "";
    		}
    	}
     
     
    	class bCeListener implements ActionListener{
    		// Button CE Clears last nummeric entrance
    		public void actionPerformed(ActionEvent event){
    			value1 = "";
    			display.setText("0");
    		}
    	}
     
    	class bPointListener implements ActionListener{
    		// Button 0
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(bPoint.getText());
    		}
    	}
     
    	class b0Listener implements ActionListener{
    		// Button 0
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b0.getText());
    		}
    	}
     
    	class b1Listener implements ActionListener{
    		// Button 1
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b1.getText());
    		}
    	}
     
    	class b2Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b2.getText());
    		}
    	}
     
    	class b3Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b3.getText());
    		}
    	}
    	class b4Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b4.getText());
    		}
    	}
    	class b5Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b5.getText());
    		}
    	}
    	class b6Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b6.getText());
    		}
    	}
    	class b7Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b7.getText());
    		}
    	}
    	class b8Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b8.getText());
    		}
    	}
    	class b9Listener implements ActionListener{
    		// Button 2
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(b9.getText());
    		}
    	}
     
    	class bAdditionListener implements ActionListener{
    		// Addition operator
    		public void actionPerformed(ActionEvent event){
    			operator(bAddition.getText());
    		}
    	}
     
    	class bSignListener implements ActionListener{
    		// Sign operator
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.signChange(display);
    		}
    	}
     
    	class bMinusListener implements ActionListener{
    		// Minus operator
    		public void actionPerformed(ActionEvent event){
    			operator(bMinus.getText());
    		}
    	}
     
    	class bMultiplyListener implements ActionListener{
    		// Multiply operator
    		public void actionPerformed(ActionEvent event){
    			operator(bMultiply.getText());
    		}
    	}
     
    	class bDivideListener implements ActionListener{
    		// Divide operator
    		public void actionPerformed(ActionEvent event){
    			operator(bDivide.getText());
    		}
    	}
     
    	class bBackspaceListener implements ActionListener{
    		// Backspace operator
    		public void actionPerformed(ActionEvent event){
    			//backspace();
    			Calculations calc = new Calculations();
    			calc.backspace(display, numericValue);
    		}
    	}
     
    	class bEqualsListener implements ActionListener{
    		// the equals (=) button
    		public void actionPerformed(ActionEvent event){
    			numericValue = false;
    			opperatorValue = true;
     
    			//Check if there is a division by zero.
    			if (value1.equals("0") && operator.equals("/")) {
    				display.setText("Cannot divide by zero.");
    			} else {
    				Calculations calc = new Calculations();
    				display.setText(calc.resultCalc(Double.parseDouble(valueAfterOp), Double.parseDouble(value1),operator) + "");
    			}
    		}
    	}
    }

    Class Calculations
    import javax.swing.JTextField;
     
    public class Calculations {
     
    	public Calculations(){
    	}
     
    	public double resultCalc(double beforeOp,double afterOp,String operator) {
    		double result = 0;
     
    		switch (operator) {
    		case "+":
    			result = beforeOp + afterOp;
    			break;
    		case "-":
    			result = beforeOp - afterOp;
    			break;		
    		case "/":
    			result = beforeOp / afterOp;
    			break;		
    		case "*":
    			result = beforeOp * afterOp;
    			break;		
    		}
    		return result;
    	}
     
    	public void backspace(JTextField display,Boolean checkNumVal) {
    		String tempNum = "";
    		if (checkNumVal == true) {
    			if(display.getText().length() < 2)	{
    				tempNum = "";
    				display.setText("0");
    			}else {
    				if ((display.getText().charAt(display.getText().length() - 1) == '.' &&
    					display.getText().charAt(display.getText().length() - 2) == '0') 
    					|| (display.getText().length() == 2 && display.getText().charAt(0) == '-')) {
    						tempNum = "";
    						display.setText("0");
    						//decimalUsed = false;
    				} else if (display.getText().charAt(display.getText().length() - 1) == '.') {
    					//decimalUsed = false;
    					tempNum = display.getText().substring(0, display.getText().length() - 1);
    					display.setText(tempNum);
    				} else {
    					tempNum = display.getText().substring(0, display.getText().length() - 1);
    					//value1 = tempNum;
    					display.setText(tempNum);		
    				}
    			}
    		}
    	}
     
    	//Makes it negative if positive and vice versa
    	public String signChange(JTextField display)	{
    		String temp = display.getText();
     
    		if (temp.equals("0") || temp.equals("0.0")) {
    		} else {
    			if (temp.charAt(0) == '-') {
    				temp = temp.substring(1,temp.length());
    			} else {
    				temp = "-" + temp;
    			}
    			display.setText(temp);
    		}
    		return temp;
    	}
     
    			//1 Divide by X function (1/x)
    			public String divideByX(JTextField display)	{
    				String temp;
    					temp = display.getText();
    					if(temp.equals("0"))
    						return "Cannot divide by zero.";
    					else
    						return (1 / Double.parseDouble(temp)) + "" ;
    				}
     
    			//Square Root function. (sqrt)
    			public String sqrt(JTextField display)	{
    				String temp;
    				temp = display.getText();
    				if(temp.indexOf("-") == 0) {
    					return "";
    				} else {
    					return (Math.sqrt(Double.parseDouble(temp))) + "";
    				}
    			}
     
    			//Calculates percent (%)
    			public String percent(JTextField display,String valueAfterOp)	{
    				double A,B,result = 0;
    				A = Double.parseDouble(valueAfterOp);
    				B = Double.parseDouble(display.getText());
    				result = A*B/100;
    				return result + "";
    			}
    }

    Thanks in advance!!!!

  16. #15
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator app

    Thanks again for all of your help. I read into refractoring a little bit and then reviewed my code but I just can't see an opportunity to clean up my code.
    I added all of the javadoc into my code.

    Can anybody see how I can divide up my code into more classes according to the IS-A and HAS-A methodology?

    Below are my 2 main classes Calculator and Calculations:

    Calculations
    import javax.swing.JTextField;
     
    /**
     * <p>Title: 		Calculations </p>
     * <p>Description: 	The calculations behind the calculator </p>
     * <p>Copyright: 	Copyright (c) 2014</p>
     * <p>Date: 		14-03-2014 </p>
     * <p>@author  		John Hermes </p>
     * <p>@version		1.0 </p>
     * 
     **/
    public class Calculations {
     
    	public Calculations(){
    	}
     
    	/**
    	 * Description:  function that calculates 2 values based on operators; +,-,/,*
    	 * <p>
    	 * Calculate 2 values based on the operator (+,-,/,*) and return the value.
    	 * <p>
    		 * @return resultCalc 	This double value is the result of the calculations.
    		 * @param beforeOp		The value entered before the operator button is clicked.
    		 * @param afterOp		The value entered after the operator button is clicked.
    		 * @param operator 		The operator button the user has pressed (+,-,/,*).
    		 *
    	 **/ 
    	public double resultCalc(double beforeOp,double afterOp,String operator) {
    		double result = 0;
     
    		switch (operator) {
    		case "+":
    			result = beforeOp + afterOp;
    			break;
    		case "-":
    			result = beforeOp - afterOp;
    			break;		
    		case "/":
    			result = beforeOp / afterOp;
    			break;		
    		case "*":
    			result = beforeOp * afterOp;
    			break;		
    		}
    		return result;
    	}
     
    	/**
    	 * Description:  Backspace
    	 * <p>
    	 * if the user hits the backspace button 1 character (last char entered) of the display.
    	 * <p>
    		 * @param display			The value in the display the moment the method is invoked.
    		 * @param checkNumVal		checks for numeric value.
    		**/ 
    	public void backspace(JTextField display,Boolean checkNumVal) {
    		String tempNum = "";
    		if (checkNumVal == true) {
    			if(display.getText().length() < 2)	{
    				tempNum = "";
    				display.setText("0");
    			}else {
    				if ((display.getText().charAt(display.getText().length() - 1) == '.' &&
    					display.getText().charAt(display.getText().length() - 2) == '0') 
    					|| (display.getText().length() == 2 && display.getText().charAt(0) == '-')) {
    						tempNum = "";
    						display.setText("0");
    						//decimalUsed = false;
    				} else if (display.getText().charAt(display.getText().length() - 1) == '.') {
    					//decimalUsed = false;
    					tempNum = display.getText().substring(0, display.getText().length() - 1);
    					display.setText(tempNum);
    				} else {
    					tempNum = display.getText().substring(0, display.getText().length() - 1);
    					//value1 = tempNum;
    					display.setText(tempNum);		
    				}
    			}
    		}
    	}
     
    	/**
    	 * Description:  signChange
    	 * <p>
    	 * Adds or removes the '-' char when the method is invoked. 
    	 * Makes it negative if positive and vice versa.
    	 * <p>
    	 * @return signChange	The value of the display with the '-' char added or removed
    	 * @param display		The value of the display when the method is invoked.	
    	**/ 
    	public String signChange(JTextField display)	{
    		String temp = display.getText();
     
    		if (temp.equals("0") || temp.equals("0.0")) {
    		} else {
    			if (temp.charAt(0) == '-') {
    				temp = temp.substring(1,temp.length());
    			} else {
    				temp = "-" + temp;
    			}
    			display.setText(temp);
    		}
    		return temp;
    	}
     
    	/**
    	 * Description:  Reciprocal
    	 * <p>
    	 * Divide 1 by x (1/x).
    	 * <p>
    	 * @return divideBy		Returns the value of the calculation (1/X). 
    	 * @param display		The value of the display when the method is invoked.	
    	**/ 
    	public String divideByX(JTextField display)	{
    		String temp;
    			temp = display.getText();
    			if(temp.equals("0"))
    				return "Cannot divide by zero.";
    			else
    				return (1 / Double.parseDouble(temp)) + "" ;
    		}
     
    	/**
    	 * Description:  Square root
    	 * <p>
    	 * Square root.
    	 * <p>
    	 * @return sqrt		Returns the value of the calculation (sqrt). 
    	 * @param display	The value of the display when the method is invoked.	
    	**/ 
    	public String sqrt(JTextField display)	{
    		String temp;
    		temp = display.getText();
    		if(temp.indexOf("-") == 0) {
    			return "";
    		} else {
    			return (Math.sqrt(Double.parseDouble(temp))) + "";
    		}
    	}
     
    	/**
    	 * Description:  Percent
    	 * <p>
    	 * Calculates the percent.
    	 * <p>
    	 * @return percent	Returns the value of the calculation (%). 
    	 * @param display	The value of the display when the method is invoked.	
    	**/ 
    	public String percent(JTextField display,String valueAfterOp)	{
    		double A,B,result = 0;
    		A = Double.parseDouble(valueAfterOp);
    		B = Double.parseDouble(display.getText());
    		result = A*B/100;
    		return result + "";
    	}
    }

    Calculator
    //import java.awt.event.ActionListener;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
    /**
     * <p>Title: 		Calculator </p>
     * <p>Description: 	The GUI and listener classes of the calculator </p>
     * <p>Copyright: 	Copyright (c) 2014</p>
     * <p>Date: 		14-03-2014 </p>
     * <p>@author  		John Hermes </p>
     * <p>@version		1.0 </p>
     * 
     **/
    public class Calculator extends JPanel {
    	boolean numericValue,opperatorValue;
    	String buttonText;
    	String value1 = "";
    	String memory;
    	String valueAfterOp;
    	double result1;
    	String operator;
    	JPanel mainPanel = new JPanel();
    	BoxLayout boxLayout = new BoxLayout(mainPanel , 1);
    	JPanel displayPanel = new JPanel();
    	JTextField display = new JTextField();
        JPanel topButtonRow = new JPanel();
        JPanel calcPanel = new JPanel();
        GridLayout topButtonRowGrid = new GridLayout();
        GridLayout calcPanelGrid = new GridLayout();
        JButton bMemoryClear = new JButton("MC");
        JButton b0 = new JButton("0");
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bDivide = new JButton("/");
        JButton bSqrt = new JButton("sqrt");
        JButton bMemoryRecall = new JButton("MR");
        JButton bMultiply = new JButton("*");
        JButton bPercent = new JButton("%");
        JButton bMemoryStore = new JButton("MS");
        JButton bMinus = new JButton("-");
        JButton bReciprocal = new JButton("1/x");
        JButton bMemoryAdd = new JButton("M+");
        JButton bSign = new JButton("+/-");
        JButton bPoint = new JButton(".");
        JButton bAddition = new JButton("+");
        JButton bEquals = new JButton("=");
        JButton bDisplayMem = new JButton("");
        JButton bBackspace = new JButton("Backspace");
        JButton bCE = new JButton("CE");
        JButton bC = new JButton("C");
     
     
    	public Calculator(){
    		add( mainPanel);   
    	    mainPanel.setLayout(boxLayout);
    	    mainPanel.add(displayPanel);
    	    displayPanel.add(display);
    	    display.setColumns(40);
    	    display.setText("0");
    	    display.setHorizontalAlignment(SwingConstants.RIGHT);
    	    mainPanel.add(topButtonRow);
    	    topButtonRow.setLayout(topButtonRowGrid);
    	    topButtonRowGrid.setRows(1);
    	    topButtonRowGrid.setColumns(4);
    	    topButtonRow.add(bDisplayMem);
    	    topButtonRow.add(bBackspace);
    	    topButtonRow.add(bCE);
    	    topButtonRow.add(bC);
    	    mainPanel.add(calcPanel);
    	    calcPanel.setName("JCalcPanel");
    	    calcPanel.setLayout(calcPanelGrid);
    	    calcPanelGrid.setRows(4);
    	    calcPanelGrid.setColumns(6);
     
    	    // Add buttons to panel
    	    JButton buttons []  = {bMemoryClear,b7,b8,b9,bDivide,bSqrt,bMemoryRecall,b4,b5,b6,bMultiply,bPercent,bMemoryStore,b1,b2,b3,bMinus,bReciprocal,bMemoryAdd,b0,bSign,bPoint,bAddition,bEquals};
    	    for(int i = 0; i < buttons.length; i++){
    	    	calcPanel.add(buttons[i]);
            	}
     
    	    // Add listeners
    	    bBackspace.addActionListener(new bBackspaceListener());
    	    bC.addActionListener(new bCListener());
    	    bCE.addActionListener(new bCeListener());
    	    bMemoryClear.addActionListener(new bMemoryClearListener());
    	    bDivide.addActionListener(new bDivideListener());
    	    bSqrt.addActionListener(new bSqrtListener());
    	    bMemoryRecall.addActionListener(new bMemoryRecallListener());
    	    bMultiply.addActionListener(new bMultiplyListener());
    	    bPercent.addActionListener(new bPercentListener());
    	    bMemoryStore.addActionListener(new bMemoryStoreListener());
    	    bAddition.addActionListener(new bAdditionListener());
    	    bMinus.addActionListener(new bMinusListener());
    	    bReciprocal.addActionListener(new bReciprocalListener());
    	    bMemoryAdd.addActionListener(new bMemoryAddListener());
    	    bSign.addActionListener(new bSignListener());
    	    bPoint.addActionListener(new bPointListener());
    	    bAddition.addActionListener(new bAdditionListener());
    	    bEquals.addActionListener(new bEqualsListener());
     
    	    JButton numButtons []  = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};
    	    for(int i = 0; i < numButtons.length; i++){
    	    	numButtons[i].addActionListener(new numberListener(i+""));
            	}
    	  	}
    		/////////////////////////////////////////////////////////////////
     
    	/**
    	 * Description: In the case a numeric value is pressed the value should be congatenated with value1 and shown in the display. 
    	 */
    	public void displayNumbers(String valueNum) {
    		value1 += valueNum;
    		display.setText(value1);
    		numericValue = true;
    		opperatorValue = false;
    	}
     
    	/**
    	 * Description: In the case a numeric value is pressed the value should be congatenated with value1 and shown in the display. 
    	 * <p>
    	 * Fill the variable 'operator' with the operator button that has been pressed.
    	 * <p>
    	 * @Param getOperator the operator that is pressed
    	 **/   
    	public void operator (String getOparator) {
    		numericValue = false;
    		opperatorValue = true;
    		valueAfterOp = display.getText();
    		value1 = "";
    		operator = getOparator;
    	}
     
    	/**
    	 * Description: Listener inner class for the button bMemoryClear.  
    	 **/  
    	class bMemoryClearListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bMemoryClear.
    		 * <p>
    		 * Clear the variable 'memory' and the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/  
    		public void actionPerformed(ActionEvent event){
    			memory = "";
    			bDisplayMem.setText("");
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bMemoryRecall.  
    	 **/  
    	class bMemoryRecallListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bMemoryRecall.
    		 * <p>
    		 * Show the value of the variable 'memory' on the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			display.setText(memory);
    			value1 = memory;
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bMemoryStore.  
    	 **/
    	class bMemoryStoreListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bMemoryStore.
    		 * <p>
    		 * Save the value on the display to the variable 'memory' and set the text of the empty button to 'M'.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			bDisplayMem.setText("M");
    			memory = display.getText();
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bMemoryAdd.  
    	 **/
    	class bMemoryAddListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bMemoryAdd.
    		 * <p>
    		 * Add the value on the display to the value that is stored in the variable 'memory'.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			double result;
    			result = Double.parseDouble(memory) + Double.parseDouble(display.getText());
    			memory = result + "";
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bReciprocal.  
    	 **/
    	class bReciprocalListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bReciprocal.
    		 * <p>
    		 * Create a reference variable to Calculations. 
    		 * Call the function divideByX and pass the value in the display.
    		 * Then show the result of the function in the display
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.divideByX(display);
    			display.setText(value1);
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bSqrt.  
    	 **/
    	class bSqrtListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bSqrt.
    		 * <p>
    		 * Create a reference variable to Calculations. 
    		 * Call the function sqrt and pass the value in the display.
    		 * Then show the result of the function in the display
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.sqrt(display);
    			display.setText(value1);
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bPercent.  
    	 **/
    	class bPercentListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bPercent.
    		 * <p>
    		 * Create a reference variable to Calculations. 
    		 * Call the function percent and pass the value in the display.
    		 * Then show the result of the function in the display
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.percent(display, valueAfterOp);
    			display.setText(value1);
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bC.  
    	 **/
    	class bCListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bC.
    		 * <p>
    		 * Clear variables value1,valueAfterOp,operator and the display when the C button is pressed 
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/
    		public void actionPerformed(ActionEvent event){
    			value1 = "";
    			display.setText("0");
    			valueAfterOp = "";
    			operator = "";
    		}
    	}
     
    	// Listerner classes
    	/**
    	 * Description: Listener inner class for the button bCe.  
    	 **/
    	class bCeListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bCe.
    		 * <p>
    		 * Clear the last numeric entrance 
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			value1 = "";
    			display.setText("0");
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bPoint.  
    	 **/
    	class bPointListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bPoint.
    		 * <p>
    		 * Call the method 'displayNumbers' and add a decimal to the number on the display. 
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			displayNumbers(bPoint.getText());
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the numeric button.  
    	 **/
    	class numberListener implements ActionListener{
    	    private String number;
     
    	    /**
    		 * Description:  Constructor of the class numberListener.
    		 * <p>
     		 * @Param number contstructs an ActionEvent
    		 **/ 
    	    public numberListener(String number) { 
    	    	this.number = number; 
    	    	}
     
    	    /**
    		 * Description:  represents the activation of the numeric buttons.
    		 * <p>
    		 * Call the method 'displayNumbers' and add the number on the button to the display. 
    		 * <p>
     		 * @event contstructs an ActionEvent
    		 **/ 
    	    public void actionPerformed(ActionEvent event){
    	        displayNumbers(number);
    	    }
    	}
     
    	/**
    	 * Description: Listener inner class for the button bAddition.  
    	 **/
    	class bAdditionListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bAddition.
    		 * <p>
    		 * Call the method 'operator' and show the value of the operator in the display. 
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			operator(bAddition.getText());
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bSign.  
    	 **/
    	class bSignListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bSign.
    		 * <p>
    		 * Create a reference variable to Calculations. 
    		 * Call the function signChange and pass the value in the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			Calculations calc = new Calculations();
    			value1 = calc.signChange(display);
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bMinus.  
    	 **/
    	class bMinusListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bMinus.
    		 * <p>
    		 * Call the method 'operator' and show the value of the operator in the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			operator(bMinus.getText());
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bMultiply.  
    	 **/
    	class bMultiplyListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bMultiply.
    		 * <p>
    		 * Call the method 'operator' and show the value of the operator in the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			operator(bMultiply.getText());
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bDivide.  
    	 **/
    	class bDivideListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bDivide.
    		 * <p>
    		 * Call the method 'operator' and show the value of the operator in the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			operator(bDivide.getText());
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bBackspace.  
    	 **/
    	class bBackspaceListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bBackspace.
    		 * <p>
    		 * Create a reference variable to Calculations. 
    		 * Call the function backspace and pass the value in the display.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			//backspace();
    			Calculations calc = new Calculations();
    			calc.backspace(display, numericValue);
    		}
    	}
     
    	/**
    	 * Description: Listener inner class for the button bEquals.  
    	 **/
    	class bEqualsListener implements ActionListener{
    		/**
    		 * Description:  represents the activation of the button bEquals.
    		 * <p>
    		 * Create a reference variable to Calculations when there is not a division by zero. 
    		 * Call the function calc and pass the values 'valueAfterOp' and 'value1'.
    		 * <p>
     		 * @Param event contstructs an ActionEvent
    		 **/ 
    		public void actionPerformed(ActionEvent event){
    			numericValue = false;
    			opperatorValue = true;
     
    			//Check if there is a division by zero.
    			if (value1.equals("0") && operator.equals("/")) {
    				display.setText("Cannot divide by zero.");
    			} else {
    				Calculations calc = new Calculations();
    				display.setText(calc.resultCalc(Double.parseDouble(valueAfterOp), Double.parseDouble(value1),operator) + "");
    			}
    		}
    	}
     
    	// ***** End listener classes ***** 
    }

Similar Threads

  1. Help with a calculator
    By minipanda1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 9th, 2013, 09:17 AM
  2. [SOLVED] Calculator
    By ikocijan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 26th, 2012, 05:54 AM
  3. My app audio sounds different when running the app?
    By TP-Oreilly in forum Android Development
    Replies: 1
    Last Post: January 29th, 2012, 07:23 AM
  4. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:08 AM
  5. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM