-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeVariable.java
1822 lines (1643 loc) · 60.3 KB
/
ThreeVariable.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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package kmapSolver;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
/*******************************************************
Dominick Lamastra
3 variable Kmap Solver using Quine McCluskey algorithm
completed January 2021
finalized August 2021
*******************************************************/
public class ThreeVariable extends JFrame implements ActionListener {
private JLabel[] binaryValues; // holds the format of the binary representations of the numbers in the kmap
private JButton[] bits; // holds the buttons to change the state of the binary values
private static JLabel[] map; // displays the state of the current minterm on the kmap
private JButton[] subcubes; // keeps track of what loops have already been created
// different arrays to hold the borders for each respective length of bits captured in the border and location of bits
private static JLabel[] oneWordFullBorders;
private static JLabel[] twoWordFullBorders;
private static JLabel[] fourWordFullBorders;
private JLabel[] eightWordBorder;
private static JLabel[][] twoWordWrapAroundBorders;
private static JLabel[][] fourWordWrapAroundBorders;
private static boolean[] addBorder; // determines which bits are valid for receiving a border
// allows the user to select bits to potentially receive a border
private JCheckBox[] boxBits;
private boolean[] addToKmap;
private int[][] locations; // the locations of where each minterm is placed on the map
private JButton compute; // starts the process for a border to be placed on a specific set of minterms in the kmap
private JButton fillKmap; // the user clicks this button if they want the kmap to be filled with 1's
private JButton reset; // the user clicks this button if they want the kmap to be filled with 0's
private JButton showMinimal; // the user clicks this button if they want to see a minimum sum of products
private JButton change; // the user clicks this button if they want to change to a 3 variable Kmap
private JButton help; // takes the user to a page with more information
private JButton about; // takes the user to the credits page
private JButton clearLoops; // the user clicks this button to set all loops to invisible
private static JLabel status; // shows the user if they have solved the correct kmap
private static JTextArea showCanonical; // the area where the Canonical expression is shown
private static JTextArea showKmap; // the area where the user's inputted loops are shown
private static JTextArea showMinimalSoP; // the area where the minimum sum of products are shown
private static String canonical; // the canonical expression
private static String kmap; // what loops the user has created
private static String minimalSoP = ""; // the minimum sum of products for a given canonical expression
private static JLabel minimalSoPLabel; // labels the minimal sum of products
private int[] countArray = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Used to implement the menu where the user selects the boxes
private static String[] MintermArray = {"0","1","X"}; // possible symbols that can be added to the kmap
private Color[] colors = {new Color(0, 51, 102), new Color(255, 153, 102), new Color(153, 150, 204), new Color(0, 0, 0),
new Color(102, 153, 0), new Color(102, 255, 153), new Color(0, 102, 0), new Color(51, 51, 0),
new Color(204, 153, 0), new Color(100, 100, 100), new Color(255, 153, 51), new Color(255, 51, 0),
new Color(153, 0, 51), new Color(255, 51, 204), new Color(102, 0, 102), new Color(102, 102, 153)}; // used to give every value in the kmap a new color
private JPanel pane = new JPanel(); // JPanel used for the scroll bar
private static JScrollPane minScrollPane;
// constructor
public ThreeVariable() {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
setBounds(x - 350, 0, 700, (int)(dimension.getHeight() * 0.9));
// set up the JFrame and pane
setTitle("Kmap Solver with Three Variables");
pane.setLayout(null);
this.pane.setBounds(0, 0, 1000, 800);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// add a scroll pane
this.pane.setPreferredSize(new Dimension(1000, 1000));
final JScrollPane scrollp = new JScrollPane(this.pane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(scrollp);
setResizable(false);
binaryValues = new JLabel[16]; // the bits 0000-1111
bits = new JButton[16]; // a 0, 1, or X
map = new JLabel[8]; // the 0's and 1's in the kmap
boxBits = new JCheckBox[8]; // the boxes below the Kmap
addToKmap = new boolean[8]; // to check if the value is checked or not in Box bits
addBorder = new boolean[8]; // to check if the value should be added to a border
subcubes = new JButton[8]; // keeps track of what loops have already been created
locations = new int[8][4]; // the locations of the map
// Used to label the columns of the Kmap
JLabel[] greyCodeHorizontal = new JLabel[4];
greyCodeHorizontal[0] = new JLabel("00");
greyCodeHorizontal[1] = new JLabel("01");
greyCodeHorizontal[2] = new JLabel("11");
greyCodeHorizontal[3] = new JLabel("10");
// Used to label the rows of the Kmap
JLabel[] greyCodeVertical = new JLabel[2];
greyCodeVertical[0] = new JLabel("0");
greyCodeVertical[1] = new JLabel("1");
// Used to show that AB is labeling the rows
JLabel rowLabel = new JLabel("A");
Dimension size = rowLabel.getPreferredSize();
rowLabel.setBounds(172, 185, size.width, size.height);
pane.add(rowLabel);
// Used to show that CD is labeling the columns
JLabel columnLabel = new JLabel("BC");
size = columnLabel.getPreferredSize();
columnLabel.setBounds(200, 150, size.width, size.height);
pane.add(columnLabel);
// Array declarations of the borders that will have 4 visible lines
oneWordFullBorders = new JLabel[8];
twoWordFullBorders = new JLabel[10];
fourWordFullBorders = new JLabel[5];
eightWordBorder = new JLabel[1];
// Array declarations of the borders that will have 3 visible lines
twoWordWrapAroundBorders = new JLabel[2][2];
fourWordWrapAroundBorders = new JLabel[1][2];
// Adds the grey code that is along the rows and columns of the Kmap
for (int i = 0; i < 4; i++) {
greyCodeHorizontal[i].setBounds(250 + (i * 50), 160, 20, 20);
if (i == 0 || i == 1) {
greyCodeVertical[i].setBounds(200, 210 + (i * 60), 20, 20);
pane.add(greyCodeVertical[i]);
}
pane.add(greyCodeHorizontal[i]);
}
int constant = 15;
int downMapCount = 1;
int sideMapCount = 0;
int downCheckboxCount = 0;
int sideCheckboxCount = 0;
// loops through 1-8 and initializes beginning arrays
for (int i = 0; i < 8; i++) {
addToKmap[i] = false; // initially, nothing is checked
// sets up the subcube buttons
subcubes[i] = new JButton("");
subcubes[i].setBounds(530, 115 + constant, 100, 15);
subcubes[i].addActionListener(this);
pane.add(subcubes[i]);
subcubes[i].setVisible(true);
constant = constant + 30;
// adds the four digit values in binary
binaryValues[i] = new JLabel(threeDigits(i));
binaryValues[i].setBounds(50 , (120 + (i * 30)), 35, 35);
pane.add(binaryValues[i]);
//adds the 1 bit values in the kmap
bits[i] = new JButton(MintermArray[0]);
bits[i].setBounds(85, (130 + (i * 30)), 50, 15);
bits[i].addActionListener(this);
pane.add(bits[i]);
// adds 0's in all locations of the map
map[i] = new JLabel ("0");
map[i].setVisible(true);
// count where the kmap should place each value
if (i % 4 == 0) {
downMapCount++;
}
int down = 60;
// set the proper location for each value in the kmap
locations[i][0] = 250 + (sideMapCount * 50);
locations[i][1] = 90 + (down * downMapCount);
locations[i][2] = 20;
locations[i][3] = 20;
map[i].setBounds(locations[i][0], locations[i][1], locations[i][2], locations[i][3]);
sideMapCount++;
if (sideMapCount == 4) {
sideMapCount = 0;
}
pane.add(map[i]);
// create a checkbox for each value in the kmap
boxBits[i] = new JCheckBox(threeDigits(i));
boxBits[i].addActionListener(this);
if (i % 4 == 0){
boxBits[i].setBounds(150, locations[i][1] - sideCheckboxCount + 250, 75, 15);
} else if (i % 4 == 1) {
boxBits[i].setBounds(250, locations[i][1] - sideCheckboxCount + 250, 75, 15);
} else if (i % 4 == 2) {
boxBits[i].setBounds(450, locations[i][1] - sideCheckboxCount + 250, 75, 15);
} else if (i % 4 == 3) {
boxBits[i].setBounds(350, locations[i][1] - sideCheckboxCount + 250, 75, 15);
}
downCheckboxCount++;
if (downCheckboxCount == 4) {
downCheckboxCount = 0;
sideCheckboxCount = sideCheckboxCount + 10;
}
pane.add(boxBits[i]);
}
// swaps to put the map elements in the correct order
for (int i = 0; i < map.length; i++) {
JLabel tmp = new JLabel();
if (i % 4 == 2) {
tmp = map[i];
map[i] = map[i + 1];
map[i + 1] = tmp;
}
}
// Labels the Kmap
JLabel kmapHeader = new JLabel("Kmap");
kmapHeader.setBounds(310, 135, 60, 15);
kmapHeader.setVisible(true);
pane.add(kmapHeader);
// Labels the truth table
JLabel tableLabel = new JLabel("ABC");
tableLabel.setBounds(50, 115, 60, 15);
tableLabel.setVisible(true);
pane.add(tableLabel);
// Creates a button the user will use to switch variable length
change = new JButton("Switch to 4 Variable");
size = change.getPreferredSize();
change.setBounds(240, 100, size.width, size.height);
change.addActionListener(this);
pane.add(change);
// Creates a button the user will use to get more information
help = new JButton("Help");
help.setBounds(495, 50, 80, 15);
help.addActionListener(this);
pane.add(help);
// Creates a button the user will use to get more information
about = new JButton("About");
about.setBounds(575, 50, 80, 15);
about.addActionListener(this);
pane.add(about);
// Creates a button the user will press when they have a group of binary values to group
compute = new JButton("Generate Box Around Selected Minterms");
compute.setBounds(190, 430, 275, 15);
compute.addActionListener(this);
pane.add(compute);
// the user clicks this button to set all loops to invisible
clearLoops = new JButton("Clear Loops");
clearLoops.setBounds(5, 50, 130, 15);
clearLoops.addActionListener(this);
pane.add(clearLoops);
// Creates a button that the user can press to solve the kmap
showMinimal = new JButton("Show Minimal");
showMinimal.setBounds(135, 50, 130, 15);
showMinimal.addActionListener(this);
pane.add(showMinimal);
// Creates a button the user will press when to fill kmap values with 1's
fillKmap = new JButton("Fill Kmap");
fillKmap.setBounds(395, 50, 100, 15);
fillKmap.addActionListener(this);
pane.add(fillKmap);
// Creates a button the user will press when to fill kmap values with 0's
reset = new JButton("Reset Table");
reset.setBounds(265, 50, 130, 15);
reset.addActionListener(this);
pane.add(reset);
// Creates a Label to label the subcubes
JLabel subcubeLabel = new JLabel("subcubes:");
subcubeLabel.setBounds(530, 115, 130, 15);
subcubeLabel.setVisible(true);
pane.add(subcubeLabel);
// Creates a space the user can see the canonical expression
JLabel canonicalLabel = new JLabel("Canonical:");
canonicalLabel.setBounds(20, 610, 100, 20);
canonicalLabel.setVisible(true);
pane.add(canonicalLabel);
showCanonical = new JTextArea("", 2, 1);
showCanonical.setBounds(20, 630, 600, 30);
showCanonical.setLineWrap(true);
showCanonical.setWrapStyleWord(true);
showCanonical.setEditable(false);
pane.add(showCanonical);
showCanonical.setVisible(true);
canonical = "";
// Creates a space where the user can see their current sum of products
JLabel KmapLabel = new JLabel("Kmap:");
KmapLabel.setBounds(20, 660, 100, 20);
pane.add(KmapLabel);
KmapLabel.setVisible(true);
showKmap = new JTextArea("", 2, 1);
showKmap.setBounds(20, 680, 600, 30);
showKmap.setLineWrap(true);
showKmap.setWrapStyleWord(true);
showKmap.setEditable(false);
pane.add(showKmap);
showKmap.setVisible(true);
kmap = "";
// Creates a space where the user can see their current sum of products
status = new JLabel("(Correct!)");
status.setBounds(80, 660, 100, 20);
status.setForeground(new Color(0, 115, 0));
pane.add(status);
status.setVisible(true);
// Creates a space the user can see the minimal SoP if they want
minimalSoPLabel = new JLabel("Minimum Sum Of Products: ");
minimalSoPLabel.setBounds(20, 710, 300, 20);
pane.add(minimalSoPLabel);
minimalSoPLabel.setVisible(false);
minScrollPane = new JScrollPane();
minScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
minScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
showMinimalSoP = new JTextArea("0", 30, 30);
showMinimalSoP.setBounds(20, 730, 600, 30);
showMinimalSoP.setLineWrap(true);
showMinimalSoP.setWrapStyleWord(true);
showMinimalSoP.setEditable(false);
minScrollPane.setBounds(20, 730, 600, 100);
minScrollPane.getViewport().setBackground(Color.WHITE);
minScrollPane.getViewport().add(showMinimalSoP);
pane.add(minScrollPane);
repaint();
minScrollPane.setVisible(false);
minimalSoP = "";
//build the list of possible groupings
buildEight();
buildFour();
buildTwo();
buildOne();
grid();
}
// takes an event based on a specific action
public void actionPerformed(ActionEvent e) {
// the frame switches to a three variable kmap
if (e.getSource() == change) {
FourVariable f = new FourVariable();
f.setVisible(true);
this.setVisible(false);
}
// the help frame opens
if (e.getSource() == help) {
Help h = new Help();
h.setVisible(true);
this.setVisible(false);
}
// the about frame opens
if (e.getSource() == about) {
About a = new About();
a.setVisible(true);
this.setVisible(false);
}
// all the users loops created will be cleared
if (e.getSource() == clearLoops) {
fullPlacement(oneWordFullBorders, 0, "clear");
fullPlacement(twoWordFullBorders, 0, "clear");
fullPlacement(fourWordFullBorders, 0, "clear");
fullPlacement(eightWordBorder, 0, "clear");
wrapAroundPlacement(twoWordWrapAroundBorders, 0, "clear");
wrapAroundPlacement(fourWordWrapAroundBorders, 0, "clear");
setStatus();
}
// runs the methods that add boxes into the kmap if specified by the user
if (e.getSource() == compute) {
int count = 0; // used to count how many values will be grouped in the kmap
for (int i = 0; i < 8; i++) {
map[i].setOpaque(false);
map[i].repaint();
if (addToKmap[i] == true) {
addToKmap[i] = false;
boxBits[i].setSelected(false);
if (map[i].getText() != MintermArray[0]) {
count++;
addBorder[i] = true;
} else {
count = 20;
}
}
}
// call method that checks if the boxes checked by the user are valid to be boxed
if (count == 1) {
checkOne("");
setStatus();
} else if (count == 2) {
checkTwo("");
setStatus();
} else if (count == 4) {
checkFour("");
setStatus();
} else if (count == 8) {
checkEight("1");
setStatus();
}
// reset the variables used for the next use
count = 0;
for (int i = 0; i < 8; i++) {
addBorder[i] = false;
}
}
// fills kmap with 1's
if (e.getSource() == fillKmap) {
for (int i = 0; i < 8; i++) {
boxBits[i].setSelected(false);
addToKmap[i] = false;
bits[i].setText(MintermArray[1]);
map[i].setText(MintermArray[1]);
countArray[i] = 1;
}
canonicalExpression();
findMinimalSoP();
setStatus();
}
// resets kmap to 0's
if (e.getSource() == reset) {
for (int i = 0; i < 8; i++) {
map[i].setOpaque(false);
boxBits[i].setSelected(false);
addToKmap[i] = false;
bits[i].setText(MintermArray[0]);
map[i].setText(MintermArray[0]);
map[i].repaint();
countArray[i] = 0;
canonical = "";
kmap = "";
minimalSoP = "";
showMinimalSoP.setText(minimalSoP);
showCanonical.setText(canonical);
showKmap.setText(kmap);
}
}
// show a minimal sum of products to the user using Quine McCluskey algorithm
if (e.getSource() == showMinimal) {
if (showMinimal.getText().equals("Show Minimal")) {
showMinimal.setText("Hide Minimal");
} else {
showMinimal.setText("Show Minimal");
}
if (minimalSoPLabel.isVisible() == true && showMinimalSoP.isVisible() == true) {
minimalSoPLabel.setVisible(false);
showMinimalSoP.setVisible(false);
minScrollPane.setVisible(false);
} else {
minimalSoPLabel.setVisible(true);
showMinimalSoP.setVisible(true);
minScrollPane.setVisible(true);
}
repaint();
}
// Gives the correct output on the Kmap if the user is creating the Kmap
for (int i = 0; i < 8; i++) {
// if the user clicked the subcube, then the user can remove the loop from the kmap
if (e.getSource() == subcubes[i]) {
String str = subcubes[i].getText();
int count = 0;
for (int j = 0; j < str.length(); j++) {
if (Character.isLetter(str.charAt(j))) {
count++;
}
}
// will check the loop for a match in the correct length literal method
if (count == 3) {
checkOne(str);
setStatus();
} else if (count == 2) {
checkTwo(str);
setStatus();
} else if (count == 1) {
checkFour(str);
setStatus();
} else if (str == "1") {
checkEight(str);
setStatus();
}
}
// changes the bits in the kmap based on the action by the user
if (e.getSource() == bits[i]) {
// Changes the respective but from a 0 to a 1
if (countArray[i] % 3 == 0) {
bits[i].setText(MintermArray[1]);
map[i].setText(MintermArray[1]);
countArray[i]++;
// Changes the respective bits from a 1 to a X
} else if (countArray[i] % 3 == 1) {
bits[i].setText(MintermArray[2]);
map[i].setText(MintermArray[2]);
countArray[i]++;
// Changes the respective bits from a X to a 0
} else if (countArray[i] % 3 == 2) {
bits[i].setText(MintermArray[0]);
map[i].setText(MintermArray[0]);
countArray[i]++;
}
canonicalExpression();
findMinimalSoP();
setStatus();
}
}
// if the user checked a box to solve the Kmap
for (int i = 0; i < 8; i++) {
if (e.getSource() == boxBits[i]) {
if (addToKmap[i] == false) {
map[i].setOpaque(true);
map[i].setBackground(Color.YELLOW);
map[i].repaint();
addToKmap[i] = true;
} else {
addToKmap[i] = false;
map[i].setOpaque(false);
map[i].repaint();
}
}
}
}
public void findMinimalSoP() {
// arrays used to hold the minterm values for each step of the algorithm
String zeros[] = new String[8];
String ones[] = new String[8];
String twos[] = new String[8];
String threes[] = new String[8];
String totalMinterms[] = new String[8];
String oneGroupedMinterms[] = new String[8];
String twoGroupedMintermsA[] = new String[25];
String twoGroupedMintermsB[] = new String[25];
String twoGroupedMintermsC[] = new String[25];
String fourGroupedMintermsAB[] = new String[25];
String fourGroupedMintermsBC[] = new String[25];
// reset the minimal sum of products variables
minimalSoP = "";
showMinimalSoP.setText(minimalSoP);
// fills totalMinterms with original minterms defined in the kmap
int count = 0;
String[] values = new String[8];
for (int i = 0; i < 8; i++) {
if (map[i].getText() == MintermArray[1]) {
values[i] = binaryValues[i].getText();
totalMinterms[count] = Integer.toString(i);
count++;
} else if (map[i].getText() == MintermArray[2]) {
values[i] = binaryValues[i].getText();
totalMinterms[count] = Integer.toString(i) + "dc";
count++;
}
}
// if all numbers in kmap are 0 then the minimum is 0
if (count == 0) {
minimalSoP = "0";
showMinimalSoP.setText(minimalSoP);
// if all numbers in kmap are 1 then the minimum is 1
} else if (count == 8) {
minimalSoP = "1";
showMinimalSoP.setText(minimalSoP);
} else {
String[] start = new String[8]; // used to find the starting literals
count = 0;
for (int i = 0; i < 8; i++) { // find starting literals
oneGroupedMinterms[i] = binaryValues[i].getText(); // used to collect the 4 bit binary values
if (map[i].getText() != MintermArray[0]) {
if (i == 0) {
zeros[0] = values[i];
start[count] = values[i];
count++;
} else if (i == 1 || i == 2 || i == 4) {
ones[i] = values[i];
start[count] = values[i];
count++;
} else if (i == 3 || i == 5 || i == 6) {
twos[i] = values[i];
start[count] = values[i];
count++;
} else if (i == 7) {
threes[i] = values[i];
start[count] = values[i];
count++;
}
}
}
// performs the first round of the quine McCluskey method and updates start, combines possible minterms to make products of length 2
String[] oneLiteralA = round1(zeros, ones, twoGroupedMintermsA, oneGroupedMinterms);
String[] oneLiteralB = round1(ones, twos, twoGroupedMintermsB, oneGroupedMinterms);
String[] oneLiteralC = round1(twos, threes, twoGroupedMintermsC, oneGroupedMinterms);
// have start only be including the minterms that are used in the problem and correctly rearranges it to match the map positioning
int i = 0;
while(start[i] != null) {
if (zeros[0] != null && start[i].charAt(0) == zeros[0].charAt(0) && start[i].charAt(1) == zeros[0].charAt(1) && start[i].charAt(2) == zeros[0].charAt(2)) {
start[i] = zeros[0];
} else if (threes[7] != null && start[i].charAt(0) == threes[7].charAt(0) && start[i].charAt(1) == threes[7].charAt(1) && start[i].charAt(2) == threes[7].charAt(2)) {
start[i] = threes[7];
} else {
for (int j = 0; j < 8; j++) {
if (ones[j] != null) {
if (start[i].charAt(0) == ones[j].charAt(0) && start[i].charAt(1) == ones[j].charAt(1) && start[i].charAt(2) == ones[j].charAt(2)) {
start[i] = ones[j];
}
} else if (twos[j] != null) {
if (start[i].charAt(0) == twos[j].charAt(0) && start[i].charAt(1) == twos[j].charAt(1) && start[i].charAt(2) == twos[j].charAt(2)) {
start[i] = twos[j];
}
}
}
}
i++;
}
// combines the literals from round 1 into larger arrays
String[] oneLiteralAB = arrayMaker(oneLiteralA, oneLiteralB) ;
String[] oneLiteralBC = arrayMaker(oneLiteralB, oneLiteralC) ;
String[] oneLiteralABC = arrayMaker(oneLiteralAB, oneLiteralBC);
// combines the minterm arrays from round 1 into larger arrays
String[] twoGroupedMintermsAB = arrayMaker(twoGroupedMintermsA, twoGroupedMintermsB);
String[] twoGroupedMintermsBC = arrayMaker(twoGroupedMintermsB, twoGroupedMintermsC);
String[] twoGroupedMintermsABC = arrayMaker(twoGroupedMintermsAB, twoGroupedMintermsC);
// performs round 2 of the Quine McCluskey method, combines possible minterms to make products of length 4
String[] twoLiteralAB = round2(oneLiteralA, oneLiteralB, fourGroupedMintermsAB, oneLiteralAB, twoGroupedMintermsAB);
String[] twoLiteralBC = round2(oneLiteralB, oneLiteralC, fourGroupedMintermsBC, oneLiteralBC, twoGroupedMintermsBC);
// redo twoLiteralAB, twoLiteralBC, and twoLiteralCD to add the possible checks (denoted as c's)
oneLiteralAB = arrayMaker(oneLiteralA, oneLiteralB);
oneLiteralBC = arrayMaker(oneLiteralB, oneLiteralC);
// combines the literals from round 2 into larger arrays
String[] twoLiteralABC = arrayMaker(twoLiteralAB, twoLiteralBC);
// combines the minterms from round 2 into larger arrays
String[] fourGroupedMintermsABC = arrayMaker(fourGroupedMintermsAB, fourGroupedMintermsBC);
// redo oneLiteralABC, and oneLiteralBCD to add the possible checks (denoted as c's)
oneLiteralABC = arrayMaker(oneLiteralAB, oneLiteralBC);
// uses the total arrays to make one final array
String[] total = arrayMaker (totalMinterms, twoGroupedMintermsABC);
total = arrayMaker (total, fourGroupedMintermsABC);
// uses the literal arrays to make one final array
String[] literals = arrayMaker(oneLiteralABC, twoLiteralABC);
literals = arrayMaker(start, literals);
// uses the final arrays to find the point implicants
findPI(total, literals, totalMinterms);
}
}
// takes a binary value and makes it three digits wide
public static String threeDigits(int num) {
String digits = Integer.toBinaryString(num);
if (num < 2) {
return ("00" + digits);
} else if (num < 4) {
return ("0" + digits);
}
return (digits);
}
// add a string to a minimum sum of products expression
public static void addMinimalSoPExpression (String s) {
if (minimalSoP.contains(s)) {
return;
}
if (minimalSoP == null || minimalSoP.isEmpty() == true) {
minimalSoP = s;
} else {
minimalSoP = (minimalSoP + "+" + s);
}
//showMinimalSoP.setText(minimalSoP);
}
public static void setStatus() {
kmap = showKmap.getText();
ArrayList<String> literalsKmap = new ArrayList<String>();
String[] kmapArr = kmap.split("\\+", 16);
String tmp = "";
for (int i = 0; i < kmapArr.length; i++) {
if (kmapArr[i] != null) {
literalsKmap.add(kmapArr[i]);
}
}
Collections.sort(literalsKmap);
for (int i = 0; i < literalsKmap.size(); i++) {
tmp = tmp + (literalsKmap.get(i)) + "+";
}
if (tmp.length() > 0) {
tmp = tmp.substring(0, tmp.length() - 1);
}
String lines[] = showMinimalSoP.getText().split("\r?\n");
for (int i = 0; i < lines.length; i++) {
if (lines[i].equals(tmp)) {
if (status.getText() == "(Incorrect)") {
status.setText("(Correct!)");
status.setForeground(new Color(0, 115, 0));
return;
}
}
}
if (lines[0].equals("0")) {
if (status.getText() == "(Incorrect)") {
status.setText("(Correct!)");
status.setForeground(new Color(0, 115, 0));
return;
}
}
if (status.getText() == "(Correct!)") {
status.setText("(Incorrect)");
status.setForeground(new Color(115, 0, 0));
}
kmap = "";
}
public String[] round1 (String[] x, String[] y, String[] z, String[] m) { // x is comparing with y
String[] newArray = new String[25]; // the array that will be returned with the literals
int endCount = 0;
String endValue = "";
int count = 0;
// compare x and y and if 3 characters match then they can be combined
for (int r = 0; r < 8; r++) {
for (int t = 0; t < 8; t++) {
if (x[r] != null && y[t] != null) {
for (int i = 0; i < 3; i++) {
if (x[r].charAt(i) == y[t].charAt(i)) {
count++;
}
}
}
// create the new combined literal
if (count == 2) {
for (int j = 0; j < 3; j++) {
if (x[r].charAt(j) == y[t].charAt(j)) {
endValue = endValue + x[r].charAt(j);
} else {
endValue = endValue + "_";
}
}
// puts place holders in between the minterms
String s = "";
for (int i = 0; i < 8; i++) {
if ((x[r].charAt(0) == m[i].charAt(0)) && (x[r].charAt(1) == m[i].charAt(1)) &&
(x[r].charAt(2) == m[i].charAt(2))) {
s = s + Integer.toString(i) + "x";
}
if ((y[t].charAt(0) == m[i].charAt(0)) && (y[t].charAt(1) == m[i].charAt(1)) &&
(y[t].charAt(2) == m[i].charAt(2))) {
s = s + Integer.toString(i) + "x";
}
}
// removes the final x
s = s.substring(0, s.length() - 1);
// adds c to the end of the string, meaning that it has already been checked
x[r] = x[r] + "c";
y[t] = y[t] + "c";
// add the new minterm to the array that holds combined minterms
for (int i = 0; i < z.length; i++) {
if (z[i] == null) {
z[i] = s;
s = "";
break;
}
}
// adds the new literal with the _ to the proper array
newArray[endCount] = endValue;
endValue = "";
endCount++;
}
count = 0;
}
}
return (newArray);
}
public String[] round2 (String[] x, String[] y, String[] z, String[] m, String[] w) { // x is comparing with y
// the array that will be returned with the literals
String[] newArray = new String[25];
int endCount = 0;
String endValue = "";
int count = 0;
int lengthX = 0;
int lengthY = 0;
int lengthM = 0;
for (int i = 0; i < x.length; i++) {
if (x[i] == null) {
break;
}
lengthX++;
}
for (int i = 0; i < y.length; i++) {
if (y[i] == null) {
break;
}
lengthY++;
}
for (int i = 0; i < m.length; i++) {
if (m[i] == null) {
break;
}
lengthM++;
}
// compare x and y and if 3 characters match then they can be combined
for (int r = 0; r < lengthX; r++) {
for (int t = 0; t < lengthY; t++) {
if (x[r] != null && y[t] != null) {
for (int i = 0; i < 3; i++) {
if (x[r].length() > 0 && y[t].length() > 0 && x[r].charAt(i) == y[t].charAt(i)) {
count++;
}
}
}
if (count == 2) {
String s = "";
// puts placeholders in between the minterms
for (int i = 0; i < lengthM; i++) {
if ((x[r].charAt(0) == m[i].charAt(0)) && (x[r].charAt(1) == m[i].charAt(1)) &&
(x[r].charAt(2) == m[i].charAt(2))) {
s = s + w[i] + "x";
}
if ((y[t].charAt(0) == m[i].charAt(0)) && (y[t].charAt(1) == m[i].charAt(1)) &&
(y[t].charAt(2) == m[i].charAt(2))) {
s = s + w[i] + "x";
}
}
if (s.length() > 0) {
s = s.substring(0, s.length() - 1);
}
// create the new combined literal
for (int j = 0; j < 3; j++) {
if (x[r].charAt(j) == y[t].charAt(j)) {
endValue = endValue + x[r].charAt(j);
} else {
endValue = endValue + "_";
}
}
// adds c to the end of the string, meaning that it has already been checked
x[r] = x[r] + "c";
y[t] = y[t] + "c";
// initialize variables to check for duplicates
count = 0;
int c = 0;
boolean check = false;
// add the new minterm to the array that holds combined minterms
for (int i = 0; i < z.length; i++) {
if (z[i] != null) {
c++;
}
}
// checks for duplicates
for (int i = 0; i < newArray.length; i++) {
if (newArray[i] != null) {
if (newArray[i].equals(endValue)) {
check = true;
break;
}
}
}
// add the new minterm to the array that holds combined minterms
if (check == false) {
z[endCount] = s;
newArray[endCount] = endValue;
endCount++;
}
}
endValue = "";
count = 0;
}
}
return (newArray);
}
// combines two arrays into one array
public String[] arrayMaker (String[] q, String[] p) {
int qCount = 0;
int pCount = 0;
for (int i = 0; i < q.length; i++) {
if (q[i] != null) {
qCount++;
}
}
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
pCount++;
}
}
String[] n = new String[pCount + qCount];
for (int i = 0; i < qCount; i++) {
n[i] = q[i];
}
int subtract = 0;
for (int i = 0; i < pCount; i++) {
boolean add = true;
for (int j = 0; j < qCount; j++) {
if (p[i] == q[j]) {
add = false;
subtract++;
}
}
if (add == true) {
n[i + qCount - subtract] = p[i];
}
}
return (n);
}
public void findPI (String[] t, String[] l, String[] m) {
String realM[] = new String[m.length]; // used to remove the don't cares
int count = 0;
// removes the don't cares from the total count
for (int i = 0; i < m.length; i++) {
if (m[i] != null) {
if (m[i].endsWith("dc") == false) {
realM[count] = m[i];
count++;
}
}
}
// if there are no non-don't cares, then there are automatically no prime implicants
if (count == 0) {
return;
}
count = 0; // reset count to 0
// initial variables
boolean dc = false;
String str = "";
String tmp = "";
String optionsL[] = new String[64];
String optionsT[][] = new String[32][16];
String placers[] = {"A", "B", "C", "D"};
// add an x to the end of the totals and find your options for optionsL and optionsT
for (int i = 0; i < l.length; i++) {
if (l[i].length() == 3 && t[i].endsWith("dc") == false) {
t[i] = t[i] + "x";
int countT = 0;
for (int k = 0; k < t[i].length(); k++) {
if (Character.isDigit(t[i].charAt(k))) {
tmp = tmp + t[i].charAt(k);
} else if (t[i].charAt(k) == 'x') {