Question: Given two polynomials equation, write a function that adds and multiply the given two polynomials. It adds and multiply the two equations which are in

Given two polynomials equation, write a function that adds and multiply the given two polynomials.

It adds and multiply the two equations which are in standard form and return them in the sum poly which also be in standard form.

Write one add method and one multiply method.

class PLL {

private static class Node {

private int coefficient;

private int exponent;

private Node next;

public Node(int coe, int exp) {

this(coe, exp, null);

}

public Node(int coe, int exp, Node n) {

coefficient = coe;

exponent = exp;

next = n;

}

public void setCoefficient(int coe) {

coefficient = coe;

}

public void setExponent(int exp) {

exponent = exp;

}

public void setNext(Node n) {

next = n;

}

public int getCoefficient() {

return coefficient;

}

public int getExponent() {

return exponent;

}

public Node getNext() {

return next;

}

}

private Node first;

private Node last;

public PLL() {

first = last = null;

}

public PLL(int c, int e) {

Node tempn = new Node(c, e);

first = last = tempn;

}

public void print() {

if (first == null) {

System.out.println();

return;

}

Node temp = first;

String ans = "";

while (temp != null) {

if (temp.getCoefficient() > 0) {

if (temp != first) ans = ans + " + ";

ans = ans + temp.getCoefficient();

} else if (temp.getCoefficient() < 0) ans = ans + " - " + temp.getCoefficient() * -1;

if (temp.getExponent() != 0) {

ans = ans + "X^" + temp.getExponent();

}

temp = temp.getNext();

}

System.out.println(ans);

}

}

public class pll {

public static void main(String argc[]) {

PLL ans_sum,ans_mul;

PLL p1 = new PLL(4, 2);

PLL p2 = new PLL(6, 5);

p3 = p1.add(p2);

p1 = new PLL(2, 4);

p2 = new PLL(5, 6);

p4 = p1.add(p2);

ans_sum = p3.add(p4);

ans_mul = p3.multiply(p4);

ans_sum.print();

ans_mul.print();

p1.print();

p2.print();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!