Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You need to complete multiply() method (ToDo) in LargeInt class here is largeint class SpecializedList class.. and largeintapp PLEASE MAKE SURE IT WORK CORRECTLY. //----------------------------------------------------------------------------

You need to complete multiply() method (ToDo) in LargeInt class

here is largeint class SpecializedList class.. and largeintapp

PLEASE MAKE SURE IT WORK CORRECTLY.

//---------------------------------------------------------------------------- // LargeInt.java by Dale/Joyce/Weems Chapter 7 // // 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 ch07.largeInts; import ch07.byteLists.*; public class LargeInt { private SpecializedList numbers; // Holds digits // Constants for sign variable private static final boolean PLUS = true; private static final boolean MINUS = false; private boolean sign; public LargeInt() // Instantiates an "empty" large integer. { numbers = new SpecializedList(); sign = PLUS; } public LargeInt(String intString) // Precondition: intString contains a well-formatted integer // // Instantiates a large integer as indicated by intString { numbers = new SpecializedList(); 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 <= lastDigitPosition; count++) { digitChar = intString.charAt(count); digitInt = Character.digit(digitChar, 10); digitByte = (byte)digitInt; addDigit(digitByte); } }

public void setNegative() { sign = MINUS; } public void addDigit(byte digit) { numbers.addEnd(digit); } public String toString()

{ String largeIntString; if (sign == PLUS) largeIntString = "+"; else largeIntString = "-"; int length; length = numbers.size(); numbers.resetForward(); for (int count = length; count >= 1; count--) { largeIntString = largeIntString + numbers.getNextElement(); if ((((count - 1) % 3) == 0) && (count != 1)) largeIntString = largeIntString + ","; } return(largeIntString); }

