-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyCalc.java
872 lines (772 loc) · 29.5 KB
/
MyCalc.java
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
import java.awt.*;
import java.awt.event.*;
import javax.swing.* ;
import javax.swing.JOptionPane ;
import java.io.*;
public class MyCalc extends JFrame
implements ActionListener {
//jar cf jar-file input-file(s);
private JButton bOne, bTwo, bThree, bFour, bFive,
bSix, bSeven, bEight, bNine, bZero, bExit,
bMult, bDiv, bSub, bPlus, bCEnt, bCAll, bEquals,
bPlusMinus, bExp, bMPlus, bMMinus,
bFact, bOneDivX, bSqrt, bDecPt ;
private JTextField enterBox ;
private JPanel textPanel, exitPanel, buttonPanel, functionPanel ;
private Container c ;
private Font font;
private String firstNum = new String(""), secondNum = new String(""), tempStr ;
private boolean myDiag = false, result = true ,
firstNumSet = false, secondNumSet = false,
pendingOp = false ;
private double aNum, dNum1 = 0.0, dNum2 = 0.0 , answer = 0.0,
tempD = 0.0 , minusOne = -1.0 ;
private double dArray[] = new double[ 10 ] ;
private int opCode = -1, tempInt = 0, tempInt2 = 0,
dArrayPtr = 0, pendingOpCode = -1 ;
MyCalc() {
super( "Calculator" );
setup() ;
setSize( 350, 300 );
setLocation( 200, 200 ) ;
show();
}
private void setup() {
c = getContentPane() ;
enterBox = new JTextField( 15 ) ;
enterBox.setText( "..........." );
enterBox.setEditable( false );
enterBox.setBackground( Color.white );
enterBox.setHorizontalAlignment( JTextField.RIGHT );
enterBox.addKeyListener(
new KeyAdapter() {
public void keyPressed ( KeyEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result +
"this is from the enterbox keylistener." );
result = false ;
enterBox.setText( "" );
}
}
}
);
textPanel = new JPanel() ;
textPanel.add(enterBox ) ;
c.add( textPanel , BorderLayout.NORTH ) ;
buttonPanel = new JPanel() ;
c.add( buttonPanel , BorderLayout.CENTER ) ;
bZero = new JButton( "0" ) ;
bZero.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bOne = new JButton( "1" ) ;
bTwo = new JButton( "2" ) ;
bThree = new JButton( "3" ) ;
bFour = new JButton( "4" ) ;
bFive = new JButton( "5" ) ;
bSix = new JButton( "6" ) ;
bSeven = new JButton( "7" ) ;
bEight = new JButton( "8" ) ;
bNine = new JButton( "9" ) ;
bExit = new JButton( "Exit" ) ;
bMult = new JButton( "*" ) ;
bMult.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bDiv = new JButton( "/" ) ;
bDiv.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bSub = new JButton( "-" ) ;
bSub.setFont( new Font("Sanserif", Font.BOLD, 24 ) );
bPlus = new JButton( "+" ) ;
bPlusMinus = new JButton( "+/-" ) ;
bPlusMinus.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bEquals = new JButton( "=" ) ;
bEquals.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bPlus = new JButton( "+" ) ;
bPlus.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bExp = new JButton( "Pow" ) ;
bExp.setFont( new Font("Sanserif", Font.BOLD, 12 ) );
bOneDivX = new JButton( "1/x" ) ;
bOneDivX.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bSqrt = new JButton( "SQRT" ) ;
bSqrt.setFont( new Font("Sanserif", Font.BOLD, 12 ) );
bDecPt = new JButton( "." ) ;
bDecPt.setFont( new Font("Sanserif", Font.BOLD, 22 ) );
bZero.addActionListener( this );
bOne.addActionListener( this );
bTwo.addActionListener( this );
bThree.addActionListener( this );
bFour.addActionListener( this );
bFive.addActionListener( this );
bSix.addActionListener( this );
bSeven.addActionListener( this );
bEight.addActionListener( this );
bNine.addActionListener( this );
bExit.addActionListener( this );
bMult.addActionListener( this );
bDiv.addActionListener( this );
bSub.addActionListener( this );
bPlus.addActionListener( this );
bPlusMinus.addActionListener( this );
bEquals.addActionListener( this );
bExp.addActionListener( this );
bOneDivX.addActionListener( this );
bSqrt.addActionListener( this );
bDecPt.addActionListener( this );
buttonPanel.add( bSeven ) ;
buttonPanel.add( bEight ) ;
buttonPanel.add( bNine ) ;
buttonPanel.add( bMult ) ;
buttonPanel.add( bFour ) ;
buttonPanel.add( bFive ) ;
buttonPanel.add( bSix ) ;
buttonPanel.add( bDiv ) ;
buttonPanel.add( bOne ) ;
buttonPanel.add( bTwo ) ;
buttonPanel.add( bThree ) ;
buttonPanel.add( bSub ) ;
buttonPanel.add( bPlusMinus ) ;
buttonPanel.add( bZero ) ;
buttonPanel.add( bDecPt ) ;
buttonPanel.add( bPlus ) ;
buttonPanel.add( bExp );
buttonPanel.add( bOneDivX );
buttonPanel.add( bSqrt );
buttonPanel.add( bEquals ) ;
buttonPanel.setLayout( new GridLayout( 6, 5, 5, 5 ) );
functionPanel = new JPanel() ;
c.add( functionPanel , BorderLayout.EAST ) ;
functionPanel.setLayout( new GridLayout( 6, 1, 5, 3 ) );
bCEnt = new JButton( "CE" ) ;
bCEnt.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bCAll = new JButton( "CA" ) ;
bCAll.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bMPlus = new JButton( "M+" ) ;
bMPlus.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bMMinus = new JButton( "M-" ) ;
bMMinus.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bFact = new JButton( "! n" ) ;
bFact.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
functionPanel.add( bCEnt ) ;
functionPanel.add( bCAll );
functionPanel.add( bMPlus );
functionPanel.add( bMMinus );
functionPanel.add( bFact );
bCEnt.addActionListener( this );
bCEnt.setBackground( Color.blue ) ;
bCEnt.setForeground( Color.white );
bCAll.addActionListener( this );
bCAll.setBackground( Color.blue ) ;
bCAll.setForeground( Color.white );
bMPlus.addActionListener( this );
bMPlus.setBackground( Color.blue ) ;
bMPlus.setForeground( Color.white );
bMMinus.addActionListener( this );
bMMinus.setBackground( Color.blue ) ;
bMMinus.setForeground( Color.white );
bFact.addActionListener( this );
bFact.setBackground( Color.blue ) ;
bFact.setForeground( Color.white );
exitPanel = new JPanel() ;
c.add( exitPanel , BorderLayout.SOUTH ) ;
bExit = new JButton( "Exit" ) ;
bExit.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bExit.setBackground( Color.red ) ;
bExit.setForeground( Color.white );
// bAbout = new JButton( "About" ) ;
// bAbout.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
// bAbout.setBackground( Color.darkGray ) ;
// bAbout.setForeground( Color.white );
exitPanel.add( bExit ) ;
// exitPanel.add( bAbout ) ;
bExit.addActionListener( this );
// bAbout.addActionListener( this );
}
public void actionPerformed( ActionEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result );
result = false ;
enterBox.setText( "" );
}
if ( !pendingOp && opCode == 99 ) {
enterBox.setText( "" );
MyPrint( "The value of opCode is " + opCode );
pendingOpCode = -1 ;
firstNumSet = false ;
firstNum = "" ;
}
if ( e.getSource() == bDecPt ) {
MyPrint( "The decimal point button was pressed." );
enterBox.setText( enterBox.getText() + "." ) ;
}
else if ( e.getSource() == bZero ) {
MyPrint( "The zero button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "0" ) ;
}
else if ( e.getSource() == bOne ) {
MyPrint( "The one button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "1" ) ;
}
else if ( e.getSource() == bTwo ) {
MyPrint( "The two button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "2" ) ;
}
else if ( e.getSource() == bThree ) {
MyPrint( "The Three button was pressed. And pendingOp is " + pendingOp);
enterBox.setText( enterBox.getText() + "3" ) ;
}
else if ( e.getSource() == bFour ) {
MyPrint( "The Four button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "4" ) ;
}
else if ( e.getSource() == bFive ) {
MyPrint( "The Five button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "5" ) ;
}
else if ( e.getSource() == bSix ) {
MyPrint( "The Six button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "6" ) ;
}
else if ( e.getSource() == bSeven ) {
MyPrint( "The Seven button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "7" ) ;
}
else if ( e.getSource() == bEight ) {
MyPrint( "The Eight button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "8" ) ;
}
else if ( e.getSource() == bNine ) {
MyPrint( "The Nine button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "9" ) ;
}
else if ( e.getSource() == bExit ) {
dispose();
}
else if ( e.getSource() == bMult ) {
MyPrint( "The Mult button was pressed. And pendingOp is " + pendingOp );
multOp() ;
}
else if ( e.getSource() == bDiv ) {
MyPrint( "The Div button was pressed. And pendingOp is " + pendingOp );
divOp() ;
}
else if ( e.getSource() == bSub ) {
MyPrint( "The Sub button was pressed. And pendingOp is " + pendingOp );
subOp() ;
}
else if ( e.getSource() == bPlus ) {
MyPrint( "The Plus button was pressed. And pendingOp is " + pendingOp );
plusOp() ;
}
else if ( e.getSource() == bCEnt ) {
MyPrint( "The CEnt button was pressed." );
enterBox.setText( "" ) ;
}
else if ( e.getSource() == bSqrt ) {
MyPrint( "The bSqrt button was pressed. And pendingOp is " + pendingOp );
MySqrt() ;
}
else if ( e.getSource() == bOneDivX ) {
MyPrint( "The bOneDivX button was pressed. And pendingOp is " + pendingOp );
OneDivX() ;
}
else if ( e.getSource() == bCAll ) {
MyPrint( "The CAll button was pressed." );
enterBox.setText( "" ) ;
firstNum = "" ;
firstNumSet = false ;
secondNum = "" ;
answer= 0.0 ;
opCode = -1 ;
}
else if ( e.getSource() == bEquals ) {
MyPrint( "The Equals button was pressed. And pendingOp is " + pendingOp +
" The opCode value is " + opCode );
if ( pendingOp ) {
opCodeMethod() ;
opCode = -1 ;
}
secondNum = enterBox.getText() ;
opCodeMethod() ;
firstNum = "" + answer ;
result = true ;
}
else if ( e.getSource() == bPlusMinus ) {
MyPrint( "The PlusMinus button was pressed." );
opCode = -1 ;
PlusMinus() ;
}
else if ( e.getSource() == bExp ) {
MyPrint( "The Power button was pressed." );
secondNumSet = false ;
powerOp() ;
}
else if ( e.getSource() == bMPlus ) {
MyPrint( "1a: The MPlus button was pressed. pendingOp =s " + pendingOp);
if ( dArrayPtr < 10 ) { /** array hold 10 entries */
if ( !pendingOp ) {
if ( opCode == 99 ) {
//enterBox.setText( "" );
MyPrint( "1b: The value of opCode is " + opCode +
" firstNum =s " + firstNum + " answer =s " + answer );
pendingOpCode = -1 ;
firstNumSet = true ;
firstNum = "" + answer ;
dArray [ dArrayPtr ] =
Double.parseDouble( "" + answer ) ;
dArrayPtr++ ;
enterBox.setText( firstNum ) ;
MyPrint( "1c: The value of dArrayPtr is " + dArrayPtr );
}
else {
/** This code is entered if the =s button was not just pressed */
firstNum = "" + enterBox.getText( );
firstNumSet = true ;
dArray [ dArrayPtr ] =
Double.parseDouble( "" + firstNum ) ;
dArrayPtr++ ;
MyPrint( "1d: The value of dArrayPtr is " + dArrayPtr );
}
}
else if ( !enterBox.getText().equals( "" ) ) {
dArray [ dArrayPtr ] =
Double.parseDouble( enterBox.getText() ) ;
dArrayPtr++ ;
firstNumSet = true ;
firstNum = "" + answer ;
MyPrint( "1d: The value of dArrayPtr is " + dArrayPtr );
}
else {
JOptionPane.showMessageDialog( this, "A number must be entered befor clicking the M+ button" ,
"Attempted to store a blank number.",
JOptionPane.ERROR_MESSAGE );
}
}
result = true ;
}
else if ( e.getSource() == bMMinus ) {
/** This code retrieves values from the array stack */
MyPrint( "The MMinus button was pressed." ) ;
if ( dArrayPtr > 0 ) {
enterBox.setText( "" + dArray [ dArrayPtr-1 ] ) ;
firstNum = "" + answer ;
answer = dArray [ dArrayPtr-1 ] ;
dArrayPtr-- ;
MyPrint( "1a: The retrieved value is " + answer ) ;
}
else {
enterBox.setText( "" ) ;
MyPrint( "1b: There are no more values to retrieve." ) ;
JOptionPane.showMessageDialog( this, "There are no more values to retrieve using M-." ,
"No More Values.",
JOptionPane.ERROR_MESSAGE );
}
if ( pendingOp ) {
secondNumSet = true ;
secondNum = "" + answer ;
}
else {
MyPrint( "1a- MMinus: The value of answer is " + answer );
secondNum = "" ;
opCode = -1 ;
dNum2 = 0.0 ;
dNum1 = answer ;
secondNumSet = false ;
firstNum = "" + answer ;
firstNumSet = true ;
pendingOp = false ;
enterBox.setText( "" + answer ) ;
}
}
else if ( e.getSource() == bFact ) {
MyPrint( "1a- bFact(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( opCode == 99 ) {
tempInt = ( int ) answer ;
}
else if ( !enterBox.getText().equals( "" ) ) {
MyPrint( "1b- bFact(): The value of enterBox.getText() is " + enterBox.getText() );
tempInt = Integer.parseInt( enterBox.getText() ) ;
MyPrint( "1c- bFact(): The value of tempInt is " + tempInt );
}
else if ( firstNumSet ) {
MyPrint( "1d- bFact() The value of firstNum is " + firstNum ) ;
tempInt = (int)Double.parseDouble( firstNum ) ;
MyPrint( "1e- bFact(): The value of tempInt is " + tempInt );
}
MyPrint( "1f- bFact(): The value of tempInt is " + tempInt );
tempInt2 = 1 ;
for( int ii = 1 ; ii < tempInt + 1; ii++ ) {
tempInt2 *= ii ;
}
enterBox.setText( "" + tempInt2 ) ;
answer = (double)tempInt2 ;
MyPrint( "1g- factorial: The value of answer is " + answer );
secondNum = "" ;
opCode = -1 ;
dNum2 = 0.0 ;
dNum1 = answer ;
secondNumSet = false ;
firstNum = "" + answer ;
firstNumSet = true ;
pendingOp = false ;
}
}
private boolean checkFirstNum( ) {
boolean retCode = false ;
if ( !firstNumSet ) {
if ( !enterBox.getText().equals( "" ) ) {
firstNum = enterBox.getText() ;
firstNumSet = true ;
result = true ;
retCode = true ;
MyPrint( "1a- In checkFirstNum( ) -- firstNum =s " + firstNum);
}
}
else if ( !firstNum.equals( "" ) ) {
result = true ;
retCode = true ;
MyPrint( "1b- In checkFirstNum( ) firstNumSet is " + firstNumSet );
}
return retCode ;
}
private void setResults() {
enterBox.setText( "" + answer );
firstNum = "" + answer ;
firstNumSet = true ;
result = true ;
pendingOp = false ;
}
private void opCodeMethod() {
int currentOpCode = -1 ;
MyPrint( "In opCodeMethod() with an opCode of " + opCode );
if ( pendingOp ) {
currentOpCode = pendingOpCode ;
pendingOp = false ;
}
else {
currentOpCode = opCode ;
}
switch ( currentOpCode ) {
case 0 :
multOp() ;
break ;
case 1 :
divOp() ;
break ;
case 2 :
MyPrint( "opCodeMethod(): opCode =s " + currentOpCode ) ;
subOp() ;
break ;
case 3 :
MyPrint( "opCodeMethod(): opCode =s " + currentOpCode ) ;
plusOp() ;
break ;
case 4 :
MyPrint( "Calling PowerOp method." );
MyPrint( "Case 4: the value of firstNum is " +
firstNum + " the value of secondNum is " +
secondNum ) ;
secondNumSet = true ;
powerOp() ;
break ;
case 5 :
break ;
default :
setResults() ;
firstNumSet = true ;
secondNum = "0.0" ;
dNum2 = 0.0 ;
pendingOp = false ;
opCode = 99 ;
MyPrint( "Case default: firstNum =s " + firstNum);
break ;
}
if ( !( opCode == 99 ) )
opCode = -1 ;
}
private void powerOp() {
MyPrint( "1a- powerOp: The value of firstNum is " + firstNum +
" The value of secondNum is " + secondNum
+
" The value of answer is " + answer );
if ( opCode == 99 ) {
setResults() ;
}
if ( firstNumSet ) {
if ( pendingOp ) {
opCodeMethod() ;
pendingOp = false ;
secondNumSet = false ;
}
if ( secondNumSet ) {
MyPrint( "1b- powerOp: The value of firstNum is " + firstNum +
" The value of secondNum is " + secondNum
+
" The value of answer is " + answer );
secondNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.pow( dNum1 , dNum2 ) ;
setResults() ;
MyPrint( "1c- powerOp: The value of answer is " + answer +
" The value of opCode is " + opCode );
}
}
else {
if ( checkFirstNum() ) {
MyPrint( "1d- powerOp: The value of answer is " + answer +
" The value of opCode is " + opCode );
pendingOpCode = 4 ;
}
}
opCode = 4 ;
}
private void multOp() {
if ( pendingOp ) {
MyPrint( "1a- multOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
opCodeMethod() ;
opCode = -1 ;
}
if ( !firstNumSet ) {
pendingOp = true ;
if ( opCode == 99 ) {
firstNum = "" + answer ;
firstNumSet = true ;
MyPrint( "1b- multOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
}
if ( checkFirstNum() ) {
opCode = 0 ;
MyPrint( "1c- multOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
pendingOpCode = 0 ;
}
}
else if ( opCode == 0 ) {
pendingOp = false ;
secondNum = enterBox.getText() ;
pendingOp = true ;
MyPrint( "2a- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
if ( secondNum.equals( "" ) ) {
secondNum = "1.0" ;
MyPrint( "2b- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
}
MyPrint( "3a- multOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 * dNum2 ;
setResults() ;
MyPrint( "3b- multOp(): opCode is " + opCode + " The value of answer is " + answer );
dNum1 = answer ;
secondNum = "0.0" ;
secondNumSet = true ;
}
else {
pendingOp = true ;
pendingOpCode = 0 ;
opCode = 0 ;
result = true ;
MyPrint( "3a- multOp(): opCode is " + opCode + " The value of answer is " + answer );
}
}
private void divOp() {
MyPrint( "1a- divOp(): opCode is " + opCode + " firstNumSet =s " + firstNumSet );
if ( pendingOp ) {
MyPrint( "1aa- divOp(): opCode is " + opCode );
opCodeMethod() ;
opCode = -1 ;
}
if ( !firstNumSet ) {
pendingOp = true ;
if ( opCode == 99 ) {
firstNum = "" + answer ;
firstNumSet = true ;
MyPrint( "1b- divOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
}
if ( checkFirstNum() ) {
MyPrint( "1c- divOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
pendingOpCode = 1 ;
opCode = 1 ;
}
}
else if ( firstNumSet ) {
if ( pendingOp ) {
opCodeMethod() ;
opCode = 1 ;
}
secondNum = enterBox.getText() ;
MyPrint( "2a- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
if ( secondNum.equals( "" ) || opCode < 0 ) {
MyPrint( "2b- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
convertNumsToDouble() ;
if (dNum2 != 0.0 ) {
answer = dNum1 / dNum2 ;
setResults() ;
MyPrint( "2- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
JOptionPane.showMessageDialog( this, "The number " +
dNum2 + " cannot be used as a denometer" ,
"Attempted to Divide by Zero",
JOptionPane.ERROR_MESSAGE );
firstNumSet = false ;
firstNum = "" ;
opCode = -1 ;
}
}
opCode = 1 ;
}
result = true ;
}
private void subOp() {
if ( pendingOp ) {
MyPrint( "subOp() 1a: In pendingOp code. The opcode value is " + opCode ) ;
opCodeMethod() ;
opCode = 2 ;
pendingOp = true ;
pendingOpCode = 2 ;
}
else if( !firstNumSet ) {
firstNum = enterBox.getText() ;
pendingOp = true ;
pendingOpCode = 2 ;
opCode = 2 ;
firstNumSet = true ;
result = true ;
MyPrint( "1b- subOp()-checkFirstNum(): opCode is " + opCode + " The value of answer is " + answer );
}
else if ( opCode == 2 ) {
pendingOp = false ;
if ( secondNum.equals( "0.0" ) ) {
firstNum = "" + answer ;
}
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- subOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 - dNum2 ;
setResults() ;
MyPrint( "2b- subOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "0.0" ;
opCode = -1 ;
secondNumSet = true ;
}
else {
pendingOp = true ;
pendingOpCode = 2 ;
opCode = 2 ;
result = true ;
MyPrint( "3a- subOp(): opCode is " + opCode + " The value of answer is " + answer );
}
}
private void plusOp() {
if ( pendingOp ) {
MyPrint( "subOp() 1a: In pendingOp code. The opcode value is " + opCode ) ;
opCodeMethod() ;
opCode = 3 ;
pendingOp = true ;
pendingOpCode = 3 ;
}
else if( !firstNumSet ) {
firstNum = enterBox.getText() ;
pendingOp = true ;
pendingOpCode = 3 ;
opCode = 3 ;
firstNumSet = true ;
result = true ;
MyPrint( "1b- plusOp()-checkFirstNum(): opCode is " + opCode + " The value of answer is " + answer );
}
else if ( opCode == 3 ) {
pendingOp = false ;
MyPrint( "2aa- plusOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
if ( secondNum.equals( "0.0" ) ) {
firstNum = "" + answer ;
}
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- plusOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 + dNum2 ;
setResults() ;
MyPrint( "2b- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "0.0" ;
opCode = -1 ;
secondNumSet = true ;
}
else {
pendingOp = true ;
pendingOpCode = 3 ;
opCode = 3 ;
result = true ;
MyPrint( "3a- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
}
}
private void convertNumsToDouble() {
if ( !firstNum.equals( "" ) )
dNum1 = Double.parseDouble( firstNum ) ;
if ( !secondNum.equals( "" ) )
dNum2 = Double.parseDouble( secondNum ) ;
MyPrint( "convertNumsToDouble(): The value of dNum1 is " + dNum1 );
MyPrint( "convertNumsToDouble(): The value of dNum2 is " + dNum2 );
}
private void PlusMinus() {
MyPrint( "PlusMinus: The value of enterBox is " + enterBox.getText() );
if ( !enterBox.getText().equals( "" ) ) {
answer = Double.parseDouble( enterBox.getText() ) * minusOne ;
MyPrint( "1a- PlusMinus: The value of answer is " + answer );
setResults() ;
}
else if ( !firstNum.equals( "" ) ) {
answer = Double.parseDouble( firstNum ) * minusOne ;
setResults() ;
MyPrint( "1b- PlusMinus: The value of firstNum is " + firstNum );
}
result = true ;
}
private void MySqrt() {
MyPrint( "1a- MySqrt(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( opCode == 99 ) {
answer = Math.sqrt( answer ) ;
setResults() ;
}
else if ( !enterBox.getText().equals( "" ) ) {
MyPrint( "1b- MySqrt(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( pendingOp )
opCodeMethod() ;
answer = Double.parseDouble( enterBox.getText() ) ;
answer = Math.sqrt( answer ) ;
setResults() ;
}
else if ( !firstNum.equals( "" ) ) {
MyPrint( "1c- MySqrt(): opCode is " + opCode + " firstNum is " + firstNum ) ;
answer = Double.parseDouble( firstNum ) ;
answer = Math.sqrt( answer ) ;
setResults() ;
}
}
private void OneDivX() {
MyPrint( "1a- OneDivX(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( opCode == 99 ) {
answer = ( 1.0 / answer ) ;
setResults() ;
}
else if ( !enterBox.getText().equals( "" ) ) {
answer = Double.parseDouble( enterBox.getText() ) ;
answer = ( 1.0 / answer ) ;
setResults() ;
}
else if ( firstNumSet ) {
answer = Double.parseDouble( firstNum ) ;
answer = ( 1.0 / answer ) ;
setResults() ;
}
}
private void MyPrint( String str ) {
if ( myDiag )
System.out.println( str );
}
/* public static void main( String args[] ) {
new LoginWindow().setVisible(true);
}*/
}