Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

Write a program in java for sparse polynomial

Expert Solution
Check Mark
Step 1

Multiplication of Sparse Polynomials

b) Program:

// SpasePolynomialsMultiplication.java

import java.util.ArrayList;

public class SpasePolynomialsMultiplication

{

/* create two objects for ArrayList class of type integers for two polynomial equations */

private ArrayList poly1 = new ArrayList();

private ArrayList poly2 = new ArrayList();

// parameterized constructor

public SpasePolynomialsMultiplication( ArrayList newPoly1, ArrayList newPoly2)

{

poly1 = newPoly1;

poly2 = newPoly2;

} // end of constructor

// polynomialsMultiplication method

public ArrayList polynomialsMultiplication()

{

ArrayList polyResult = new ArrayList();

for(int i = 0; i < poly1.size() + poly2.size()-1; i++){

polyResult.add(i, 0);

//Repeat the loop for all coefficients of the first equation.

for(int i = 0; i < poly1.size(); i++)

{

/* repeat the loop for all coefficients of the second equation */

for(int j = 0; j < poly2.size(); j++)

{

int value = poly1.get(i) * poly2.get(j);

int previousValue = polyResult.get(i + j);

//Store the resultant coefficients in the resultant equation.

polyResult.set(i + j, value + previousValue);

} // end inner for

} // end outer for

}

// return resultant equation

return polyResult;

} // end of polynomialsMultiplication method

// start main method

public static void main(String[] args)

{

//Create two objects for ArrayList class of type integers for two equations.

ArrayList equation1 = new ArrayList();

ArrayList equation2 = new ArrayList();

// In a sparse polynomial equation, the number of non-zero coefficients should be less than the number of zero-value coefficients.

// add coefficients to the first equation

equation1.add(0); // at exponential 0

equation1.add(0); // at exponential 1

equation1.add(1); // at exponential 2

equation1.add(0); // at exponential 3

equation1.add(-2); // at exponential 4

// In a sparse polynomial equation, the number of non-zero coefficients should be less than the number of zero-value coefficients.

// add coefficients to the second equation

equation2.add(-5); // at exponential 0

equation2.add(0); // at exponential 1

equation2.add(0); // at exponential 2

equation2.add(0); // at exponential 3

equation2.add(2); // at exponential 4

// Create an object for SpasePolynomialsMultiplication class with two equations.

SpasePolynomialsMultiplication spm = new SpasePolynomialsMultiplication(equation1, equation2);

// Call polynomialsMultiplication method to get the resultant equation of the multiplication of the two equations.

ArrayList result = spm.polynomialsMultiplication();

// display the first equation

System.out.print("Equation 1: ");

for(int i = equation1.size() - 1; i >= 0; i--)

{

if(equation1.get(i) != 0)

{

if(equation1.get(i) > 0)

System.out.print("+" + equation1.get(i) + "x^" + i);

else

System.out.print(equation1.get(i) + "x^" + i);

}

} // end for

// display the second equation

System.out.print("\nEquation 2: ");

for(int i = equation2.size() - 1; i >= 0; i--)

{

if(equation2.get(i) != 0)

{

if(equation2.get(i) > 0)

System.out.print("+" + equation2.get(i) + "x^" + i);

else

System.out.print(equation2.get(i) + "x^" + i);

}

} // end for

// display the resultant equation

System.out.print("\nResultant Equation: ");

for(int i = result.size() - 1; i >= 0; i--)

{

if(result.get(i) != 0)

{

if(result.get(i) > 0)

System.out.print("+" + result.get(i) + "x^" + i);

else

System.out.print(result.get(i) + "x^" + i);

}

} // end for

} // end of main method

} // end of SpasePolynomialsMultiplication class

Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY