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.
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);
}
}
Code:
// Java program to add two polynomials
class Main {
// A utility function to return maximum of two integers
static int max(int m, int n) {
return (m > n) ? m : n;
}
// A[] represents coefficients of first polynomial
// B[] represents coefficients of second polynomial
// m and n are sizes of A[] and B[] respectively
static int[] add(int A[], int B[], int m, int n) {
int size = max(m, n);
int sum[] = new int[size];
// Initialize the porduct polynomial
for (int i = 0; i < m; i++) {
sum[i] = A[i];
}
// Take ever term of first polynomial
for (int i = 0; i < n; i++) {
sum[i] += B[i];
}
return sum;
}
// A[] represents coefficients
// of first polynomial
// B[] represents coefficients
// of second polynomial
// m and n are sizes of A[] and B[] respectively
static int[] multiply(int A[], int B[],
int m, int n)
{
int[] prod = new int[m + n - 1];
// Initialize the porduct polynomial
for (int i = 0; i < m + n - 1; i++)
{
prod[i] = 0;
}
// Multiply two polynomials term by term
// Take ever term of first polynomial
for (int i = 0; i < m; i++)
{
// Multiply the current term of first polynomial
// with every term of second polynomial.
for (int j = 0; j < n; j++)
{
prod[i + j] += A[i] * B[j];
}
}
return prod;
}
// A utility function to print a polynomial
static void printPoly(int poly[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(poly[i]);
if (i != 0) {
System.out.print("x^" + i);
}
if (i != n - 1) {
System.out.print(" + ");
}
}
}
// Driver program to test above functions
public static void main(String[] args) {
// The following array represents polynomial 5 + 10x^2 + 6x^3
int A[] = {5, 0, 10, 6};
// The following array represents polynomial 1 + 2x + 4x^2
int B[] = {1, 2, 4};
int m = A.length;
int n = B.length;
// print The First polynomial Equation
System.out.println("First polynomial is:");
printPoly(A, m);
// print The Second polynomial Equation
System.out.println("\nSecond polynomial is:");
printPoly(B, n);
// print The Sum of polynomial Equations
int sum[] = add(A, B, m, n);
int size = max(m, n);
System.out.println("\nSum of polynomials is:");
printPoly(sum, size);
// print The Product of polynomial Equations
int[] prod = multiply(A, B, m, n);
System.out.println("\nProduct of polynomials is:");
printPoly(prod, m + n - 1);
}
}
Code Screenshots:



Output Screenshot:

Note: Please consider my work and give up vote.
Given two polynomials equation, write a function that adds and multiply the given two polynomials. It...
Add and subtract polynomials using linked lists:
The output should look like the following:
The code for the header file, Polynomial.h, is given as
such:
***********HEADER*************
#include <iostream>
#include <cstdlib>
using namespace std;
class polyll { //this is class POLYLL, but all lower case
private:
struct polynode {
float coeff;
int exp;
polynode* link;
} * p;
public:
polyll();
void poly_append(float c, int e);
void display_poly();
void poly_add(polyll& l1, polyll& l2);
void poly_subtract(polyll& l1, polyll& l2);
~polyll();
};
polyll::polyll()
{...
A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are real numbers, and n is a non-negative integer. n is called the degree of polynomial. Every term in a polynomial consists of a coefficient and an exponent. For example, for the first term axn, a is the coefficient and n is the exponent. This assignment is about representing and computing...
Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...
iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override public int lastIndexOf(Object arg0) { required code } @Override public ListIterator<R> listIterator() { required code } @Override public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...