Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:49:19+00:00 2026-05-27T01:49:19+00:00

I want to modify the below code to read the nodes from a text

  • 0

I want to modify the below code to read the nodes from a text file (vs. hard-coded values)

Furthermore, read data from text file in the format:

P1 = 3 5 1 -1 0 8

P2 = 5 6 2 -1 1 7 0 -4

etc…

Name the values P(x) and input the remaining data. Any advice?

import java.io.DataInputStream;
import java.util.LinkedList;
import java.util.ListIterator;

// A term contains the coefficient and the exponent of x.
class Term {
public Term(int c, int d) {
    coeff = c;
    degree = d;
}

public Term multiply(Term other) {
    return new Term(coeff * other.coeff, degree + other.degree);
}

public void add(int c) {
    coeff += c;
}

public void sub(int c) {
    coeff -= c;
}

public int getCoeff() {
    return coeff;
}

public int getDegree() {
    return degree;
}

private int coeff;
private int degree;
}

// A polynomial made up of terms.
class Polynomial {
// Construct a Polynomial object.
public Polynomial() {
    terms = new LinkedList();
}

public Polynomial add(Polynomial p) {
    Polynomial r = new Polynomial();
    ListIterator iterator = terms.listIterator();
    while (iterator.hasNext()) {
        r.add((Term) iterator.next());
    }
    ListIterator pIterator = p.terms.listIterator();
    while (pIterator.hasNext()) {
        r.add((Term) pIterator.next());
    }
    return r;
}

public Polynomial sub(Polynomial p) {
    Polynomial r = new Polynomial();
    ListIterator iterator = terms.listIterator();
    while (iterator.hasNext()) {
        r.sub((Term) iterator.next());
    }
    ListIterator pIterator = p.terms.listIterator();
    while (pIterator.hasNext()) {
        r.sub((Term) pIterator.next());
    }
    return r;
}

public Polynomial multiply(Polynomial p) {
    Polynomial r = new Polynomial();
    ListIterator iterator = terms.listIterator();
    while (iterator.hasNext()) {
        ListIterator pIterator = p.terms.listIterator();
        Term t1 = (Term) iterator.next();
        while (pIterator.hasNext()) {
            Term t2 = (Term) pIterator.next();
            r.add(t1.multiply(t2));
        }
    }
    return r;
}

// Adds a coefficient and degree as a new Term.

public void addTerm(int c, int d) {
    add(new Term(c, d));
}

// Adds a term

public void add(Term t) {
    int c = t.getCoeff();
    int d = t.getDegree();

    ListIterator iterator = terms.listIterator();
    while (iterator.hasNext()) {
        Term current = (Term) iterator.next();
        if (d == current.getDegree()) {
            if (c == -current.getCoeff())
                iterator.remove();
            else
                current.add(c);
            return;
        } else if (d < current.getDegree()) {
            iterator.previous();
            iterator.add(t);
            return;
        }
    }
    iterator.add(t);
}

public void sub(Term t) {
    int c = t.getCoeff();
    int d = t.getDegree();

    ListIterator iterator = terms.listIterator();
    while (iterator.hasNext()) {
        Term current = (Term) iterator.next();
        if (d == current.getDegree()) {
            if (c == current.getCoeff())
                iterator.remove();
            else
                current.add(-c);
            return;
        } else if (d < current.getDegree()) {
            iterator.previous();
             iterator.add(t);
            current.add(-c);
            return;
        }
    }
    iterator.add(t);

}

// Prints the polynomial.
public void print() {
    ListIterator iterator = terms.listIterator();

    while (iterator.hasNext()) {
        Term current = (Term) iterator.next();
        if (current.getCoeff() != 0) {
            if ((current.getCoeff() < 0) || (!(current.getDegree() != 0))) {
                System.out.print(" ");
            }

            else {
                System.out.print(" + ");
            }
            System.out.print(current.getCoeff());

            if (current.getDegree() > 0) {
                System.out.print("x");
                if (current.getDegree() > 1)
                    System.out.print("^" + current.getDegree());
            }
        }
    }
    System.out.println();
}

@SuppressWarnings("deprecation")
public char getChar() {
    char val = ' ';
    String buffer = "";

    DataInputStream ds = new DataInputStream(System.in);
    try {
        buffer = ds.readLine();
        val = buffer.charAt(0);
    } catch (Exception e) {
    }

    return (val);
}

public char enterNext() {
    System.out.println("Enter next Polynomial? ");
    char ans = this.getChar();
    switch (ans) {
    case 'n':
    case 'N':
        System.out.println("All Done.");
        System.exit(0);
        break;
    case 'y':
    case 'Y':
        System.out.println("Enter next one, please!");
    }
    return ans;
}

private LinkedList terms;
}

