-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurvey_code.java
More file actions
1876 lines (1700 loc) · 69.3 KB
/
Copy pathsurvey_code.java
File metadata and controls
1876 lines (1700 loc) · 69.3 KB
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
/*
ID Method CC Cognitive LOC MaxNest FanOut Params HalVol HalEff AvgIdW
--------------------------------------------------------------------------------------------------------
01-ORIG calc 4 6 11 3 0 1 341.3 11263.6 1.00
01-V1 calc 4 4 9 2 0 1 325.5 11252.5 1.00
01-V2 calc 4 5 13 2 0 1 364.3 12596.1 1.00
01-V3 sumPositiveEvenValues 4 6 11 3 0 1 341.3 11263.6 1.18
02-ORIG grade 5 14 21 4 0 1 376.0 4512.4 1.00
02-V1 grade 5 4 15 1 0 1 257.8 1890.8 1.00
02-V2 grade 5 5 15 4 0 1 347.8 4174.0 1.00
02-V3 grade 5 4 3 0 0 1 169.6 1357.1 1.00
02-V4 grade 5 14 21 4 0 1 376.0 4512.4 1.00
02-V5 grade 5 14 21 4 0 1 376.0 4512.4 1.00
03-ORIG check 6 2 8 1 5 1 338.6 4643.3 1.21
03-V1 check 6 5 18 1 5 1 427.9 5760.7 1.25
03-V2 check 4 1 3 0 3 1 125.1 1081.2 1.60
03-V2 hasMinLength 1 0 3 0 1 1 76.0 501.6 1.40
03-V2 hasLowerAndUpper 2 1 3 0 3 1 145.9 1605.4 1.64
03-V2 hasDigit 1 0 3 0 1 1 70.3 421.9 1.20
03-V3 check 6 2 7 1 5 1 338.6 4643.3 1.21
04-ORIG count 7 4 10 2 2 1 375.0 6508.9 1.05
04-V1 count 7 3 17 2 2 1 365.0 5601.3 1.07
04-V2 count 3 3 9 2 3 1 277.3 4215.4 1.13
04-V3 countVowels 7 4 11 2 2 1 375.0 6508.9 1.25
05-ORIG sd 3 4 9 1 1 1 203.9 5352.4 1.00
05-V1 sd 2 1 9 1 1 1 229.2 4126.5 1.00
05-V2 sd 3 4 6 1 1 1 188.9 5618.9 1.00
06-ORIG max 4 6 11 3 0 1 413.7 11169.2 1.04
06-V1 max 4 6 11 3 0 1 256.8 4364.9 1.07
06-V2 max 2 2 7 1 2 1 224.7 3145.3 1.14
06-V2 rowMax 3 3 9 2 0 1 194.5 3306.7 1.17
06-V3 max 4 6 11 3 0 1 413.7 11169.2 1.04
07-ORIG withdraw 5 11 23 3 1 2 549.7 10704.3 1.10
07-V1 withdraw 5 4 19 1 1 2 448.0 7093.5 1.13
07-V2 withdraw 5 4 17 1 2 2 444.6 6669.2 1.05
08-ORIG dedupe 5 8 15 3 4 1 620.6 14740.3 1.03
08-V1 dedupe 3 3 9 2 2 1 282.0 5327.2 1.05
08-V2 dedupe 1 0 3 0 0 1 126.7 1045.4 1.33
08-V3 removeDuplicates 5 8 15 3 4 1 620.6 14740.3 1.39
09-ORIG words 4 7 15 3 2 1 381.5 7248.0 1.06
09-V1 words 3 3 9 2 2 1 213.6 2658.3 1.08
09-V2 words 4 4 12 2 2 1 361.7 6871.5 1.05
09-V3 words 4 7 15 3 2 1 381.5 7248.0 1.06
10-ORIG price 5 11 19 3 0 3 418.2 8402.8 1.00
10-V1 price 5 11 19 3 0 3 418.2 8402.8 1.58
10-V2 price 7 6 16 1 0 3 394.2 7919.9 1.00
10-V3 price 5 4 4 0 0 3 252.6 3399.8 1.00
11-ORIG sign 3 5 13 2 0 1 220.4 4628.8 1.00
11-V1 sign 3 3 11 2 0 1 211.8 4447.3 1.00
11-V2 sign 3 2 3 0 0 1 108.4 1707.6 1.00
12-ORIG leap 4 9 17 3 0 1 325.5 5696.8 1.00
12-V1 leap 3 2 3 0 0 1 136.2 1872.2 1.00
12-V2 leap 3 2 9 1 0 1 188.9 1994.9 1.00
13-ORIG rev 2 1 7 1 2 1 239.7 3835.6 1.06
13-V1 rev 1 0 3 0 2 1 100.0 666.7 1.25
13-V2 rev 2 1 9 1 1 1 421.1 12250.4 1.07
14-ORIG find 4 6 11 3 0 2 317.3 7535.5 1.00
14-V1 find 3 3 8 2 0 2 242.5 4850.0 1.00
14-V2 find 4 4 9 2 0 2 301.2 7511.0 1.00
14-V3 find 4 6 11 3 0 2 317.3 7535.5 1.00
15-ORIG tally 3 4 12 2 4 1 542.8 12892.5 1.06
15-V1 tally 2 1 7 1 2 1 309.1 4851.0 1.14
15-V2 countByValue 3 4 12 2 4 1 542.8 12892.5 1.11
16-ORIG merge 6 7 27 2 0 2 840.0 57960.0 1.00
16-V1 merge 4 5 16 2 1 2 795.0 46835.0 1.00
16-V2 merge 1 0 7 0 2 2 385.4 8942.1 1.00
16-V3 merge 6 7 27 2 0 2 840.0 57960.0 1.00
17-ORIG sortIt 4 6 13 3 1 1 544.7 19607.9 1.03
17-V1 sortIt 4 6 11 3 2 1 460.7 14029.5 1.04
17-V1 swap 1 0 5 0 0 3 179.3 3263.4 1.00
17-V2 sortIt 4 6 16 3 1 1 569.8 19325.6 1.03
17-V3 sortIt 1 0 5 0 2 1 148.7 1449.6 1.11
18-ORIG roman 4 8 16 3 3 1 519.8 15310.6 1.06
18-ORIG value 8 1 12 1 0 1 312.1 2313.3 1.00
18-V1 roman 4 5 12 2 3 1 461.2 13145.6 1.07
18-V1 value 8 1 12 1 0 1 312.1 2313.3 1.00
18-V2 roman 3 4 14 2 3 1 421.1 10001.3 1.04
18-V2 value 8 1 12 1 0 1 312.1 2313.3 1.00
19-ORIG bs 4 8 17 3 0 2 479.2 15981.9 1.00
19-V1 bs 4 5 15 3 0 2 469.1 15645.4 1.00
19-V2 bs 2 1 4 0 1 2 173.9 2347.9 1.10
20-ORIG balanced 15 28 26 4 5 1 938.7 26937.3 1.11
20-V1 balanced 6 10 17 4 6 1 707.2 16786.6 1.25
20-V2 balanced 6 8 14 4 7 1 649.7 14422.7 1.19
20-V2 matches 1 0 3 0 1 2 118.9 1159.7 1.29
20-V3 balanced 15 28 26 4 5 1 938.7 26937.3 1.11
*/
/* ----------------------------------------------------------------------------------
* ID: 01-ORIG
* Function: Sums the positive even values in an array.
* Complexities: CC 4 | Cognitive 6 | Nesting 3 | LOC 11 | FanOut 0
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
public static int calc(int[] d) {
int t = 0;
for (int i = 0; i < d.length; i++) {
if (d[i] > 0) {
if (d[i] % 2 == 0) {
t = t + d[i];
}
}
}
return t;
}
/* ----------------------------------------------------------------------------------
* ID: 01-V1
* Function: Sums the positive even values in an array.
* Complexities: CC 4 | Cognitive 4 | Nesting 2 | LOC 9 | FanOut 0
* Isolating: nesting, by merging the two conditions with &&
* ---------------------------------------------------------------------------------- */
// CHECK HERE i think this might be too small a change to add, like i think everyone will
// pick the option that there was no change in how complex they found it idk
public static int calc(int[] d) {
int t = 0;
for (int i = 0; i < d.length; i++) {
if (d[i] > 0 && d[i] % 2 == 0) {
t = t + d[i];
}
}
return t;
}
/* ----------------------------------------------------------------------------------
* ID: 01-V2
* Function: Sums the positive even values in an array.
* Complexities: CC 4 | Cognitive 5 | Nesting 2 | LOC 13 | FanOut 0
* Isolating: nesting, by inverting both conditions into guard clauses. LOC rises here,
* unlike 01-V1, which lets you separate depth from size
* ---------------------------------------------------------------------------------- */
public static int calc(int[] d) {
int t = 0;
for (int i = 0; i < d.length; i++) {
if (d[i] <= 0) {
continue;
}
if (d[i] % 2 != 0) {
continue;
}
t = t + d[i];
}
return t;
}
/* ----------------------------------------------------------------------------------
* ID: 01-V3
* Function: Sums the positive even values in an array.
* Complexities: CC 4 | Cognitive 6 | Nesting 3 | LOC 11 | FanOut 0
* Isolating: naming, structure identical to 01-ORIG
* ---------------------------------------------------------------------------------- */
// CHECK HERE i like this but the students might abuse the function name for the 'explain
// what this method does' question
public static int sumPositiveEvenValues(int[] values) {
int total = 0;
for (int index = 0; index < values.length; index++) {
if (values[index] > 0) {
if (values[index] % 2 == 0) {
total = total + values[index];
}
}
}
return total;
}
/* ----------------------------------------------------------------------------------
* ID: 02-ORIG
* Function: Turns a numeric score into a letter grade.
* Complexities: CC 5 | Cognitive 14 | Nesting 4 | LOC 21 | FanOut 0
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
public static String grade(int s) {
String r;
if (s >= 50) {
if (s >= 65) {
if (s >= 80) {
if (s >= 90) {
r = "A+";
} else {
r = "A";
}
} else {
r = "B";
}
} else {
r = "C";
}
} else {
r = "D";
}
return r;
}
/* ----------------------------------------------------------------------------------
* ID: 02-V1
* Function: Turns a numeric score into a letter grade.
* Complexities: CC 5 | Cognitive 4 | Nesting 1 | LOC 15 | FanOut 0
* Isolating: nesting, by using guard clauses that return early
* ---------------------------------------------------------------------------------- */
public static String grade(int s) {
if (s < 50) {
return "D";
}
if (s < 65) {
return "C";
}
if (s < 80) {
return "B";
}
if (s < 90) {
return "A";
}
return "A+";
}
/* ----------------------------------------------------------------------------------
* ID: 02-V2
* Function: Turns a numeric score into a letter grade.
* Complexities: CC 5 | Cognitive 5 | Nesting 1 | LOC 15 | FanOut 0
* Isolating: multiple exits, by flattening to an else-if chain but keeping one exit
* ---------------------------------------------------------------------------------- */
// CHECK HERE i think this one is too similar to one above ^ , prob should keep one delete
// the other. i dont have a preference on which
public static String grade(int s) {
String r;
if (s >= 90) {
r = "A+";
} else if (s >= 80) {
r = "A";
} else if (s >= 65) {
r = "B";
} else if (s >= 50) {
r = "C";
} else {
r = "D";
}
return r;
}
/* ----------------------------------------------------------------------------------
* ID: 02-V3
* Function: Turns a numeric score into a letter grade.
* Complexities: CC 5 | Cognitive 10 | Nesting 4 | LOC 3 | FanOut 0
* Isolating: volume, by collapsing to a chained ternary at constant CC. Cognitive is
* 10 under a strict reading of the nesting rule and 4 if a ternary chain is
* treated as flat, so measure it with your own extractor
* ---------------------------------------------------------------------------------- */
public static String grade(int s) {
return s >= 90 ? "A+" : s >= 80 ? "A" : s >= 65 ? "B" : s >= 50 ? "C" : "D";
}
/* ----------------------------------------------------------------------------------
* ID: 02-V4
* Function: Turns a numeric score into a letter grade.
* Complexities: CC 5 | Cognitive 14 | Nesting 4 | LOC 21 | FanOut 0 | 4 comment lines
* Isolating: comment quality, accurate summary. Code token for token identical to
* 02-ORIG with a correct header comment stating the bands
* ---------------------------------------------------------------------------------- */
/**
* Converts a raw exam mark into a letter grade.
* Bands: 90 and above A+, 80 to 89 A, 65 to 79 B, 50 to 64 C, below 50 D.
*/
public static String grade(int s) {
String r;
if (s >= 50) {
if (s >= 65) {
if (s >= 80) {
if (s >= 90) {
r = "A+";
} else {
r = "A";
}
} else {
r = "B";
}
} else {
r = "C";
}
} else {
r = "D";
}
return r;
}
/* ----------------------------------------------------------------------------------
* ID: 02-V5
* Function: Turns a numeric score into a letter grade.
* Complexities: CC 5 | Cognitive 14 | Nesting 4 | LOC 21 | FanOut 0 | 8 comment lines
* Isolating: comment quality, redundant noise. Code identical to 02-ORIG, every
* comment restates the syntax of the line below it and adds nothing.
* Pair against 02-V4 to separate comment presence from comment usefulness
* ---------------------------------------------------------------------------------- */
// CHECK HERE i dont like this one or the one above ^ bc i think this method is too
// comprehensible to have comments. i think we get the comment results from other
// methods
public static String grade(int s) {
// declare the result variable
String r;
// check whether s is at least 50
if (s >= 50) {
// check whether s is at least 65
if (s >= 65) {
// check whether s is at least 80
if (s >= 80) {
// check whether s is at least 90
if (s >= 90) {
// set r to A+
r = "A+";
} else {
// set r to A
r = "A";
}
} else {
r = "B";
}
} else {
r = "C";
}
} else {
r = "D";
}
// return the result
return r;
}
/* ----------------------------------------------------------------------------------
* ID: 03-ORIG
* Function: Checks a password for length, mixed case and a digit.
* Complexities: CC 6 | Cognitive 2 | Nesting 1 | LOC 8 | FanOut 5
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
// CHECK HERE i think this method could be good to add comments too as a variant
public static boolean check(String p) {
boolean ok = false;
if (p != null && p.length() >= 8 && !p.equals(p.toLowerCase())
&& !p.equals(p.toUpperCase()) && p.matches(".*[0-9].*")) {
ok = true;
}
return ok;
}
/* ----------------------------------------------------------------------------------
* ID: 03-V1
* Function: Checks a password for length, mixed case and a digit.
* Complexities: CC 6 | Cognitive 5 | Nesting 1 | LOC 18 | FanOut 5
* Isolating: cognitive complexity against CC, held equal at 6 while cognitive moves 2 to 5
* ---------------------------------------------------------------------------------- */
public static boolean check(String p) {
if (p == null) {
return false;
}
if (p.length() < 8) {
return false;
}
if (p.equals(p.toLowerCase())) {
return false;
}
if (p.equals(p.toUpperCase())) {
return false;
}
if (!p.matches(".*[0-9].*")) {
return false;
}
return true;
}
/* ----------------------------------------------------------------------------------
* ID: 03-V2
* Function: Checks a password for length, mixed case and a digit.
* Complexities: CC 4 | Cognitive 1 | Nesting 0 | LOC 3 | FanOut 4
* Helpers: CC 1, 2, 1 and LOC 3, 3, 3. Block totals CC 8, LOC 12
* Isolating: extraction into named helpers
* ---------------------------------------------------------------------------------- */
// CHECK HERE is it fine to make helper methods? just wanted to make sure since our study is
// about method complexity and not class complexity
public static boolean check(String p) {
return p != null && hasMinLength(p) && hasLowerAndUpper(p) && hasDigit(p);
}
private static boolean hasMinLength(String p) {
return p.length() >= 8;
}
private static boolean hasLowerAndUpper(String p) {
return !p.equals(p.toLowerCase()) && !p.equals(p.toUpperCase());
}
private static boolean hasDigit(String p) {
return p.matches(".*[0-9].*");
}
/* ----------------------------------------------------------------------------------
* ID: 03-V3
* Function: Checks a password for length, mixed case and a digit.
* Complexities: CC 6 | Cognitive 2 | Nesting 1 | LOC 7 | FanOut 5
* Isolating: layout, identical tokens to 03-ORIG on one line instead of two
* ---------------------------------------------------------------------------------- */
// CHECK HERE i think this one is too similar to 03-ORIG to be useful
public static boolean check(String p) {
boolean ok = false;
if (p != null && p.length() >= 8 && !p.equals(p.toLowerCase()) && !p.equals(p.toUpperCase()) && p.matches(".*[0-9].*")) {
ok = true;
}
return ok;
}
/* ----------------------------------------------------------------------------------
* ID: 04-ORIG
* Function: Counts the lower case vowels in a string.
* Complexities: CC 7 | Cognitive 4 | Nesting 2 | LOC 10 | FanOut 2
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
// CHECK HERE cbk to scrap this method entirely
public static int count(String s) {
int n = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
n++;
}
}
return n;
}
/* ----------------------------------------------------------------------------------
* ID: 04-V1
* Function: Counts the lower case vowels in a string.
* Complexities: CC 7 | Cognitive 3 | Nesting 2 | LOC 17 | FanOut 2
* Isolating: construct type, a switch scoring the same CC as the || chain it replaces
* ---------------------------------------------------------------------------------- */
public static int count(String s) {
int n = 0;
for (int i = 0; i < s.length(); i++) {
switch (s.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
n++;
break;
default:
break;
}
}
return n;
}
/* ----------------------------------------------------------------------------------
* ID: 04-V2
* Function: Counts the lower case vowels in a string.
* Complexities: CC 3 | Cognitive 3 | Nesting 2 | LOC 9 | FanOut 3
* Isolating: branch count, traded for implicit knowledge of what indexOf returns
* ---------------------------------------------------------------------------------- */
// CHECK HERE too similar to originial i reckon, and .indexOf is just gonna make students
// say its more complex than them comparing branches
public static int count(String s) {
int n = 0;
for (int i = 0; i < s.length(); i++) {
if ("aeiou".indexOf(s.charAt(i)) >= 0) {
n++;
}
}
return n;
}
/* ----------------------------------------------------------------------------------
* ID: 04-V3
* Function: Counts the lower case vowels in a string.
* Complexities: CC 7 | Cognitive 4 | Nesting 2 | LOC 11 | FanOut 2
* Isolating: naming and layout, control flow identical to 04-ORIG
* ---------------------------------------------------------------------------------- */
// CHECK HERE idk i think this ones a maybe
public static int countVowels(String text) {
int vowelCount = 0;
for (int position = 0; position < text.length(); position++) {
char current = text.charAt(position);
if (current == 'a' || current == 'e' || current == 'i'
|| current == 'o' || current == 'u') {
vowelCount++;
}
}
return vowelCount;
}
/* ----------------------------------------------------------------------------------
* ID: 05-ORIG
* Function: Sums the decimal digits of an integer.
* Complexities: CC 3 | Cognitive 4 | Nesting 1 | LOC 9 | FanOut 1
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
public static int sd(int n) {
if (n < 0) {
return sd(-n);
}
if (n < 10) {
return n;
}
return n % 10 + sd(n / 10);
}
/* ----------------------------------------------------------------------------------
* ID: 05-V1
* Function: Sums the decimal digits of an integer.
* Complexities: CC 2 | Cognitive 1 | Nesting 1 | LOC 9 | FanOut 1
* Isolating: recursion, with LOC identical on both sides so size cannot explain a preference
* ---------------------------------------------------------------------------------- */
public static int sd(int n) {
int remaining = Math.abs(n);
int total = 0;
while (remaining > 0) {
total = total + remaining % 10;
remaining = remaining / 10;
}
return total;
}
/* ----------------------------------------------------------------------------------
* ID: 05-V2
* Function: Sums the decimal digits of an integer.
* Complexities: CC 3 | Cognitive 4 | Nesting 1 | LOC 6 | FanOut 1
* Isolating: volume, with recursion and both control flow metrics held constant
* ---------------------------------------------------------------------------------- */
public static int sd(int n) {
if (n < 0) {
return sd(-n);
}
return n < 10 ? n : n % 10 + sd(n / 10);
}
/* ----------------------------------------------------------------------------------
* ID: 06-ORIG
* Function: Finds the largest value in a 2D array.
* Complexities: CC 4 | Cognitive 6 | Nesting 3 | LOC 11 | FanOut 0
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
public static int max(int[][] g) {
int m = Integer.MIN_VALUE;
for (int i = 0; i < g.length; i++) {
for (int j = 0; j < g[i].length; j++) {
if (g[i][j] > m) {
m = g[i][j];
}
}
}
return m;
}
/* ----------------------------------------------------------------------------------
* ID: 06-V1
* Function: Finds the largest value in a 2D array.
* Complexities: CC 4 | Cognitive 6 | Nesting 3 | LOC 11 | FanOut 0
* Isolating: volume alone, since enhanced for loops move only Halstead vocabulary
* ---------------------------------------------------------------------------------- */
// CHECK HERE too similar to original, i think using other java features is not good for this
// study, since it will make students think the complexity is higher
public static int max(int[][] g) {
int m = Integer.MIN_VALUE;
for (int[] row : g) {
for (int cell : row) {
if (cell > m) {
m = cell;
}
}
}
return m;
}
/* ----------------------------------------------------------------------------------
* ID: 06-V2
* Function: Finds the largest value in a 2D array.
* Complexities: CC 2 | Cognitive 1 | Nesting 1 | LOC 7 | FanOut 2
* Helper rowMax: CC 3, Cognitive 3, Nesting 2, LOC 9. Block totals CC 5, LOC 16
* Isolating: extraction, where summed CC exceeds the original but no method is as deep
* ---------------------------------------------------------------------------------- */
// CHECK HERE as written before, idk if we should use helper methods. also i dont think
// we should use a helper method for this method if we are including them, it makes it
// sm more complicated i reckon
public static int max(int[][] g) {
int m = Integer.MIN_VALUE;
for (int[] row : g) {
m = Math.max(m, rowMax(row));
}
return m;
}
private static int rowMax(int[] row) {
int m = Integer.MIN_VALUE;
for (int cell : row) {
if (cell > m) {
m = cell;
}
}
return m;
}
/* ----------------------------------------------------------------------------------
* ID: 06-V3
* Function: Finds the largest value in a 2D array.
* Complexities: CC 4 | Cognitive 6 | Nesting 3 | LOC 11 | FanOut 0 | 3 comment lines
* Isolating: comment quality, helpful inline. Code token for token identical to 06-ORIG,
* each comment explains why rather than what
* ---------------------------------------------------------------------------------- */
// CHECK HERE i like having comments in this one, although i think the comments should be
// more comprehensible (also bc a lot of compsci students are international, may not
// have english as first language)
public static int max(int[][] g) {
// start below every possible value so the first cell always wins
int m = Integer.MIN_VALUE;
for (int i = 0; i < g.length; i++) {
// rows may have different lengths, so bound on this row
for (int j = 0; j < g[i].length; j++) {
if (g[i][j] > m) {
// keep the running best
m = g[i][j];
}
}
}
return m;
}
/* ----------------------------------------------------------------------------------
* ID: 07-ORIG
* Function: Withdraws an amount from an account and returns a status word.
* Complexities: CC 5 | Cognitive 11 | Nesting 3 | LOC 23 | FanOut 1
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
// CHECK HERE i think whole method may be a bit too complex for this study, maybe we could
// remove the class and just have a double balance variable or smth, or just remove
// this method entirely
class Account {
double balance;
}
public static String withdraw(Account a, String amt) {
String msg;
if (a != null) {
try {
double v = Double.parseDouble(amt);
if (v > 0) {
if (a.balance >= v) {
a.balance = a.balance - v;
msg = "ok";
} else {
msg = "insufficient";
}
} else {
msg = "bad amount";
}
} catch (NumberFormatException e) {
msg = "not a number";
}
} else {
msg = "no account";
}
return msg;
}
/* ----------------------------------------------------------------------------------
* ID: 07-V1
* Function: Withdraws an amount from an account and returns a status word.
* Complexities: CC 5 | Cognitive 4 | Nesting 1 | LOC 19 | FanOut 1
* Isolating: nesting, held against constant CC. The strongest depth pair in the set
* ---------------------------------------------------------------------------------- */
class Account {
double balance;
}
public static String withdraw(Account a, String amt) {
if (a == null) {
return "no account";
}
double v;
try {
v = Double.parseDouble(amt);
} catch (NumberFormatException e) {
return "not a number";
}
if (v <= 0) {
return "bad amount";
}
if (a.balance < v) {
return "insufficient";
}
a.balance = a.balance - v;
return "ok";
}
/* ----------------------------------------------------------------------------------
* ID: 07-V2
* Function: Withdraws an amount from an account and returns a status word.
* Complexities: CC 5 | Cognitive 4 | Nesting 1 | LOC 17 | FanOut 2
* Isolating: exception handling swapped for a regex. Pair against 07-V1, not the original.
* Diverges from the original only on exponent notation such as 1e3
* ---------------------------------------------------------------------------------- */
// CHECK HERE i reckon too similar to above ^ , also i think 'matches("-?\\d+(\\.\\d+)?"' is too
// complex for our audience
class Account {
double balance;
}
public static String withdraw(Account a, String amt) {
if (a == null) {
return "no account";
}
if (!amt.matches("-?\\d+(\\.\\d+)?")) {
return "not a number";
}
double v = Double.parseDouble(amt);
if (v <= 0) {
return "bad amount";
}
if (a.balance < v) {
return "insufficient";
}
a.balance = a.balance - v;
return "ok";
}
/* ----------------------------------------------------------------------------------
* ID: 08-ORIG
* Function: Removes duplicates from a list, keeping first appearance order.
* Complexities: CC 5 | Cognitive 8 | Nesting 3 | LOC 15 | FanOut 5
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
// CHECK HERE love this method (like including variants), def keep, also could be good
// candidate for comments
import java.util.ArrayList;
import java.util.List;
public static List<String> dedupe(List<String> in) {
List<String> out = new ArrayList<>();
for (int i = 0; i < in.size(); i++) {
boolean found = false;
for (int j = 0; j < out.size(); j++) {
if (in.get(i).equals(out.get(j))) {
found = true;
}
}
if (!found) {
out.add(in.get(i));
}
}
return out;
}
/* ----------------------------------------------------------------------------------
* ID: 08-V1
* Function: Removes duplicates from a list, keeping first appearance order.
* Complexities: CC 3 | Cognitive 3 | Nesting 2 | LOC 9 | FanOut 3
* Isolating: branch count and nesting together, algorithm unchanged
* ---------------------------------------------------------------------------------- */
import java.util.ArrayList;
import java.util.List;
public static List<String> dedupe(List<String> in) {
List<String> out = new ArrayList<>();
for (String s : in) {
if (!out.contains(s)) {
out.add(s);
}
}
return out;
}
/* ----------------------------------------------------------------------------------
* ID: 08-V2
* Function: Removes duplicates from a list, keeping first appearance order.
* Complexities: CC 1 | Cognitive 0 | Nesting 0 | LOC 3 | FanOut 2
* Isolating: library knowledge, at the floor of every structural metric
* ---------------------------------------------------------------------------------- */
// CHECK HERE idk i think this one may be too based on utilising java libraries instead of
// the actual algorithm
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
public static List<String> dedupe(List<String> in) {
return new ArrayList<>(new LinkedHashSet<>(in));
}
/* ----------------------------------------------------------------------------------
* ID: 08-V3
* Function: Removes duplicates from a list, keeping first appearance order.
* Complexities: CC 5 | Cognitive 8 | Nesting 3 | LOC 15 | FanOut 5
* Isolating: naming, on a structurally hard method. Pair with 01-V3 for the interaction
* between naming and structural difficulty
* ---------------------------------------------------------------------------------- */
import java.util.ArrayList;
import java.util.List;
public static List<String> removeDuplicates(List<String> source) {
List<String> unique = new ArrayList<>();
for (int sourceIndex = 0; sourceIndex < source.size(); sourceIndex++) {
boolean alreadySeen = false;
for (int uniqueIndex = 0; uniqueIndex < unique.size(); uniqueIndex++) {
if (source.get(sourceIndex).equals(unique.get(uniqueIndex))) {
alreadySeen = true;
}
}
if (!alreadySeen) {
unique.add(source.get(sourceIndex));
}
}
return unique;
}
/* ----------------------------------------------------------------------------------
* ID: 09-ORIG
* Function: Counts space separated words in a string.
* Complexities: CC 4 | Cognitive 7 | Nesting 3 | LOC 15 | FanOut 2
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
public static int words(String t) {
int c = 0;
boolean in = false;
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) != ' ') {
if (!in) {
c++;
in = true;
}
} else {
in = false;
}
}
return c;
}
/* ----------------------------------------------------------------------------------
* ID: 09-V1
* Function: Counts space separated words in a string.
* Complexities: CC 3 | Cognitive 3 | Nesting 2 | LOC 9 | FanOut 3
* Isolating: branch count and nesting, at the cost of knowing how split treats empty pieces
* ---------------------------------------------------------------------------------- */
// CHECK HERE library method may be too complex for our audience
public static int words(String t) {
int c = 0;
for (String part : t.split(" ")) {
if (!part.isEmpty()) {
c++;
}
}
return c;
}
/* ----------------------------------------------------------------------------------
* ID: 09-V2
* Function: Counts space separated words in a string.
* Complexities: CC 4 | Cognitive 4 | Nesting 2 | LOC 12 | FanOut 2
* Isolating: nesting, with the algorithm and CC both held fixed
* ---------------------------------------------------------------------------------- */
// CHECK HERE i think this one is too similar to the original since nesting only changes by 1
public static int words(String t) {
int c = 0;
boolean in = false;
for (int i = 0; i < t.length(); i++) {
boolean space = t.charAt(i) == ' ';
if (!space && !in) {
c++;
}
in = !space;
}
return c;
}
/* ----------------------------------------------------------------------------------
* ID: 09-V3
* Function: Counts space separated words in a string.
* Complexities: CC 4 | Cognitive 7 | Nesting 3 | LOC 15 | FanOut 2 | 4 comment lines
* Isolating: comment quality, actively misleading. Code token for token identical to
* 09-ORIG. Every comment is wrong: c holds words not spaces, in is true
* inside a word not after one, nothing skips punctuation, and the last
* comment describes the wrong event. Pair against 09-ORIG to measure the
* cost of a wrong comment against no comment at all
* ---------------------------------------------------------------------------------- */
// CHECK HERE should we do misleading comments? i dont think our study is large enough to
// measure the effect of misleading comments, but i think helpful comments could be good
// to include in this method bc i lowkey took forever to understand it
public static int words(String t) {
// number of spaces seen so far
int c = 0;
// true when the previous character was a space
boolean in = false;
for (int i = 0; i < t.length(); i++) {
// skip over any punctuation
if (t.charAt(i) != ' ') {
if (!in) {
c++;
in = true;
}
} else {
// we have reached the end of the string
in = false;
}
}
return c;
}
/* ----------------------------------------------------------------------------------
* ID: 10-ORIG
* Function: Prices an order after quantity and membership discounts.
* Complexities: CC 5 | Cognitive 11 | Nesting 3 | LOC 19 | FanOut 0
* Isolating: baseline
* ---------------------------------------------------------------------------------- */
// CHECK HERE not a big fan of the method entirely, i think its very difficult to
// understand without context, like if i didnt read it was for a member ship id just think
// its doing random math. I think if we are including this method, we should just have
// the original against added comments / naming, and not the other variants.
public static double price(double p, int q, boolean m) {
double r = p * q;
if (q > 100) {
if (m) {
r = r * 0.75;
} else {
r = r * 0.85;
}
} else {
if (q > 10) {
if (m) {
r = r * 0.9;
} else {
r = r * 0.95;
}
}
}
return r;
}
/* ----------------------------------------------------------------------------------
* ID: 10-V1
* Function: Prices an order after quantity and membership discounts.
* Complexities: CC 5 | Cognitive 11 | Nesting 3 | LOC 19 | FanOut 0
* Isolating: magic numbers and naming, with every listed metric held constant
* ---------------------------------------------------------------------------------- */
// CHECK HERE similar to what i said ab the helper methods, is it alg to have variables
// outside the methods? idk if we can say we're comparing java methods if we're also
// comparing the variables outside of them
private static final double MEMBER_BULK_RATE = 0.75;
private static final double STANDARD_BULK_RATE = 0.85;
private static final double MEMBER_VOLUME_RATE = 0.90;
private static final double STANDARD_VOLUME_RATE = 0.95;
private static final int BULK_THRESHOLD = 100;
private static final int VOLUME_THRESHOLD = 10;
public static double price(double unitPrice, int quantity, boolean isMember) {
double total = unitPrice * quantity;
if (quantity > BULK_THRESHOLD) {
if (isMember) {
total = total * MEMBER_BULK_RATE;
} else {
total = total * STANDARD_BULK_RATE;
}
} else {
if (quantity > VOLUME_THRESHOLD) {
if (isMember) {
total = total * MEMBER_VOLUME_RATE;
} else {
total = total * STANDARD_VOLUME_RATE;
}
}