Question
Write one add method and one multiply method for the code below: Given two polynomials equation, implement an adds and a multiply method for the
Write one add method and one multiply method for the code below:
Given two polynomials equation, implement an adds and a multiply method for 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.
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();
}
}
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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started