Translate
Monday, 27 March 2017
Monday, 6 March 2017
Thursday, 2 March 2017
java ascpn
01 | import java.awt.*; |
002 | import javax.swing.*; |
003 | import java.awt.event.*; |
004 |
005 | public class Calculator extends JFrame implements ActionListener { |
006 | |
007 | JPanel[] row = new JPanel[ 5 ]; |
008 | JButton[] button = new JButton[ 19 ]; |
009 | String[] buttonString = { "7" , "8" , "9" , "+" , |
010 | "4" , "5" , "6" , "-" , |
011 | "1" , "2" , "3" , "*" , |
012 | "." , "/" , "C" , "√" , |
013 | "+/-" , "=" , "0" }; |
014 | int [] dimW = { 300 , 45 , 100 , 90 }; |
015 | int [] dimH = { 35 , 40 }; |
016 | Dimension displayDimension = new Dimension(dimW[ 0 ], dimH[ 0 ]); |
017 | Dimension regularDimension = new Dimension(dimW[ 1 ], dimH[ 1 ]); |
018 | Dimension rColumnDimension = new Dimension(dimW[ 2 ], dimH[ 1 ]); |
019 | Dimension zeroButDimension = new Dimension(dimW[ 3 ], dimH[ 1 ]); |
020 | boolean [] function = new boolean [ 4 ]; |
021 | double [] temporary = { 0 , 0 }; |
022 | JTextArea display = new JTextArea( 1 , 20 ); |
023 | Font font = new Font( "Times new Roman" , Font.BOLD, 14 ); |
024 | |
025 | Calculator() { |
026 | super ( "Calculator" ); |
027 | setDesign(); |
028 | setSize( 380 , 250 ); |
029 | setResizable( false ); |
030 | setDefaultCloseOperation(EXIT_ON_CLOSE); |
031 | GridLayout grid = new GridLayout( 5 , 5 ); |
032 | setLayout(grid); |
033 | |
034 | for ( int i = 0 ; i < 4 ; i++) |
035 | function[i] = false ; |
036 | |
037 | FlowLayout f1 = new FlowLayout(FlowLayout.CENTER); |
038 | FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1 , 1 ); |
039 | for ( int i = 0 ; i < 5 ; i++) |
040 | row[i] = new JPanel(); |
041 | row[ 0 ].setLayout(f1); |
042 | for ( int i = 1 ; i < 5 ; i++) |
043 | row[i].setLayout(f2); |
044 | |
045 | for ( int i = 0 ; i < 19 ; i++) { |
046 | button[i] = new JButton(); |
047 | button[i].setText(buttonString[i]); |
048 | button[i].setFont(font); |
049 | button[i].addActionListener( this ); |
050 | } |
051 | |
052 | display.setFont(font); |
053 | display.setEditable( false ); |
054 | display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); |
055 | display.setPreferredSize(displayDimension); |
056 | for ( int i = 0 ; i < 14 ; i++) |
057 | button[i].setPreferredSize(regularDimension); |
058 | for ( int i = 14 ; i < 18 ; i++) |
059 | button[i].setPreferredSize(rColumnDimension); |
060 | button[ 18 ].setPreferredSize(zeroButDimension); |
061 | |
062 | row[ 0 ].add(display); |
063 | add(row[ 0 ]); |
064 | |
065 | for ( int i = 0 ; i < 4 ; i++) |
066 | row[ 1 ].add(button[i]); |
067 | row[ 1 ].add(button[ 14 ]); |
068 | add(row[ 1 ]); |
069 | |
070 | for ( int i = 4 ; i < 8 ; i++) |
071 | row[ 2 ].add(button[i]); |
072 | row[ 2 ].add(button[ 15 ]); |
073 | add(row[ 2 ]); |
074 | |
075 | for ( int i = 8 ; i < 12 ; i++) |
076 | row[ 3 ].add(button[i]); |
077 | row[ 3 ].add(button[ 16 ]); |
078 | add(row[ 3 ]); |
079 | |
080 | row[ 4 ].add(button[ 18 ]); |
081 | for ( int i = 12 ; i < 14 ; i++) |
082 | row[ 4 ].add(button[i]); |
083 | row[ 4 ].add(button[ 17 ]); |
084 | add(row[ 4 ]); |
085 | |
086 | setVisible( true ); |
087 | } |
088 | |
089 | public void clear() { |
090 | try { |
091 | display.setText( "" ); |
092 | for ( int i = 0 ; i < 4 ; i++) |
093 | function[i] = false ; |
094 | for ( int i = 0 ; i < 2 ; i++) |
095 | temporary[i] = 0 ; |
096 | } catch (NullPointerException e) { |
097 | } |
098 | } |
099 | |
100 | public void getSqrt() { |
101 | try { |
102 | double value = Math.sqrt(Double.parseDouble(display.getText())); |
103 | display.setText(Double.toString(value)); |
104 | } catch (NumberFormatException e) { |
105 | } |
106 | } |
107 | |
108 | public void getPosNeg() { |
109 | try { |
110 | double value = Double.parseDouble(display.getText()); |
111 | if (value != 0 ) { |
112 | value = value * (- 1 ); |
113 | display.setText(Double.toString(value)); |
114 | } |
115 | else { |
116 | } |
117 | } catch (NumberFormatException e) { |
118 | } |
119 | } |
120 | |
121 | public void getResult() { |
122 | double result = 0 ; |
123 | temporary[ 1 ] = Double.parseDouble(display.getText()); |
124 | String temp0 = Double.toString(temporary[ 0 ]); |
125 | String temp1 = Double.toString(temporary[ 1 ]); |
126 | try { |
127 | if (temp0.contains( "-" )) { |
128 | String[] temp00 = temp0.split( "-" , 2 ); |
129 | temporary[ 0 ] = (Double.parseDouble(temp00[ 1 ]) * - 1 ); |
130 | } |
131 | if (temp1.contains( "-" )) { |
132 | String[] temp11 = temp1.split( "-" , 2 ); |
133 | temporary[ 1 ] = (Double.parseDouble(temp11[ 1 ]) * - 1 ); |
134 | } |
135 | } catch (ArrayIndexOutOfBoundsException e) { |
136 | } |
137 | try { |
138 | if (function[ 2 ] == true ) |
139 | result = temporary[ 0 ] * temporary[ 1 ]; |
140 | else if (function[ 3 ] == true ) |
141 | result = temporary[ 0 ] / temporary[ 1 ]; |
142 | else if (function[ 0 ] == true ) |
143 | result = temporary[ 0 ] + temporary[ 1 ]; |
144 | else if (function[ 1 ] == true ) |
145 | result = temporary[ 0 ] - temporary[ 1 ]; |
146 | display.setText(Double.toString(result)); |
147 | for ( int i = 0 ; i < 4 ; i++) |
148 | function[i] = false ; |
149 | } catch (NumberFormatException e) { |
150 | } |
151 | } |
152 | |
153 | public final void setDesign() { |
154 | try { |
155 | UIManager.setLookAndFeel( |
156 | "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ); |
157 | } catch (Exception e) { |
158 | } |
159 | } |
160 | |
161 | @Override |
162 | public void actionPerformed(ActionEvent ae) { |
163 | if (ae.getSource() == button[ 0 ]) |
164 | display.append( "7" ); |
165 | if (ae.getSource() == button[ 1 ]) |
166 | display.append( "8" ); |
167 | if (ae.getSource() == button[ 2 ]) |
168 | display.append( "9" ); |
169 | if (ae.getSource() == button[ 3 ]) { |
170 | //add function[0] |
171 | temporary[ 0 ] = Double.parseDouble(display.getText()); |
172 | function[ 0 ] = true ; |
173 | display.setText( "" ); |
174 | } |
175 | if (ae.getSource() == button[ 4 ]) |
176 | display.append( "4" ); |
177 | if (ae.getSource() == button[ 5 ]) |
178 | display.append( "5" ); |
179 | if (ae.getSource() == button[ 6 ]) |
180 | display.append( "6" ); |
181 | if (ae.getSource() == button[ 7 ]) { |
182 | //subtract function[1] |
183 | temporary[ 0 ] = Double.parseDouble(display.getText()); |
184 | function[ 1 ] = true ; |
185 | display.setText( "" ); |
186 | } |
187 | if (ae.getSource() == button[ 8 ]) |
188 | display.append( "1" ); |
189 | if (ae.getSource() == button[ 9 ]) |
190 | display.append( "2" ); |
191 | if (ae.getSource() == button[ 10 ]) |
192 | display.append( "3" ); |
193 | if (ae.getSource() == button[ 11 ]) { |
194 | //multiply function[2] |
195 | temporary[ 0 ] = Double.parseDouble(display.getText()); |
196 | function[ 2 ] = true ; |
197 | display.setText( "" ); |
198 | } |
199 | if (ae.getSource() == button[ 12 ]) |
200 | display.append( "." ); |
201 | if (ae.getSource() == button[ 13 ]) { |
202 | //divide function[3] |
203 | temporary[ 0 ] = Double.parseDouble(display.getText()); |
204 | function[ 3 ] = true ; |
205 | display.setText( "" ); |
206 | } |
207 | if (ae.getSource() == button[ 14 ]) |
208 | clear(); |
209 | if (ae.getSource() == button[ 15 ]) |
210 | getSqrt(); |
211 | if (ae.getSource() == button[ 16 ]) |
212 | getPosNeg(); |
213 | if (ae.getSource() == button[ 17 ]) |
214 | getResult(); |
215 | if (ae.getSource() == button[ 18 ]) |
216 | display.append( "0" ); |
217 | } |
218 | |
219 | public static void main(String[] arguments) { |
220 | Calculator c = new Calculator(); |
221 | } |
222 | } |
Subscribe to:
Posts (Atom)