adding decimal point to java calculator

Discussion in 'Software' started by Pure-D, Dec 11, 2005.

  1. Pure-D

    Pure-D Private E-2

    Hi,
    I've just finished my calculator. It's not the most amazing calculator program ever, but it works at a basic level. I've been thinking about adding a decimal function to it, but i believe the way I have coded the calculator it's not possible. I'm not asking for someone to do the code for me, not at all. I just need pointing in the right direction. well here's the code.... (actually any comments on any of the code would be great to).

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class Calculator extends Applet implements ActionListener{
    	
    	TextField Answer;
    	Button Plus;
    	Button Minus;
    	Button Multiply;
    	Button Divide;
    	Button Percent;
    	Button Equals;
    	Button Clear;
    	int Save = 0;
    	char oper = '=';
    	char lastoper = '=';
    	boolean Num = true;
    	
    	public void init() {
    		
    		Answer = new TextField("0",5);
    		Panel AnswerPanel = new Panel();
    		AnswerPanel.add(Answer);
    		Panel NumPanel = new Panel();
    		NumPanel.setLayout(new GridLayout(4,5,2,2));
    		Panel opPanel = new Panel();
    		opPanel.setLayout(new GridLayout(4,4,2,2));
    			
    		for (int i = 0; i <= 9; i++){
    			Button aNum=new Button(Integer.toString(i));
    			aNum.addActionListener (this);
    			NumPanel.add(aNum);
    			add(NumPanel);
    		}
    			
    		Plus=new Button("+");
    		Minus=new Button("-");
    		Multiply=new Button("*");	
    		Divide=new Button("/");		
    		Percent=new Button("%");
    		Equals=new Button("=");		
    		Clear=new Button("C");		
    		Plus.addActionListener(this);			
    		Minus.addActionListener(this);		
    		Multiply.addActionListener(this);
    		Divide.addActionListener(this);		
    		Percent.addActionListener(this);
    		Equals.addActionListener(this);		
    		Clear.addActionListener(this);
    		opPanel.add(Plus);
    		opPanel.add(Minus);
    		opPanel.add(Multiply);
    		opPanel.add(Divide);
    		opPanel.add(Percent);
    		opPanel.add(Equals);
    		opPanel.add(Clear);
    		add(opPanel);
    		
    		Panel All = new Panel();
            All.setLayout(new BorderLayout(6,6));
            All.add(AnswerPanel, BorderLayout.NORTH );
            All.add(opPanel, BorderLayout.WEST );
            All.add(NumPanel , BorderLayout.CENTER);
        	add(All);
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		
    		if (event.getActionCommand() instanceof String) {
    			String string = (String) event.getActionCommand();
    			if (string.charAt(0) >= '0' && string.charAt(0) <= '9') {
    				if (Num)
    					Answer.setText(string);
    				else
    					Answer.setText(Answer.getText() + string);
    				Num = false;
    			}
    			else {
    				if (string.charAt(0) == '-' && Num) {
    					Answer.setText("-");
    					Num = false;
    				}
    				else {
    					oper = string.charAt(0);
    					calculator(Integer.parseInt(Answer.getText()));
    					Num = true;
    				}
    				lastoper = oper;
    			}
    		}
    		return;
    	}
    	public void calculator(int a){
    		
    		switch (oper){
    			case '+':
    			case '-' :
    			case '*':
    			case '/':
    			case '%':
    			Save = a;
    			return;
    		}
    		if (oper == '=')
    			oper = lastoper;
    		switch (oper) {
    			case '+' :
    			Save += a;
    			break;
    			case '-':
    			Save -= a;
    			break;
    			case '*':
    			Save *= a;
    			break;
    			case '/' :
    			Save /= a;
    			break;
    			case '%':
    			Save %= a;
    			break;
    			case 'C':
    			Save = 0;
    			break;
    		}
    		Answer.setText("" + Save);
    	}
    }
    
     
  2. bytegoddess

    bytegoddess Private E-2

    Hello Pure-D,

    You asked for a hint, and I have one for you that won't spoil the surprise too much or take all of the fun out of the learning process, but may get you "unstuck":

    You are currently working with only integer data types... play around in the API, and instantiate your variables as doubles, and then use the toString() or valueOf() functions.

    Best of luck,
    Barbara
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds