/** * Univariate polynomials with double coefficients represented * as circular linked lists with dummy header node. Nodes that * represent terms with higher powers of the variable appear * earlier in the list than do nodes that represent terms with * lower powers of the variable. */ import java.io.*;
public class Polynomial{
// data members of Polynomial private Node head; private Node tail;
public static final Polynomial ZERO = new Polynomial(); // constructors /** create the zero polynomial */ public Polynomial(){ head = new Node(Term.ZERO); head.setNext(head); tail = head; } /** copy constructor */ public Polynomial(Polynomial poly){ Polynomial poly = new Polynomial();
}
/** @return polynomial degree */ public int degree(){
} public boolean equals(Object o){
} public Polynomial plus(Term term){
} public Polynomial plus(Polynomial poly){
} public Polynomial subtract(Term term){
} public Polynomial subtract(Polynomial poly){
} public Polynomial times(double factor){
} public Polynomial times(Term term){
} public Polynomial times(Polynomial poly){
} public double valueAt(double x){
}
/** output the polynomial */ public String toString(){
}
/** add a new term to the end of the list */ private void addToTail(Term term){ tail.setNext(new Node(term, head)); tail = tail.getNext(); } /** input the polynomial from the standard input stream */ public void input() throws Exception {
BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in),1);
// input number of terms System.out.print("Enter the number of non-zero terms: ");
int terms = Integer.parseInt(keyboard.readLine()); if (terms < 0){ System.out.println("The number of terms must be >= 0; " + "you entered " + terms); System.exit(-1); } if (terms > 0){ // at least one nonzero term, input the nonzero terms // in decreasing order of exponents System.out.println("\nEnter the nonzero terms " + "in decreasing order of exponents.\n" + "For example, for the polynomial 2x^7 "+ "+ x - 2,\n" + "give the input as a sequence 2, 7, "+ "1, 1, -2, 0\n" + "(without the commas, and one value per line)\n" );
// get first term System.out.print("Coefficient: "); double coefficient = Double.valueOf(keyboard.readLine()).doubleValue(); System.out.print("Exponent: "); int exponent = Integer.parseInt(keyboard.readLine()); // exponent must be >= 0 and coefficient must be nonzero if (exponent < 0 || coefficient == 0){ System.out.println("Exponent must be >= 0 and "+ "coefficient must not equal 0. "+ "Here, exponent = " + exponent + ", coefficient = " + coefficient); System.exit(-1); } this.addToTail(new Term(coefficient, exponent)); int lastExponent = exponent;//to check for input errors
// get the remaining terms for (int i = 2; i <= terms; i++){ // get next term System.out.print("Coefficient: "); coefficient = Double.valueOf(keyboard.readLine()).doubleValue(); System.out.print("Exponent: "); exponent = Integer.parseInt(keyboard.readLine()); // exponent must be > lastExponent and //coefficient must be nonzero if (exponent >= lastExponent || coefficient == 0){ System.out.println("Enter exponents in descending"+ " order; coefficients must not equal 0."); System.exit(-1); } this.addToTail(new Term(coefficient, exponent)); lastExponent = exponent; } } }
/** test program */ public static void main(String [] args)throws Exception { // to appear