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

Thread: why does the clear sign not work on my calculator?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default why does the clear sign not work on my calculator?

    here is the code I wrote:
    import javax.swing.*;
     
    import java.awt.MenuBar;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class NewCalculator implements ActionListener {
     
    	//assign button clicked number and answer onto variables.
    	int numone=0,numtwo=0, answer=0;
    	char operator=' ';
    	//components of calculator
    	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 zero = new JButton("0");
    	JButton C= new JButton("C");
    	JButton CE = new JButton("CE");
    	JButton plus = new JButton("+");
    	JButton minus= new JButton("-");
    	JButton multiply = new JButton("*");
    	JButton divide = new JButton("/");
    	JButton sqrt = new JButton("sqrt");
    	JButton percent = new JButton("%");
    	JButton equal = new JButton("=");
    	JButton x = new JButton("1/x");
    	JButton plusmin = new JButton("+/-");
    	JButton fullstop = new JButton(".");
    	JButton backsp = new JButton("backspace");
    	JTextField textfl=new JTextField();
    	JMenuBar menu = new JMenuBar();
    	//used to implement panels for calculator layout.
     
    	JPanel NewCalculator(){
     
    		//action listener to listen to event on these contents.
    		 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);
    		 zero.addActionListener(this);
    		 plus.addActionListener(this);
    		 minus.addActionListener(this);
    		 multiply.addActionListener(this);
    		 divide.addActionListener(this);
    		 sqrt.addActionListener(this);
    		 percent.addActionListener(this);
    		 equal.addActionListener(this);
    		 x.addActionListener(this);
    		 plusmin.addActionListener(this);
    		 fullstop.addActionListener(this);
    		 backsp.addActionListener(this);
    		 textfl.addActionListener(this);
     
    		 //main panel
    		JPanel parent = new JPanel();
    		//manually modify panels
    		 parent.setLayout(null);
    		//panel for number buttons
    		JPanel numbers = new JPanel();
    		numbers.setSize(150,150);
    		numbers.setLocation(10,70);
    		numbers.add(one);
    		numbers.add(two);
    		numbers.add(three);
    		numbers.add(four);
    		numbers.add(five);
    		numbers.add(six);
    		numbers.add(seven);
    		numbers.add(eight);
    		numbers.add(nine);
    		numbers.add(zero);
    		numbers.add(equal);
    		numbers.add(zero);
    		numbers.add(fullstop);
    		numbers.add(plusmin);
    		//add content to main parent panel
    		parent.add(numbers);
     
    		//panel for math button
    		JPanel math = new JPanel();
    		math.setSize(100,190);
    		math.setLocation(160,50);
    		math.add(C);
    		math.add(CE);
    		math.add(plus);
    		math.add(minus);
    		math.add(multiply);
    		math.add(divide);
    		math.add(equal);
    		math.add(sqrt);
    		math.add(percent);
    		//add content to main parent panel
    		parent.add(math);
     
    		//calling new backspace button to position it separately on the parent panel
    		backsp=new JButton("Backspace");
    		backsp.setSize(120,20);
    		backsp.setLocation(20,50);
    		parent.add(backsp);
     
     
    		//calling new text field to position it separately on the parent panel
    		textfl=new JTextField(); 
    		textfl.setSize(200,20);
    		textfl.setLocation(20,20);
    		//add content to main parent panel
    		parent.add(textfl);
     
     
    		//calling menu separately to position it on the parent panel
    		menu = new JMenuBar();
    		menu.setSize(370,20);
    		menu.setLocation(0,0);
    		JMenu help = new JMenu("help");
    		JMenu about = new JMenu("about");
    		menu.add(help);
    		menu.add(about);
    		//add content to main parent panel
    		parent.add(menu);
     
    		//returns the main parent panel with all the sub panels and contents
    		return parent;
    	}
     
    	//creating frame separately
    	private static void frameCreation(){
     
    		JFrame title = new JFrame("calculator");
     
    		NewCalculator calcul = new NewCalculator();
     
    		title.setContentPane(calcul.NewCalculator());
     
    		title.setSize(370, 320);
    		title.setVisible(true);
    	}
     
     
    public static void main(String[] args){
    	//Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
    	 SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 frameCreation();
             }
         });
     
     
     
    	//end of main
    }
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    	//identifies the actions of each separate components even if all the component actions 
    	//are the same
    		String action = e.getActionCommand();
     
    		if(action.equals("=")){
    		//gets the text in text filed and convert it to integer value to be used to calculate.
    			numtwo=Integer.parseInt(textfl.getText());
     
    			//deletes the old text displayed on the text filed to display the answer (eclipse).
    			textfl.setText("");
    			Answer();
    			DisplayResults();
     
    		}
     
    		//if the C button is clicked
    		else if(action.equals("C")){
    			//clears the text on the text field.
    			textfl.setText("");
    		}
    		else if(action.equals("CE")){
    			//clears the text on the text field.
    			textfl.setText("");
    		}
     
    		else if(action.equals("+")){
    			operator='+';
    			numone=Integer.parseInt(textfl.getText());
    			operator=e.getActionCommand().charAt(0);
    			textfl.setText("");
     
    		}
    		else if(action.equals("-")){
    			operator='-';
    			numone=Integer.parseInt(textfl.getText());
    			operator=e.getActionCommand().charAt(0);
    			textfl.setText("");
     
    		}
    		else if(action.equals("*")){
    			operator='*';
    			numone=Integer.parseInt(textfl.getText());
    			operator=e.getActionCommand().charAt(0);
    			textfl.setText("");
     
    		}
    		else if(action.equals("/")){
    			operator='/';
    			numone=Integer.parseInt(textfl.getText());
    			operator=e.getActionCommand().charAt(0);
    			textfl.setText("");
     
    		}
    		else if(action.equals("%")){
    			operator='/';
    			numone=Integer.parseInt(textfl.getText());
    			operator=e.getActionCommand().charAt(0);
    			textfl.setText("");
     
    		}
    		else
    		{
     
    		textfl.setText(textfl.getText()+action);	
     
    		}
     
    	//actionPerformed end	
    	}
     
    //if one of the math buttons are clicked then one of the mathematical calculation will be 
    	//calculated.
    	public void Answer(){
     
    		switch(operator)
    		{
    		case '+':
    			answer=numone+numtwo;
    		break;
     
    		case '-':
    		answer=numone-numtwo;
    		break;
     
    		case '*':
    			answer=numone*numtwo;
    		return;
     
    		case '/':
    		//if user attempts to divide by 0, then an error message is displayed.
    		if(numtwo==0){
     
    			textfl.setText("cannot divide by 0");
    			textfl.setText("");
    			}
    			else{
    			//calculates answer
    			answer=numone/numtwo;
    			}
    		break;
    		default:
    			answer=0;
    		}
     
    		}
     
    		//displays the answer.
    		public void DisplayResults(){
     
    			textfl.setText(String.valueOf(answer));
    		}
     
     
    	}
    //public class end

    Also what is the code I need to use to do the percentage and square root calculation in the calculation. Cos I am not sure about the sign I am required to use to do the calculation.

    thanks in advance.

    --- Update ---

    the if statement is where the code to make the clear button (C and CE) work.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: why does the clear sign not work on my calculator?

    Probably because you forgot to add action listeners to them.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why does the clear sign not work on my calculator?

    thanks. but how do I do square root and percentage maths on this?

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

    Default Re: why does the clear sign not work on my calculator?

    Quote Originally Posted by gopster01 View Post
    how do I do square root and percentage maths on this?
    sqrt(double) of java.lang.Math and for example 10 + 50% means exactly what you are thinking: add the 50% of the value at that moment (10).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Add a dollar sign to a number
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 4th, 2013, 08:31 PM
  2. [SOLVED] Making a Stop sign
    By remedys in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 3rd, 2012, 11:04 PM
  3. xmpp single sign on sso
    By richie1985 in forum Java Networking
    Replies: 2
    Last Post: April 15th, 2011, 11:39 AM
  4. sign XML with WS-Security
    By splendes in forum Java SE APIs
    Replies: 1
    Last Post: October 6th, 2010, 08:49 AM
  5. How do I privately sign my applet?
    By AlphaWhelp in forum Java Theory & Questions
    Replies: 1
    Last Post: December 16th, 2009, 09:42 AM

Tags for this Thread