// A test program for Polynomial and Term classes.

public class RPC {
private static char ans = 'y';

public static void main(String[] args) {
    Polynomial p = new Polynomial();
    do
     {
    p.addTerm(10, 0);
    p.addTerm(-1, 1);
    p.addTerm(9, 7);
    p.addTerm(5, 10);
    System.out.print("f(x) = ");
    p.print();
    System.out.println();
    Polynomial q = new Polynomial();
     do
    {
    q.addTerm(1, 0);
    q.addTerm(-11, 1);
    q.addTerm(19, 7);
    q.addTerm(15, 10);
    System.out.print("q(x) = ");
    q.print();
     p.enterNext();
    }
     while (!(ans != 'y') | !(ans != 'Y'));
    System.out.println();

    Polynomial a = p.add(q);
    System.out.print("f(x) + q(x) = ");
    a.print();
    Polynomial s = p.sub(p);
    s.print();
    Polynomial m = p.multiply(q);
    System.out.print("f(x) * q(x) = ");
    m.print();
    System.exit(0);

} while (ans != 1000);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:49:19+00:00Added an answer on May 27, 2026 at 1:49 am

    Unless your data is already in binary form, I wouldn’t use DataInputStream. The example below uses BufferedReader to compose org.jscience.mathematics.function.Polynomial.

    Note that the highest order coefficient is first for convenience in applying Horner’s scheme, as shown in this example.

    1 2 3 4
    4 3 2 1
    
    Coefficient: 1 2 3 4
    Polynomial:  [1]x³ + [2]x² + [3]x + [4]
    Coefficient: 4 3 2 1
    Polynomial:  [4]x³ + [3]x² + [2]x + [1]
    

    Code:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import org.jscience.mathematics.function.Polynomial;
    import org.jscience.mathematics.function.Term;
    import org.jscience.mathematics.function.Variable;
    import org.jscience.mathematics.number.Integer64;
    
    /** @see http://stackoverflow.com/questions/8276150 */
    public class ReadPoly {
    
        public static void main(String[] args) throws IOException {
            BufferedReader r = new BufferedReader(new FileReader("test.txt"));
            String s;
            while ((s = r.readLine()) != null) {
                System.out.println("Coefficient: " + s);
                Polynomial<Integer64> p = create(s.split(" "));
                System.out.println("Polynomial:  " + p);
            }
        }
    
        public static Polynomial<Integer64> create(String... a) {
            Variable<Integer64> x = new Variable.Local<Integer64>("x");
            Polynomial<Integer64> px = Polynomial.valueOf(Integer64.ZERO, x);
            for (int i = 0, e = a.length - 1; i < a.length; i++, e--) {
                px = px.plus(Polynomial.valueOf(
                    Integer64.valueOf(a[i]), Term.valueOf(x, e)));
            }
            return px;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to modify a connection string that's hard-coded in a Java application (jar
I want to modify some data on XML file residing on server side by
below is the code which i want to modify $input = fopen(php://input, r); $temp
I want to modify some code in the Eclipse JDT itself (In the Debug
I want to modify a view from Action Helper in Zend Framework in preDispatch()
Whenever I want to modify a winform from another thread, I need to use
I have below code in html <div id=divTest> Hello <input type=text id=txtID/> <input type=text
I can't figure out how to modify the C# code below, which works as
Question summary: How do I modify the code below so that untrusted, dynamically-loaded code
I want to modify the solution for highlighting trailing whitespace described here by NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.