-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperInt.java
More file actions
1400 lines (1124 loc) · 44.3 KB
/
Copy pathSuperInt.java
File metadata and controls
1400 lines (1124 loc) · 44.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
import java.util.*;
public class SuperInt implements Cloneable, Comparable<SuperInt>{
/*
A template file for you to use.
The only code that is correct and should not be changed is
the default constructor
getMagnitude
magnitudeToString
toString
The coding you have to do to obtain full credit is
SuperInt(int n)
SuperInt(String s)
clone
compareMagnitudes
compareTo
addMagnitudes
subtractMagnitudes
add
negate
negateMutate
addMutate
subtractMutate
multiplyMagnitudesByDigitAndShift
multiplyMagnitudes
multiply
multiplyMutate
equals
Their specifications are given below with their stub code.
There are two extra credit methods which if you do according to spec
are worth an additional 30 points.
The class implements arbitrarily large integers with a
sign and magnitude representation.
When you code the operations, you should consider for each input
which of the three categories it falls in
negative, that is, < 0
positive, that is, > 0
zero, which is neither positive nor negative
For a binary operation, that makes 3 x 3 = 9 distinct cases
to consider. Sometimes the same code can work for more than one
case, but it is safer first to consider what you need to do for
each.
The magnitude is given by a list of Integer objects, digSeq, holding int
values from 0 to 9 to represent a single decimal digit. The whole list
represents an unsigned decimal numeral, with two conventions
1. the high order decimal digit is the last item of the list and the low
order digit is the first item on the list, so 7509369 would be given in the
list 9, 6, 3, 9, 0, 5, 7, where 7 is the last item on the list.
2. there are no extra leading 0's, so the only time the last item
in the list is 0 is when the magnitude is 0, and in that case the
last 0 is the only item on the list.
The sign is given by a boolean data member, isNeg, which is true when the
represented value is < 0. The represented value is >= 0 exactly when isNeg
is false.
In this scheme the values (first item of the list is on the left)
257601 would be represented by isNeg false and digSeq 1, 0, 6, 7, 5, 2
-7884863 would be represented by isNeg true and digSeq 3, 6, 8, 4, 8, 8, 7
0 would be represented by isNeg false and digSeq 0
The class invariants are
1. the list is never empty, the items on it are never null, and the items
contain values in the range of 0 to 9.
2. if the last item on the list is 0, then that is the only item on the list
and isNeg is false.
Every object that observes these invariants represents a unique integer and every
integer value has a unique representation in this scheme.
You should make sure that any SuperInt objects you create observe these
invariants and any mutators you code preserve them.
*/
private static int
RADIX = 10;
private static Integer
zeroInteger = Integer.valueOf(0);
boolean
isNeg; // true if the value is < 0, else false
List<Integer> // although Byte would be big enough, it is more efficient to use Integer
digSeq;
/*
DO NOT MODIFY THIS.
should create the representation of 0
for this
*/
public SuperInt(){
digSeq = new LinkedList<Integer>();
digSeq.add(zeroInteger);
// isNeg is initialized to false by default
}
/*
YOU MUST CODE THIS
should create the representation of the
integer n for this
You need to be careful with the extremal values of int.
The usual technique for peeling of the individual digits of
a decimal integer from low order to high is to repeatedly % by 10
to get the low order digit, and then / by 10 to shift the lower
order digit off the number.
However, if n is negative, and n % 10 is not 0, it will be a negative
value. This is because
i / j is the truncation of i/j as a real value
i % j if i - (i/j * j)
So for example, take i = -13 and j = 3
-13 / 3 = trunc(-13/3) = trunc(-4.3333...) = -4
-13 % 3 = -13 - (-4 * 3) = -13 - (-12) = -1
Note also, that if n is Integer.MIN_VALUE, then -n will overflow, so
simply converting negative values to positive will fail in that case.
*/
public SuperInt(int n){
digSeq = new LinkedList<Integer>();
if(n==Integer.MIN_VALUE) {
isNeg = true;
}
else {
if(n<0) {
isNeg = true;
n *= -1;
}
else if(n>0) {
isNeg = false;
}
else {
digSeq.add(zeroInteger);
isNeg = false;
}
}
while(n > 0) {
digSeq.add(n % 10);
n /= 10;
}
}
/*
YOU MUST CODE THIS
checks if s matches the pattern 0|-?[1-9][0-9]* by calling
s.matches("0|-?[1-9][0-9]*")
This is the simplest way to do that test.
if not, throw InputMismatchException with the message
"Improper String value passed to SuperInt constructor."
otherwise, construct the internal representation of
the decimal numeral s amounts to for this object
*/
public SuperInt(String s) throws InputMismatchException{
digSeq = new LinkedList<Integer>();
if(!s.matches("0|-?[1-9][0-9]*")) {
throw new InputMismatchException("Improper String value passed to SuperInt constructor.");
}
else{
if(s.charAt(0)=='-') {
isNeg = true;
}
else {
isNeg = false;
s = "0"+s;
}
for(int i = 1;i<s.length();i++) {
digSeq.add(0,Integer.parseInt(String.valueOf(s.charAt(i))));
}
}
}
/*
DO NOT MODIFY THIS.
This is used in the testing and must return the digit sequence
data member of the SuperInt.
*/
List<Integer> getMagnitude(){
return digSeq;
}
/*
DO NOT MODIFY THIS.
Converts the list of digits in a magnitude to a string and
returns it.
amag is not modified
*/
private static String magnitudeToString(List<Integer> amag){
StringBuilder bld = new StringBuilder();
ListIterator<Integer> iter = amag.listIterator(amag.size());
while (iter.hasPrevious())
bld.append(iter.previous().intValue());
return bld.toString();
}
/*
DO NOT MODIFY THIS.
Constructs the string numeral for the value represented by
this.
*/
public String toString(){
StringBuilder str = new StringBuilder();
if (isNeg)
str.append('-');
str.append(magnitudeToString(digSeq));
return str.toString();
}
/*
YOU MUST CODE THIS
makes and returns a copy of this so that there are
not common instance specific data members between
this and the copy. They should NOT share the list.
Note, wrapper class objects Integer, Double, etc., are
immutable.
this is not modified
*/
public SuperInt clone(){
SuperInt C = new SuperInt();
C.isNeg = isNeg;
ListIterator<Integer> iter = digSeq.listIterator(0);
while (iter.hasNext())
C.digSeq.add(iter.next());
return C;
}
/*
YOU MUST CODE THIS
The magnitude of a integer value n is the absolute
value of n, |n|. It is what is given by the list
data member, so you can accomplish this by comparing
the lists this and other.
returns
a value < 0 if A < B as unsigned integers
0 if A equals B as unsigned integers
a value > 0 if A > B as unsigned integers
NEITHER A NOR B SHOULD BE CHANGED.
The way to do this is as follows.
If the lengths of the lists are different, the one
that is longer has the larger magnitude.
If the lengths are the same, iterate over both lists
from HIGH order digit to LOW. Either you reach a position
where they have different digit values or all are the same
and you iterate off both lists. If you iterate off both
lists, all their digits are the same and they are equal.
If you reach a position where they have different digits,
the one with the larger digit has the larger magnitude.
*/
private static int compareMagnitudes(List<Integer>A, List<Integer>B){
if(A.size()>B.size()) {
return 1;
}
else if(A.size()<B.size()) {
return -1;
}
else {
return magnitudeToString(A).compareTo(magnitudeToString(B));
}
}
/*
YOU MUST CODE THIS
returns
a value < 0 if this < other as signed integers
0 if this equals other as signed integers
a value > 0 if this > other as signed integers
Note, once you have coded up compareMagnitudes, then you can
easily accomplish this by splitting on the following
nine cases
this other
<0 <0 compare the two magnitudes and negate the result,
because when x < 0 and y < 0, x < y iff
|x| > |y|
<0 0 this < other
<0 >0 this < other
0 <0 this > other
0 0 equal
0 >0 this < other
>0 <0 this > other
>0 0 this > other
>0 >0 compare the two magnitudes
neither this nor other should be changed
If other is not a SuperInt, return false;
*/
public int compareTo(SuperInt other){
if(isNeg && other.isNeg) {
return (-1)*compareMagnitudes(digSeq,other.digSeq);
}
else if(digSeq.size()==0 || other.digSeq.size()==0) {
if(digSeq.size()<other.digSeq.size()) {
return -1;
}
else if(digSeq.size()>other.digSeq.size()) {
return 1;
}
else {
return 0;
}
}
else if(isNeg && other.digSeq.get(other.digSeq.size()-1) == 0) {
return -1;
}
else if(isNeg && !other.isNeg) {
return -1;
}
else if(digSeq.get(digSeq.size()-1) == 0 && other.isNeg) {
return 1;
}
else if(digSeq.get(digSeq.size()-1) == 0 && other.digSeq.get(other.digSeq.size()-1) == 0) {
return 0;
}
else if(digSeq.get(digSeq.size()-1) == 0 && !other.isNeg) {
return -1;
}
else if(!isNeg && other.isNeg) {
return 1;
}
else if(!isNeg && other.digSeq.get(other.digSeq.size()-1) == 0) {
return 1;
}
else if(!isNeg && !other.isNeg) {
return compareMagnitudes(digSeq,other.digSeq);
}
else {
return -1;
}
}
/*
YOU MUST CODE THIS
Create an entirely new list that contains the
sum of the lists of this and other.
split on
1. A is 0, return a copy of B
2. B is 0, return a copy of A
3. neither is 0, so first create a new, empty list.
then iterate down the A and B lists from
low order digits to high order, adding the two
digits and the carry (initially 0) to obtain
a new digit and a new carry; note you will need
to correctly handle
a. A's list is shorter than B's
b. A's list is longer than B's
c. the two lists are equal in length
and both the case when the last carry out is 1 or the last
carry out is 0.
neither A nor B should be modified
*/
private static List<Integer> addMagnitudes(List<Integer> A, List<Integer> B){
List<Integer> C;
if(A.size()==0 || A.get(A.size()-1)==0) {
C = new LinkedList<Integer>(B);
}
else if(B.size()==0 || B.get(B.size()-1)==0) {
C = new LinkedList<Integer>(A);
}
else {
int carry = 0;
C = new LinkedList<Integer>();
if(A.size()>=B.size()) {
ListIterator<Integer> iterA = A.listIterator(0);
ListIterator<Integer> iterB = B.listIterator(0);
while (iterA.hasNext()) {
int n1 = iterA.next();
int n2 = 0;
if(iterB.hasNext()) {
n2 = iterB.next();
}
int result = n1+n2+carry;
if(result<10) {
C.add(result);
carry = 0;
}
else {
C.add(result%10);
carry = result/10;
}
}
if(carry>0) {
C.add(carry);
}
}
else {
ListIterator<Integer> iterA = A.listIterator(0);
ListIterator<Integer> iterB = B.listIterator(0);
while (iterB.hasNext()) {
int n1 = iterB.next();
int n2 = 0;
if(iterA.hasNext()) {
n2 = iterA.next();
}
int result = n1+n2+carry;
if(result<10) {
C.add(result);
carry = 0;
}
else {
C.add(result%10);
carry = result/10;
}
}
if(carry>0) {
C.add(carry);
}
}
}
return C;
}
/*
YOU MUST CODE THIS
Assumes the magnitude of A is STRICTLY LARGER than the
magnitude of B, so you should never call it when that is not true.
You can use compareMagnitudes to test that.
Create an entirely new list that contains the
list representation of the value of A - the value of B.
Because A's magnitude is larger than B's magnitude,
you can iterate from low order digits to high subtracting
the B digit from the A digit "borrowing" from the next
higher A digit if necessary (the borrow functions like the
carry in addition, only you subtract the borrow from the next
higher digit).
Roughly, the iteration is
initialize borrow to 0;
iterate over A and B from low order digits to high
if (current A digit - borrow - current B digit < 0){
result digit = current A digit - borrow - current B digit + 10;
borrow = 1;
}
else{
result digit = current A digit - borrow - current B digit;
borrow = 0;
}
Like addition, you need to handle the cases when
a. A list is longer than B list
b. the two lists are equal in length
Since A is strictly larger than B, the A list cannot be shorter than
the B list, but the B list could be shorter than A list.
Note, if you were to subtract 9999 from 10000, generating
the digits as you iterate, you would end up with
00001
and you would need to remove the extraneous leading 0's.
neither A nor B should be changed
*/
private static List<Integer> subtractMagnitudes(List<Integer> A, List<Integer> B){
LinkedList<Integer> C = new LinkedList<Integer>();
int borrow = 0;
ListIterator<Integer> iterA = A.listIterator(0);
ListIterator<Integer> iterB = B.listIterator(0);
while (iterA.hasNext()) {
int n1 = iterA.next();
int n2 = 0;
if(iterB.hasNext()) {
n2 = iterB.next();
}
int result;
if (n1 - borrow - n2 < 0){
result = n1 - borrow - n2 + 10;
borrow = 1;
}
else{
result = n1 - borrow - n2;
borrow = 0;
}
C.add(result);
}
while(!C.isEmpty() && C.getLast()==0) {
C.removeLast();
}
if(C.isEmpty()) {
C.add(zeroInteger);
}
return C;
}
/*
YOU MUST CODE THIS
returns a new object representing
the signed result of adding this and other
Again, the easiest way is to split on
this other
<0 <0 add the two magnitudes
<0 0 return a copy of this
<0 >0
use compareMagnitudes to split on
|this| < |other| subtract this's magnitude from other's
|this| = |other| 0
|this| > |other| subtract other's magnitude from this's
0 <0 copy of other
0 0 copy of other
0 >0 copy of other
>0 <0 see splits for <0 >0 above; this case is similar
>0 0 copy of this
>0 >0 add the two magnitudes
In all cases, the sign of the result is obvious if you think about
it.
neither this nor other should be changed
*/
public SuperInt add(SuperInt other){
SuperInt C = new SuperInt();
if(isNeg && other.isNeg) {
C.isNeg=true;
C.digSeq=addMagnitudes(digSeq,other.digSeq);
}
else if(isNeg && other.digSeq.size()!=0 && other.digSeq.get(other.digSeq.size()-1)==0) {
C.isNeg=isNeg;
C.digSeq=digSeq;
}
else if(isNeg && !other.isNeg) {
if(compareMagnitudes(digSeq,other.digSeq)<0) {
C.isNeg=other.isNeg;
C.digSeq=subtractMagnitudes(other.digSeq,digSeq);
}
else if(compareMagnitudes(digSeq,other.digSeq)==0) {
C.isNeg=false;
C.digSeq.add(zeroInteger);
}
else {
C.isNeg=isNeg;
C.digSeq=subtractMagnitudes(digSeq,other.digSeq);
}
}
else if(digSeq.get(digSeq.size()-1)==0 && other.isNeg) {
C.isNeg = other.isNeg;
C.digSeq = other.digSeq;
}
else if(digSeq.size()!=0 && other.digSeq.size()!=0 && digSeq.get(digSeq.size()-1)==0 && other.digSeq.get(other.digSeq.size()-1)==0) {
C.isNeg = other.isNeg;
C.digSeq = other.digSeq;
}
else if(digSeq.get(digSeq.size()-1)==0 && !other.isNeg) {
C.isNeg = other.isNeg;
C.digSeq = other.digSeq;
}
else if(!isNeg && other.isNeg) {
if(compareMagnitudes(digSeq,other.digSeq)<0) {
C.isNeg=other.isNeg;
C.digSeq=subtractMagnitudes(other.digSeq,digSeq);
}
else if(compareMagnitudes(digSeq,other.digSeq)==0) {
C.isNeg=false;
C.digSeq.add(zeroInteger);
}
else {
C.isNeg=isNeg;
C.digSeq=subtractMagnitudes(digSeq,other.digSeq);
}
}
else if(!isNeg && other.digSeq.size()!=0 && other.digSeq.get(other.digSeq.size()-1)==0) {
C.isNeg=isNeg;
C.digSeq=digSeq;
}
else if(!isNeg && !other.isNeg) {
C.isNeg=false;
C.digSeq=addMagnitudes(digSeq,other.digSeq);
}
return C;
}
/*
YOU MUST CODE THIS
returns a new SuperInt object representing the
negation of this's value
NOTE THE NEGATION OF ZERO IS JUST ZERO.
this should not be changed
*/
public SuperInt negate(){
SuperInt C = new SuperInt();
if(digSeq.size()!=0 && digSeq.get(digSeq.size()-1)==0) {
C.isNeg=false;
C.digSeq.add(zeroInteger);
}
else {
C.isNeg = !isNeg;
C.digSeq = digSeq;
}
return C;
}
/*
YOU MUST CODE THIS
like the last but this is given the result
For the unary operator negation and the binary operators of add,
subtract and multiply if you do either the op or the opMutate version,
it's not difficult to do the other from it.
Method 1 From mutator to nonmutator
Implement the mutating version of the operation.
Implement the clone operation.
Implement the non-mutating version by cloning the receiver and using the
mutating version on the clone, then return the clone.
Method 2 From nonmutator to mutator
Implement the non-mutating version.
Implement the mutating version by using the non-mutating version to
calculate the result into a local variable, and then assign the receiver
data members the values of the local variable's data members.
*/
public void negateMutate(){
SuperInt C = negate();
isNeg = C.isNeg;
digSeq = C.digSeq;
}
/*
YOU MUST CODE THIS
Modifies this to be this + other
should still work corrrect if this and other are the same object.
*/
public void addMutate(SuperInt other){
SuperInt C = add(other);
isNeg = C.isNeg;
digSeq = C.digSeq;
}
/*
YOU MUST CODE THIS
returns a new SuperInt object representing the
the signed result of subtracting other's value
with this's value
If you do add, and negate, then this method is simple, because
x - y = x + (-y)
neither this nor other should be changed
*/
public SuperInt subtract(SuperInt other){
return this.add(other.negate());
}
// see earlier comments for addMutate
// YOU MUST CODE THIS
// should still work correctly if this and other are the same object
public void subtractMutate(SuperInt other){
SuperInt C = subtract(other);
isNeg = C.isNeg;
digSeq = C.digSeq;
}
// the next several methods build to the multiply operation
/*
if you consider a typical multiplication
78913
462
-----
157826
4734780
31565200
--------
36457806
You will note that the computation can be broken down into two parts
1. a nested iteration where in the outer loop we iterate over the digits of
the lower factor (462 in this example), and multiple the higher factor
by each of the digits and add an increasing number of 0's to obtain
the three addends, 157826, 4734780, and 31565200.
2. add the addends to obtain the final product.
This breakdown informs the following decomposition of the multiplication
operation into simpler parts as follows.
*/
private static List<Integer> multiplyMagnitudeByDigitAndShift(List<Integer> list,
int digit, int zeros){
/*
list will be the magnitude of the higher factor, 78913 in our example,
which would be given as the list 3, 1, 9, 8, 7
digit will be a single digit from the lower factor, 462 in our example,
so it will be successively 2, 6, and 4.
zeros will be the number of will be the number of 0's to add at the front
of the result, and so would successively be 0, 1, and 2
The assumptions are
list is a non empty sequence of Integers following our conventions
digit is an int value in the range from 0 to 9
zeros is a nonnegative integer value
The method should return the magnitude that is obtained by multiplying
list by the single digit with zeros 0's added at the front(that is, the low
order of the list).
YOU MUST USE A LISTITERATOR to iterate over list. You can use the add method
to add the result digits to the new list and to add the 0's on the front.
You may want to handle the digit values of 0 and 1 as special cases.
list must not be modified by the method.
*/
List<Integer> result = new LinkedList<Integer>();
for(int i = 0;i<zeros;i++) {
result.add(0);
}
ListIterator<Integer> iter = list.listIterator(0);
int carry = 0;
while (iter.hasNext()) {
int n1 = iter.next();
int r = (n1 * digit) + carry;
result.add(r%10);
carry = r/10;
}
if(carry>0) {
result.add(carry);
}
return result;
}
/*
YOU MUST CODE THIS
Takes two input lists, A and B, and produces an entirely new
list, call it C, such that C is the magnitude of the product
of A and B.
you can split it this way.
Simple Cases
A B
0 any C is the list for 0
any 0 C is the list for 0
1 any copy B into C
any 1 copy A into C
If A and B are none of those, then you know each is >1. We will
calculate the product into a List<Integer> C.
Suppose A's size is a and B's size is b, and that
a >= b (if not, then swap the two references). You want to iterate
over the digits of the shorter list, B, and have A serve as the
the higher factor.
Initialize a result list C to be 0.
numZeros = 0
Iterate of the digits of B; for each digit n{
temp = multiplyMagnitudeByDigitAndShift(A, n, numZeros);
C = addMagnitudes(C, temp);
numZeros++;
}
return C;
YOU MUST USE A LISTITERATOR FOR B
neither A nor B should be changed
*/
static List<Integer> multiplyMagnitudes(List<Integer> A, List<Integer> B){
LinkedList<Integer> C;
if(A.get(A.size()-1)==0 || B.get(B.size()-1)==0) {
C = new LinkedList<Integer>();
C.add(zeroInteger);
}
else if(A.size()==1 && A.get(0)==1) {
C = new LinkedList<Integer>(B);
}
else if(B.size()==1 && B.get(0)==1) {
C = new LinkedList<Integer>(A);
}
else {
int zeros = 0;
SuperInt R = new SuperInt();
if(A.size()>=B.size()) {
ListIterator<Integer> iterB = B.listIterator(0);
while (iterB.hasNext()) {
int n = iterB.next();
LinkedList<Integer> Z = new LinkedList<Integer>(multiplyMagnitudeByDigitAndShift(A, n, zeros));
zeros++;
R.digSeq = addMagnitudes(R.digSeq, Z);
}
}
else {
ListIterator<Integer> iterA = A.listIterator(0);
while (iterA.hasNext()) {
int n = iterA.next();
LinkedList<Integer> Z = new LinkedList<Integer>(multiplyMagnitudeByDigitAndShift(B, n, zeros));
zeros++;
R.digSeq = addMagnitudes(R.digSeq, Z);
}
}
C = new LinkedList<Integer>(R.digSeq);
}
return C;
}
/*
YOU MUST CODE THIS
returns a new SuperInt object representing the
the signed result of multiplying this's value
with other's value
Once you do the multiplyMagnitudes, you can use it
to create the magnitude of the result. This is because
|x * y| = |x| * |y|
The sign is easily calculated from the signs of this
and other. If the result represents 0, it is not negative,
else it's negative if the signs of this and other
disagree. It's positive if they have the same sign.
neither this nor other should be changed
*/
public SuperInt multiply(SuperInt other){
SuperInt product = new SuperInt();
product.digSeq = multiplyMagnitudes(digSeq, other.digSeq);
if(product.digSeq.get(product.digSeq.size()-1)==0 || isNeg==other.isNeg) {
product.isNeg = false;
}
else {
product.isNeg = true;
}
return product;
}
// like the last, but this is modified to hold the result
// YOU MUST CODE THIS
// should work if this and other are the same object
public void multiplyMutate(SuperInt other){
SuperInt C = multiply(other);
isNeg = C.isNeg;
digSeq = C.digSeq;
}
/*
YOU MUST CODE THIS
returns true if this and other are both SuperInt objects and
represent the same value, else false
Note, if Object is NOT a SuperInt object, the method should
return false.
*/