Results 1 to 10 of 10
- 12-14-2010, 01:22 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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 ??
- 12-14-2010, 01:29 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-14-2010, 01:34 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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 ??
- 12-14-2010, 01:52 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 12-14-2010, 01:57 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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,
- 12-14-2010, 02:03 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
I'm really not concentrated right now , so I'll check it tomorrow
I'll reply after I checked it
thanks
- 12-14-2010, 09:12 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 12-14-2010, 05:36 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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
- 12-15-2010, 02:56 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Java Code:public void actionPerformed(ActionEvent e) { // Some implementation case 15: lastOperator = "+"; processOperator("+"); break; // Rest of the code. }
- 12-15-2010, 02:56 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Think about that what the above code segment does.
Similar Threads
-
Help making a calculator
By karl_koch_89 in forum New To JavaReplies: 3Last Post: 08-27-2010, 02:47 AM -
TextArea in jtable
By anilkumar_vist in forum Advanced JavaReplies: 0Last Post: 01-26-2010, 05:27 AM -
Output to TextArea
By ljk8950 in forum AWT / SwingReplies: 4Last Post: 08-03-2008, 09:59 AM -
TextArea Bug?
By Soda in forum New To JavaReplies: 2Last Post: 12-07-2007, 12:37 PM -
textarea
By ubuntu in forum AWT / SwingReplies: 4Last Post: 05-12-2007, 09:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks