Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.Scanner;
- class Term {
- public float coeff;
- public int degree;
- public Term(float coeff, int degree) {
- this.coeff = coeff; this.degree = degree;
- }
- public boolean equals(Object other) {
- return other != null && other instanceof Term && coeff == ((Term)other).coeff && degree == ((Term)other).degree;
- }
- public String toString() {
- if (degree == 0) {
- return coeff + "";
- } else if (degree == 1) {
- return coeff + "x";
- } else {
- return coeff + "x^" + degree;
- }
- }
- }
- class Node{
- Term term;
- Node next;
- public Node(float coeff, int degree, Node next) {
- term = new Term(coeff, degree); this.next = next;
- }
- }
- public class Polynomial {
- /**
- * Reads a polynomial from an input stream (file or keyboard). The storage
- * format of the polynomial is:
- *
- * <pre>
- * <coeff> <degree>
- * <coeff> <degree>
- * ...
- * <coeff> <degree>
- * </pre>
- *
- * with the guarantee that degrees will be in descending order. For example:
- *
- * <pre>
- * 4 5
- * -2 3
- * 2 1
- * 3 0
- * </pre>
- *
- * which represents the polynomial:
- *
- * <pre>
- * 4 * x ^ 5 - 2 * x ^ 3 + 2 * x + 3
- * </pre>
- *
- * @param sc Scanner from which a polynomial is to be read
- * @throws IOException If there is any input error in reading the polynomial
- * @return The polynomial linked list (front node) constructed from coefficients
- * and degrees read from scanner
- */
- public static Node read(Scanner sc) throws IOException {
- Node poly = null;
- while (sc.hasNextLine()) {
- Scanner scLine = new Scanner(sc.nextLine());
- poly = new Node(scLine.nextFloat(), scLine.nextInt(), poly);
- scLine.close();
- }
- return poly;
- }
- /**
- * Returns the sum of two polynomials - DOES NOT change either of the input
- * polynomials. The returned polynomial MUST have all new nodes. In other words,
- * none of the nodes of the input polynomials can be in the result.
- *
- * @param poly1 First input polynomial (front of polynomial linked list)
- * @param poly2 Second input polynomial (front of polynomial linked list
- * @return A new polynomial which is the sum of the input polynomials - the
- * returned node is the front of the result polynomial
- */
- public static Node add(Node poly1, Node poly2) {
- Node poly = null;
- Node p1 = poly1;
- while (p1 != null) {
- poly = addTerm(poly, p1.term.coeff, p1.term.degree);
- p1 = p1.next;
- }
- Node p2 = poly2;
- while (p2 != null) {
- poly = addTerm(poly, p2.term.coeff, p2.term.degree);
- p2 = p2.next;
- }
- return poly;
- }
- /**
- * Returns the product of two polynomials - DOES NOT change either of the input
- * polynomials. The returned polynomial MUST have all new nodes. In other words,
- * none of the nodes of the input polynomials can be in the result.
- *
- * @param poly1 First input polynomial (front of polynomial linked list)
- * @param poly2 Second input polynomial (front of polynomial linked list)
- * @return A new polynomial which is the product of the input polynomials - the
- * returned node is the front of the result polynomial
- */
- public static Node multiply(Node poly1, Node poly2) {
- Node poly = null;
- while(poly1.next != null) {
- Node start = poly2;
- while(start != null) {
- poly = addTerm(poly, poly1.term.coeff * start.term.coeff,
- poly1.term.degree + start.term.degree);
- start = start.next;
- }
- poly1 = poly1.next;
- }
- return poly;
- }
- // helper method to add a node in already existing list
- // The passed list poly is modified
- private static Node addTerm(Node poly, float coeff, int degree) {
- if(coeff == 0)
- return poly;
- // create node
- Node n = new Node(coeff, degree, null);
- // if it is the first element
- if (poly == null) {
- return n;
- } else {
- // if it has same exponent as head
- if (poly.term.degree == degree) {
- poly.term.coeff = (poly.term.coeff + coeff);
- return poly;
- } else if (poly.term.degree < degree) {
- // it need to be inserted before head
- n.next = poly;
- poly = n;
- return poly;
- }
- Node p = poly;
- while (p.next != null) {
- if (degree == p.term.degree) {
- // if same exponent term is there
- p.term.coeff = (p.term.coeff + coeff);
- return poly;
- } else if (degree > p.next.term.degree) {
- // loop till we get some lower exponent position
- n.next = p.next;
- p.next = n;
- return poly;
- }
- // move to next
- p = p.next;
- }
- if(p.term.degree == degree) {
- p.term.coeff = (p.term.coeff + coeff);
- return poly;
- }
- // add to the last
- p.next = n;
- return poly;
- }
- }
- /**
- * Evaluates a polynomial at a given value.
- *
- * @param poly Polynomial (front of linked list) to be evaluated
- * @param x Value at which evaluation is to be done
- * @return Value of polynomial p at x
- */
- public static float evaluate(Node poly, float x) {
- float result = 0;
- while(poly != null) {
- result += poly.term.coeff * Math.pow(x, poly.term.degree);
- poly = poly.next;
- }
- return result;
- }
- /**
- * Returns string representation of a polynomial
- *
- * @param poly Polynomial (front of linked list)
- * @return String representation, in descending order of degrees
- */
- public static String toString(Node poly) {
- if (poly == null) {
- return "0";
- }
- String retval = poly.term.toString();
- for (Node current = poly.next; current != null; current = current.next) {
- retval = current.term.toString() + " + " + retval;
- }
- return retval;
- }
- }
- Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!
- 11 Comments
- Comments
- Anonymous's AvatarAnonymous posted 1 year ago
- this is the node class public class Node{ Term term; Node next; public Node(float coeff, int degree, Node next) { term = new Term(coeff, degree); this.next = next; } }
- Anonymous's AvatarAnonymous posted 1 year ago
- where is term class?
- Anonymous's AvatarAnonymous posted 1 year ago
- public class Term { public float coeff; public int degree; public Term(float coeff, int degree) { this.coeff = coeff; this.degree = degree; } public boolean equals(Object other) { return other != null && other instanceof Term && coeff == ((Term)other).coeff && degree == ((Term)other).degree; }
- Anonymous's AvatarAnonymous posted 1 year ago
- public String toString() { if (degree == 0) { return coeff + ""; } else if (degree == 1) { return coeff + "x"; } else { return coeff + "x^" + degree; } }
- Anonymous's AvatarAnonymous posted 1 year ago
- i have updated the answer.. pleas check
- Anonymous's AvatarAnonymous posted 1 year ago
- thanks a lot now it works I really appreciate the help
- Anonymous's AvatarAnonymous posted 1 year ago
- Welcome!
- Anonymous's AvatarAnonymous posted 1 year ago
- Can you help with the part where if we add two opposite polynomials, one is negative, one is positive, it returns only 0? The code returns, 0x^5+0x^3... and so on
- Anonymous's AvatarAnonymous posted 1 year ago
- Can you please post a separate question with full details.. like what is input and output.. and we willl help.
- Anonymous's AvatarAnonymous posted 8 months ago
- Sir, this is a 5 months old question, we are not allowed to re-open old questions.. the person who posted this question has different requirements which were met.. now if you have any specific requirements, please post a new question with your issues.. we will surely help. Thanks!
- Anonymous's AvatarAnonymous posted 3 days ago
- Where is the addTerm function you called in class add?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement