Write a program in java for sparse polynomial

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a program in java for sparse polynomial

Expert Solution
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

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

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