Question
LargeIntList: https://pastebin.com/xG9XbVZD support.DLLNode: https://pastebin.com/ajqAGbCD --------------------------------------------------------------------- LargeInt.java https://pastebin.com/eGTkreEN //--------------------------------------------------------------------------- // LargeInt.java by Dale/Joyce/Weems Chapter 6 // // Provides a Large Integer ADT. Large integers can consist
LargeIntList: https://pastebin.com/xG9XbVZD
support.DLLNode: https://pastebin.com/ajqAGbCD
---------------------------------------------------------------------
LargeInt.java
https://pastebin.com/eGTkreEN
//--------------------------------------------------------------------------- // LargeInt.java by Dale/Joyce/Weems Chapter 6 // // Provides a Large Integer ADT. Large integers can consist of any number // of digits, plus a sign. Supports an add and a subtract operation. //--------------------------------------------------------------------------- package ch06.largeInts;
import java.util.Iterator; public class LargeInt { protected LargeIntList numbers; // Holds digits // Constants for sign variable protected static final boolean PLUS = true; protected static final boolean MINUS = false; protected boolean sign; public LargeInt() // Instantiates an "empty" large integer. { numbers = new LargeIntList(); sign = PLUS; } public LargeInt(String intString) // Precondition: intString contains a well-formatted integer // // Instantiates a large integer as indicated by intString { numbers = new LargeIntList(); sign = PLUS;
int firstDigitPosition; // Position of first digit in intString int lastDigitPosition; // Position of last digit in intString
// Used to translate character to byte char digitChar; int digitInt; byte digitByte; firstDigitPosition = 0; if (intString.charAt(0) == '+') // Skip leading plus sign firstDigitPosition = 1; else if (intString.charAt(0) == '-') // Handle leading minus sign { firstDigitPosition = 1; sign = MINUS; } lastDigitPosition = intString.length() - 1; for (int count = firstDigitPosition; count
public void setNegative() { sign = MINUS; } public String toString() { Byte element; String largeIntString; if (sign == PLUS) largeIntString = "+"; else largeIntString = "-"; int count = numbers.size(); Iterator forward = numbers.forward(); while (forward.hasNext()) { element = forward.next(); largeIntString = largeIntString + element; if ((((count - 1) % 3) == 0) && (count != 1)) largeIntString = largeIntString + ","; count--; } return(largeIntString); }
protected static boolean greaterList(LargeIntList first, LargeIntList second) // Precondition: first and second have no leading zeros // // Returns true if first represents a larger number than second; // otherwise, returns false { boolean greater = false; if (first.size() > second.size()) greater = true; else if (first.size()
// Set up loop int length = first.size(); boolean keepChecking = true; int count = 1; while ((count digitSecond) { greater = true; keepChecking = false; } else if (digitFirst
protected static LargeIntList addLists(LargeIntList larger, LargeIntList smaller) // Precondition: larger > smaller // // Returns a specialized list that is a byte-by-byte sum of the two // argument lists { byte digit1; byte digit2; byte temp; byte carry = 0; int largerLength = larger.size(); int smallerLength = smaller.size(); int lengthDiff; LargeIntList result = new LargeIntList(); Iterator largerReverse = larger.reverse(); Iterator smallerReverse = smaller.reverse(); // Process both lists while both have digits for (int count = 1; count
protected static LargeIntList subtractLists(LargeIntList larger, LargeIntList smaller) // Precondition: larger >= smaller // // Returns a specialized list that is the difference of the two argument lists { byte digit1; byte digit2; byte temp; boolean borrow = false; int largerLength = larger.size(); int smallerLength = smaller.size(); int lengthDiff; LargeIntList result = new LargeIntList(); Iterator largerReverse = larger.reverse(); Iterator smallerReverse = smaller.reverse(); // Process both lists while both have digits. for (int count = 1; count
public static LargeInt add(LargeInt first, LargeInt second) // Returns a LargeInt that is the sum of the two argument LargeInts { LargeInt sum = new LargeInt(); if (first.sign == second.sign) { if (greaterList(first.numbers, second.numbers)) sum.numbers = addLists(first.numbers, second.numbers); else sum.numbers = addLists(second.numbers, first.numbers); sum.sign = first.sign; } else // Signs are different { if (greaterList(first.numbers, second.numbers)) { sum.numbers = subtractLists(first.numbers, second.numbers); sum.sign = first.sign; } else { sum.numbers = subtractLists(second.numbers, first.numbers); sum.sign = second.sign; } } return sum; }
public static LargeInt subtract(LargeInt first, LargeInt second) // Returns a LargeInt that is the difference of the two argument LargeInts { LargeInt diff = new LargeInt(); // Create an inverse of second LargeInt negSecond = new LargeInt(); negSecond.sign = !second.sign; Iterator secondForward = second.numbers.forward(); int length = second.numbers.size(); for (int count = 1; count CSC 241 Lab 6 Implement and test the multiply method of the LargeInt class on the following two optoment numbers: 123456789 and 123456789 The multiply method can use the LargeInt add method For example 345 x 123 1035 6900 +34500 42435 Take 345 x 3-add 345 to itself 3 times 1035 345 x 2- add 345 to itsclf 2 times 690 and append "O" to it becausc the 2 is in the tens plac: 6900 345 x 1 = add 345 to itself l time-345 and append "00" to it because the l is in the hundreds place- 34500; all using the add method Now add the 1035, 6900, and 34500 using the add method again This approach would work. Also, make sure your commas are in the right place. Here is a sample driver: public class LargeIntDrivert public static void main(Stringll args) Largent x = new Largelnt("123456789"); Largelnt y - new Largelnt(" 123456789"); Largelnt z- Largelnt.add(x, y); Largelnt product = Largelnt.multiply(x, y)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started