private static boolean greaterList(SpecializedList first, SpecializedList 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() < second.size()) greater = false; else { byte digitFirst; byte digitSecond; first.resetForward(); second.resetForward(); // Set up loop int length = first.size(); boolean keepChecking = true; int count = 1; while ((count <= length) && (keepChecking)) { digitFirst = first.getNextElement(); digitSecond = second.getNextElement(); if (digitFirst > digitSecond) { greater = true; keepChecking = false; } else if (digitFirst < digitSecond) { greater = false; keepChecking = false; } count++; } } return greater; }

private static SpecializedList addLists(SpecializedList larger, SpecializedList 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; SpecializedList result = new SpecializedList(); larger.resetBackward(); smaller.resetBackward(); // Process both lists while both have digits for (int count = 1; count <= smallerLength; count++) { digit1 = larger.getPriorElement(); digit2 = smaller.getPriorElement(); temp = (byte)(digit1 + digit2 + carry); carry = (byte)(temp / 10); result.addFront((byte)(temp % 10)); } // Finish processing of leftover digits lengthDiff = (largerLength - smallerLength); for (int count = 1; count <= lengthDiff; count++) { digit1 = larger.getPriorElement(); temp = (byte)(digit1 + carry); carry = (byte)(temp / 10); result.addFront((byte)(temp % 10)); } if (carry != 0) result.addFront((byte)carry); return result; } private static SpecializedList addListsSafely(SpecializedList first, SpecializedList second) { if (greaterList(first, second)) return addLists(first, second); else return addLists(second, first); }

private static SpecializedList subtractLists(SpecializedList larger, SpecializedList 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; SpecializedList result = new SpecializedList(); larger.resetBackward(); smaller.resetBackward(); // Process both lists while both have digits. for (int count = 1; count <= smallerLength; count++) { digit1 = larger.getPriorElement(); if (borrow) { if (digit1 != 0) { digit1 = (byte)(digit1 - 1); borrow = false; } else { digit1 = 9; borrow = true; } } digit2 = smaller.getPriorElement(); if (digit2 <= digit1) result.addFront((byte)(digit1 - digit2)); else { borrow = true; result.addFront((byte)(digit1 + 10 - digit2)); } } // Finish processing of leftover digits lengthDiff = (largerLength - smallerLength); for (int count = 1; count <= lengthDiff; count++) { digit1 = larger.getPriorElement(); if (borrow) { if (digit1 != 0) { digit1 = (byte)(digit1 - 1); borrow = false; } else { digit1 = 9; borrow = true; } } result.addFront(digit1); } return result; }

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; second.numbers.resetForward(); int length = second.numbers.size(); for (int count = 1; count <= length; count++) negSecond.numbers.addEnd(second.numbers.getNextElement()); // Add first to inverse of second diff = add(first, negSecond); return diff; } //ToDo public static LargeInt multiply(LargeInt first, LargeInt second){

LargeInt mult = new LargeInt(); //.... return mult; } }

.........................................

..........................................

//---------------------------------------------------------------------------- // SpecializedList.java by Dale/Joyce/Weems Chapter 7 // // Implements the specialized list ADT using a doubly linked list of nodes //---------------------------------------------------------------------------- package ch07.byteLists; public class SpecializedList implements SpecializedListInterface { protected class SListNode // List nodes for the specialized list implementation { protected byte info; // The info in a list node protected SListNode next; // A link to the next node on the list protected SListNode back; // A link to the previous node on the list } protected SListNode listFirst; // Reference to the first node on list protected SListNode listLast; // Reference to the last node on the list protected int numElements; // Number of elements in the list protected SListNode currentFPos; // Current forward position for iteration protected SListNode currentBPos; // Current backward position for iteration public SpecializedList() // Creates an empty list object { numElements = 0; listFirst = null; listLast = null; currentFPos = null; currentBPos = null; }

public int size() // Determines the number of elements on this list { return numElements; }

public void resetForward() // Initializes current forward position for an iteration through this list { currentFPos = listFirst; } public byte getNextElement () // Returns the value of the next element in list in forward iteration { byte nextElementInfo = currentFPos.info; if (currentFPos == listLast) currentFPos = listFirst; else currentFPos = currentFPos.next; return nextElementInfo; } public void resetBackward() // Initializes current backward position for an iteration through this list { currentBPos = listLast; } public byte getPriorElement () // Returns the value of the next element in list in backward iteration { byte nextElementInfo = currentBPos.info; if (currentBPos == listFirst) currentBPos = listLast; else currentBPos = currentBPos.back; return nextElementInfo; }

public void addFront (byte element) // Adds the value of element to the beginning of this list { SListNode newNode = new SListNode(); // Reference to node being added newNode.info = element; newNode.next = listFirst; newNode.back = null; if (listFirst == null) // Adding into an empty list { listFirst = newNode; listLast = newNode; } else // Adding into a non-empty list { listFirst.back = newNode; listFirst = newNode; } numElements++; }

public void addEnd (byte element)

// Adds the value of element to the end of this list { SListNode newNode = new SListNode(); // Reference to node being added newNode.info = element; newNode.next = null; newNode.back = listLast; if (listFirst == null) // Adding into an empty list { listFirst = newNode; listLast = newNode; } else // Adding into a non-empty list { listLast.next = newNode; listLast = newNode; } numElements++; }

public String toString() { String largeIntString=""; int length=numElements; resetForward(); for (int count = length; count >= 1; count--) { largeIntString = largeIntString + getNextElement(); if ((((count - 1) % 3) == 0) && (count != 1)) largeIntString = largeIntString + ","; } return(largeIntString); } }

.......................................

public interface SpecializedListInterface { void resetForward(); // Initializes current forward position for this list to the first // byte on the list.

byte getNextElement (); // Preconditions: The list is not empty // The list has been resetForward // The list has not been modified since the most recent reset // // Returns the value of the byte at the current forward position on // this list. If the current forward position is the last element, then // it advances the value of the current forward position to the first element; // otherwise, it advances the value of the current forward position to the // next element.

void resetBackward(); // Initializes current backward position for this list to the last // byte on the list. byte getPriorElement (); // Preconditions: The list is not empty // The list has been resetBackward // The list has not been modified since the most recent reset // // Returns the value of the byte at the current backward position on // this list. If the current backward position is the first element, then // it advances the value of the current backward position to the last element; // otherwise, it advances the value of the current backward position to the // previous element. int size(); // Returns the number of elements on this list.

void addFront (byte element); // Adds the value of element to the front of this list.

void addEnd (byte element); // Adds the value of element to the end of this list. }

............................................

//--------------------------------------------------------------------- // LargeIntApp.java by Dale/Joyce/Weems Chapter 7 // // Allows user to add or subtract large integers. //----------------------------------------------------------------------

import java.util.Scanner; import ch07.largeInts.LargeInt; import java.math.BigInteger; public class LargeIntApp { public static void main(String[] args)

{ Scanner conIn = new Scanner(System.in);

LargeInt first; LargeInt second;

String intString1, intString2; String more = null; // used to stop or continue processing BigInteger bi1 = new BigInteger("123456789"); BigInteger bi2 = new BigInteger("23456789"); BigInteger bi1s,bi2s; System.out.println("123456789 times 23456789 = "+bi1.multiply(bi2)); do { // Get large integers. System.out.println("Enter the first large integer: "); intString1 = conIn.nextLine(); first = new LargeInt(intString1); System.out.println("Enter the second large integer: "); intString2 = conIn.nextLine(); second = new LargeInt(intString2); System.out.println(); // Perform and report the addition and subtraction. System.out.print("First number: "); System.out.println(first); System.out.print("Second number: "); System.out.println(second); System.out.print("Sum: "); System.out.println(LargeInt.add(first,second)); System.out.print("Difference: "); System.out.println(LargeInt.subtract(first,second)); System.out.print("Multiplication: "); System.out.println(LargeInt.multiply(first,second)); bi1s = new BigInteger(intString1); bi2s = new BigInteger(intString2); System.out.println("Using BigIntegers: "+bi1s+" times "+bi2s+" = "+bi1s.multiply(bi2s)); // Determine if there is more to process. System.out.println(); System.out.print("Process another pair of numbers? (Y=Yes): "); more = conIn.nextLine(); System.out.println();

} while (more.equalsIgnoreCase("y")); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn Mysql The Easy Way A Beginner Friendly Guide

Authors: Kiet Huynh

1st Edition

B0CNY7143T, 979-8869761545

More Books

Students also viewed these Databases questions