Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// --------------------------- // Multiplication and Division // --------------------------- /** * Multiplies the integer v represented by this object with another number n. Note that v

// --------------------------- // Multiplication and Division // --------------------------- /** * Multiplies the integer v represented by this object with another number n. Note that v may * be too large (in which case this.value == OVERFLOW). You can do this in one loop: Factor n and * traverse the doubly linked list simultaneously. For details refer to Section 3.1 in the * project description. Store the prime factorization of the product. Update value and size. * * @param n * @throws IllegalArgumentException if n < 1 */ public void multiply(long n) throws IllegalArgumentException { // TODO } /** * Multiplies the represented integer v with another number in the factorization form. Traverse both * linked lists and store the result in this list object. See Section 3.1 in the project description * for details of algorithm. * * @param pf */ public void multiply(PrimeFactorization pf) { PrimeFactorizationIterator iter = pf.iterator(); // TODO } /** * Multiplies the integers represented by two PrimeFactorization objects. * * @param pf1 * @param pf2 * @return object of PrimeFactorization to represent the product */ public static PrimeFactorization multiply(PrimeFactorization pf1, PrimeFactorization pf2) { // TODO return null; }

Please fill in the TODO section and in Java syntax.

3.1) Discription: The first method can be implemented via a traversal of the linked list referenced by head using an iterator (which is an object of the private class PrimeFactorizationIterator) as follows. Factor n using the direct search factorization. For each prime factor found (with a multiplicity), move the iterator forward from its current position to the first node storing an equal or a larger prime. In the first case, increment the multiplicity field of that node. In the second case, create a new node for this factor and add it to the linked list before the larger prime. If the cursor reaches the end of the list, simply add the new node as the last node. The second method updates the linked list by traversing it and the linked list of the argument object pf simultaneously. Essentially, it merges a copy of the second list into the first list by doing the following on the first list: a) Copy over the nodes from the second list for prime factors that do not appear in the first list. b) For each prime factor shared with the second list, increment the multiplicity field of the PrimeFactor object stored at the corresponding node in the first list. The implementation requires the use of two iterators, one for each list, and works in a way similar to merging two sorted arrays into one.

The first two multiply() methods also call Math.multiplyExact() to carry out multiplication of this.value, if not 1, with n or pf.value. An ArithmeticException will be thrown if an overflow happens. In this case, set the value field to 1. Otherwise, store the product in value. Note that this.value == - 1 only means that the number represented by the linked list cannot be represented in Java as a long integer. We can still perform arithmetic operations on the large number by this PrimeFactorization object. The third multiply() method is a static one that takes two PrimeFactorization objects, and returns a new object storing the product of the two numbers represented by the first two objects.

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

Students also viewed these Databases questions