Results 1 to 10 of 10
  1. #1
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Question Help making calculator with TextArea

    Hi,

    I'm trying to make a calculator with TextArea ,, it should look like this:




    and with this specifications:

    1-Every different operation should be in a different line of the text area as shown in the snapshot.

    2- If the input is of invalid type, for instance, letters or special characters, the appropriate exception should be thrown.

    3- You should implement an exception class DivisionByZeroException and you throw it and catch when required.

    4- At any exception thrown, an alert window should appear and details the error.

    Note that:
    The button C clears the last digit or symbol entered.
    The button CE clears the last line.
    the button Clear clears all the TextArea.


    I made the design but the buttons are very big !! and the text area is very small !!

    could anyone help me with the whole coding and I'd be very thankful ??

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by matrixcool View Post
    could anyone help me with the whole coding and I'd be very thankful ??
    I don't think anyone wants to write code for you. SO the best option you have is, post whatever you have done so far. And try to ask question specifically. You must try to do it yourself first of all.

  3. #3
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    I tried but it didn't work out :(

    this is my code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Calc extends JFrame implements ActionListener {
    	
    	public static final int MAX_INPUT_LENGTH = 30;
    	public static final int INPUT_MODE = 0;
    	public static final int RESULT_MODE = 1;
    	public static final int ERROR_MODE = 2;
    	int displayMode;
    
    	boolean clearOnNextDigit;
    	double lastNumber;
    	String lastOperator;
    	JButton buttons [];
    	JTextArea area=new JTextArea(17,30);
    	
            
       
        public Calc() {
        	
        	super("Text Area Claculator");
        	setSize(300,300);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	
        	
        	JPanel ButtonsPanel= new JPanel();
        	ButtonsPanel.setLayout(new GridLayout(4,7,4,4));
        	
        	buttons=new JButton [19];
        	
        	for (int i=0; i<=9; i++)
    		{
    			
    			buttons[i] = new JButton(String.valueOf(i));
    		}
    		
    		
    		buttons[10] = new JButton("/");
    		buttons[11] = new JButton("x");
    		buttons[12] = new JButton("-");
    		buttons[13] = new JButton(",");
    		buttons[14] = new JButton("=");
    		buttons[15] = new JButton("+");
    		buttons[16] = new JButton("C");
    		buttons[17] = new JButton("CE");
    		buttons[18] = new JButton("Clear");
    		
    		
    		
    		for(int i=7; i<=9; i++)		{   // First Row
    			ButtonsPanel.add(buttons[i]);
    			
    		}
    		ButtonsPanel.add(buttons[10]);
    		ButtonsPanel.add(area);
    		
    		for(int i=4; i<=6; i++)  // Second Row
    		{
    			ButtonsPanel.add(buttons[i]);
    		}
    		
    		ButtonsPanel.add(buttons[11]);
    		
    		for( int i=1; i<=3; i++) //Third Row
    		{
    			ButtonsPanel.add(buttons[i]);
    		}
    		
    		ButtonsPanel.add(buttons[12]);
    		
    		// Fourth Row
    		
    		ButtonsPanel.add(buttons[0]);
    		ButtonsPanel.add(buttons[13]);
    		ButtonsPanel.add(buttons[14]);
    		ButtonsPanel.add(buttons[15]);
    		ButtonsPanel.add(buttons[16]);
    		ButtonsPanel.add(buttons[17]);
    		ButtonsPanel.add(buttons[18]);
    		
    		
    		add(ButtonsPanel);
    		
    		for (int i=0; i<buttons.length; i++){
    			buttons[i].addActionListener(this);
    		}
    		
    		
        }
        
       	public void setDisplayString(String s){
    		area.setText(s);
    	}
    	
    	public String getDisplayString (){
    		return area.getText();
    	}
    	
    	public void addDigitToDisplay(int digit){
    		if (clearOnNextDigit)
    			setDisplayString("");
    
    		String inputString = getDisplayString();
    		
    		if (inputString.indexOf("0") == 0){
    			inputString = inputString.substring(1);
    		}
    
    		if ((!inputString.equals("0") || digit > 0)  && inputString.length() < MAX_INPUT_LENGTH){
    			setDisplayString(inputString + digit);
    		}
    		
    
    		displayMode = INPUT_MODE;
    		clearOnNextDigit = false;
    		
    	}
    	
    	public void addDecimalPoint(){
    		displayMode = INPUT_MODE;
    
    		if (clearOnNextDigit)
    			setDisplayString("");
    
    		String inputString = getDisplayString();
    	
    		
    		if (inputString.indexOf(".") < 0)
    			setDisplayString(new String(inputString + "."));
    	}
    	
    	public void clearAll()	{
    		setDisplayString("0");
    		lastOperator = "0";
    		lastNumber = 0;
    		displayMode = INPUT_MODE;
    		clearOnNextDigit = true;
    	}
    	
    	public void clearExisting(){
    		setDisplayString("0");
    		clearOnNextDigit = true;
    		displayMode = INPUT_MODE;
    	}
    	
    	public double getNumberInDisplay()	{
    		String input = area.getText();
    		return Double.parseDouble(input);
    	}
    	
    	void processOperator(String op) {
    		if (displayMode != ERROR_MODE)
    		{
    			double numberInDisplay = getNumberInDisplay();
    
    			if (!lastOperator.equals("0"))	
    			{
    				try
    				{
    					double result = processLastOperator();
    					displayResult(result);
    					lastNumber = result;
    				}
    
    				catch (DivideByZeroException e)
    				{
    				}
    			}
    		
    			else
    			{
    				lastNumber = numberInDisplay;
    			}
    			
    			clearOnNextDigit = true;
    			lastOperator = op;
    		}
    	} 
    		
    	public void processEquals(){
    		double result = 0;
    
    		if (displayMode != ERROR_MODE){
    			try			
    			{
    				result = processLastOperator();
    				displayResult(result);
    			}
    			
    			catch (DivideByZeroException e)	{
    				displayError("Cannot divide by zero!");
    			}
    
    			lastOperator = "0";
    		}
    	}
    	
    	public double processLastOperator() throws DivideByZeroException {
    		double result = 0;
    		double numberInDisplay = getNumberInDisplay();
    
    		if (lastOperator.equals("/"))
    		{
    			if (numberInDisplay == 0)
    				throw (new DivideByZeroException());
    
    			result = lastNumber / numberInDisplay;
    		}
    			
    		if (lastOperator.equals("*"))
    			result = lastNumber * numberInDisplay;
    
    		if (lastOperator.equals("-"))
    			result = lastNumber - numberInDisplay;
    
    		if (lastOperator.equals("+"))
    			result = lastNumber + numberInDisplay;
    
    		return result;
    		
    	}
    	
    	public void displayResult(double result){
    		setDisplayString(Double.toString(result));
    		lastNumber = result;
    		displayMode = RESULT_MODE;
    		clearOnNextDigit = true;
    	}
    	
    	public void displayError(String errorMessage){
    		setDisplayString(errorMessage);
    		lastNumber = 0;
    		displayMode = ERROR_MODE;
    		clearOnNextDigit = true;
    	}
    	
    	
    	
    
    	
        
        public void actionPerformed(ActionEvent e){
        	
        	double result = 0;
        	
        	for (int i=0; i<buttons.length; i++)
    		{
    			if(e.getSource() == buttons[i])
    			{
    				switch(i)
    				{
    					case 0:
    						addDigitToDisplay(i);
    						break;
    
    					case 1:
    						addDigitToDisplay(i);
    						break;
    
    					case 2:
    						addDigitToDisplay(i);
    						break;
    
    					case 3:
    						addDigitToDisplay(i);
    						break;
    
    					case 4:
    						addDigitToDisplay(i);
    						break;
    
    					case 5:
    						addDigitToDisplay(i);
    						break;
    
    					case 6:
    						addDigitToDisplay(i);
    						break;
    
    					case 7:
    						addDigitToDisplay(i);
    						break;
    
    					case 8:
    						addDigitToDisplay(i);
    						break;
    
    					case 9:
    						addDigitToDisplay(i);
    						break;
    						
    					case 10:
    						processOperator("/");
    						break;
    
    					case 11:
    						processOperator("*");
    						break;
    
    					case 12:
    						processOperator("-");
    						break;
    
    					case 13:
    						addDecimalPoint();
    						break;
    
    					case 14:
    						processEquals();
    						break;
    
    					case 15:
    						processOperator("+");
    						break;	
    							
    					case 16:
    							if (displayMode != ERROR_MODE){
    							setDisplayString(getDisplayString().substring(0,
    										getDisplayString().length() - 1));
    							
    							if (getDisplayString().length() < 1)
    								setDisplayString("0");
    						}
    						break;
    					
    					case 17:
    						clearExisting();
    						break;
    						
    					case 18:
    						clearAll();
    						break;		
    							
    					
    			}
    		}
    	}			
    
        	
        	
        }
        
        
        
        
        
        
       public class DivideByZeroException extends Exception{
    	public DivideByZeroException()
    	{
    		super();
    	}
    	
    	public DivideByZeroException(String s)
    	{
    		super(s);
    	}
    }
    
        
        
        public static void main(String[] args) {
            
            
            Calc demo =new Calc();
            demo.setVisible(true);
        }
    }

    the major problem is no operation button is working !!

    I'm frustrated right now :(
    anyone please ??

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by matrixcool View Post
    I'm frustrated right now :(
    anyone please ??
    Don't panic when you stuck with something. Think from the simple step you have.

    Here you've an issue with the event seems. So try to debug that code segment. If you really don't know how to debug that put some print statements and see the results.

    Let me check your code.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems to me in any of the event (that is click on any mathematical operation buttons) you've not assign the operator to variable lastOperator. Check it again,

  6. #6
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    I'm really not concentrated right now , so I'll check it tomorrow

    I'll reply after I checked it

    thanks

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, give a try on it and let us know.

    Just try to implement one functionality at a time. Then it's easy to work out, and you can ave a better understanding as well.

  8. #8
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    is this method assigning the variable lastOperator ??
    Java Code:
    public double processLastOperator() throws DivideByZeroException {
    		double result = 0;
    		double numberInDisplay = getNumberInDisplay();
    
    		if (lastOperator.equals("/"))
    		{
    			if (numberInDisplay == 0)
    				throw (new DivideByZeroException());
    
    			result = lastNumber / numberInDisplay;
    		}
    			
    		if (lastOperator.equals("*"))
    			result = lastNumber * numberInDisplay;
    
    		if (lastOperator.equals("-"))
    			result = lastNumber - numberInDisplay;
    
    		if (lastOperator.equals("+"))
    			result = lastNumber + numberInDisplay;
    
    		return result;
    		
    	}

    if it's not , could you show me at least one operation ? I mean how to make a code for one operation ??

    thanks a lot

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    public void actionPerformed(ActionEvent e) {
          
        // Some implementation
    
       case 15:
              lastOperator = "+";
              processOperator("+");
              break;
    
        // Rest of the code.
    }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Help making a calculator
    By karl_koch_89 in forum New To Java
    Replies: 3
    Last Post: 08-27-2010, 02:47 AM
  2. TextArea in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 0
    Last Post: 01-26-2010, 05:27 AM
  3. Output to TextArea
    By ljk8950 in forum AWT / Swing
    Replies: 4
    Last Post: 08-03-2008, 09:59 AM
  4. TextArea Bug?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:37 PM
  5. textarea
    By ubuntu in forum AWT / Swing
    Replies: 4
    Last Post: 05-12-2007, 09:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •