not a number exception and zero division exception
Jan 18, 2014 at 8:46pm UTC
Hello Im very new to all of this but studying computing at university basically I need help with two things ( I have to use Eclipse Helios) the first being to create my own NotANumberException which should be thrown/caught if the user enters a letter and then presses an action key e.g. +,- etc the second thing is to create my own ZeroDivisionException to handle the situation if a user tries to divide by zero. I've been creating a calculator and calculator engine and the above things should be working with the calculator but I think they go into separate classes I'm not too sure. I think this is the code required ( in order first is the calculator second is the engine for the calculator)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class NewCalculator {
// The below code is the basics for constructing buttons to be used in the program as you can see I have created all the buttons you would see in a regular calculator.
JButton button0=new JButton("0" );
JButton button1=new JButton("1" );
JButton button2=new JButton("2" );
JButton button3=new JButton("3" );
JButton button4=new JButton("4" );
JButton button5=new JButton("5" );
JButton button6=new JButton("6" );
JButton button7=new JButton("7" );
JButton button8=new JButton("8" );
JButton button9=new JButton("9" );
JButton buttonPoint = new JButton("." );
JButton buttonEqual=new JButton("=" );
JButton buttonPlus =new JButton("+" );
JButton buttonMinus =new JButton("-" );
JButton buttonDivide=new JButton("/" );
JButton buttonMultiply=new JButton("*" );
JButton buttonOox=new JButton("1/x" );
JButton buttonSqrt=new JButton("sq" );
JButton buttonPercentage=new JButton("&" );
JButton buttonClear=new JButton("C" );
JPanel windowContent = new JPanel();
JTextField displayField = new JTextField(30);
NewCalculator(){
BorderLayout bl = new BorderLayout();
windowContent.setLayout(bl);
windowContent.add("North" ,displayField);
JPanel p1 = new JPanel();
GridLayout gl =new GridLayout(4,3);
p1.setLayout(gl);
p1.add(button1);
p1.add(button2);
p1.add(button3);
p1.add(button4);
p1.add(button5);
p1.add(button6);
p1.add(button7);
p1.add(button8);
p1.add(button9);
p1.add(button0);
p1.add(buttonPoint);
p1.add(buttonEqual);
windowContent.add("Center" ,p1);
JPanel p2 = new JPanel();
GridLayout gl2 =new
GridLayout(4,2);
p2.setLayout(gl2);
p2.add(buttonPlus);
p2.add(buttonMinus);
p2.add(buttonMultiply);
p2.add(buttonDivide);
p2.add(buttonSqrt);
p2.add(buttonOox);
p2.add(buttonPercentage);
p2.add(buttonClear);
windowContent.add("East" ,p2);
JFrame frame = new JFrame("NewCalculator" );
frame.setContentPane(windowContent);
frame.pack();
frame.setVisible(true );
CalculatorEngine calcEngine = new CalculatorEngine(this );
button0.addActionListener(calcEngine);
button1.addActionListener(calcEngine);
button2.addActionListener(calcEngine);
button3.addActionListener(calcEngine);
button4.addActionListener(calcEngine);
button5.addActionListener(calcEngine);
button6.addActionListener(calcEngine);
button7.addActionListener(calcEngine);
button8.addActionListener(calcEngine);
button9.addActionListener(calcEngine);
buttonPoint.addActionListener(calcEngine);
buttonPlus.addActionListener(calcEngine);
buttonMinus.addActionListener(calcEngine);
buttonDivide.addActionListener(calcEngine);
buttonMultiply.addActionListener(calcEngine);
buttonEqual.addActionListener(calcEngine);
buttonSqrt.addActionListener(calcEngine);
buttonOox.addActionListener(calcEngine);
buttonPercentage.addActionListener(calcEngine);
buttonClear.addActionListener(calcEngine);
}
public static void main(String[] args) {
// Instantiate the class Calculator
NewCalculator calc = new NewCalculator();
}
}
then its the engine.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JButton;
public class CalculatorEngine implements ActionListener {
NewCalculator parent; //a reference to Calculator window
char selectedAction = ' ' ; // +, -, /, or *
double currentResult =0;
// Constructor stores the reference to the Calculator
// window in the member variable parent
CalculatorEngine(NewCalculator parent){
this .parent = parent;
}
public void actionPerformed(ActionEvent e){
JButton clickedButton = (JButton) e.getSource();
String
dispFieldText=parent.displayField.getText();
double displayValue=0;
if (!"" .equals (dispFieldText)){
try {
displayValue= Double.parseDouble(dispFieldText);
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
javax.swing.JOptionPane.showConfirmDialog(null, "Please enter a Number" , "Wrong input" , javax.swing.JOptionPane.PLAIN_MESSAGE);
return ;
}
}
Object src = e.getSource ();
if (src == parent.buttonPlus){
selectedAction = '+' ;
currentResult=displayValue;
parent.displayField.setText ("" );
}
else if (src == parent.buttonMinus){
selectedAction = '-' ;
currentResult=displayValue;
parent.displayField.setText ("" );
}
else if (src == parent.buttonDivide){
selectedAction = '/' ;
currentResult=displayValue;
parent.displayField.setText ("" );
}
else if (src == parent.buttonMultiply){
selectedAction = '*' ;
currentResult=displayValue;
parent.displayField.setText ("" );
}
else if (src == parent.buttonEqual){
if (selectedAction=='+' ){
currentResult+=displayValue;
parent.displayField.setText("" +currentResult);
}
}else if (selectedAction=='-' ){
currentResult -=displayValue;
parent.displayField.setText("" +currentResult);
}
else if (selectedAction=='/' ){
currentResult /=displayValue;
parent.displayField.setText("" +currentResult);
}
else if (selectedAction=='*' ){
currentResult *=displayValue;
parent.displayField.setText("" +currentResult);
}
else {
String clickedButtonLabel=clickedButton.getText();
parent.displayField.setText(dispFieldText +clickedButtonLabel);
}
}
}
Jan 18, 2014 at 10:22pm UTC
You may have more luck on a forum where Java is topical.
Topic archived. No new replies allowed.