The Artima Developer Community
Sponsored Link

Java Answers Forum
polynomial class

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
david villacis

Posts: 2
Nickname: rexcat
Registered: Nov, 2003

polynomial class Posted: Nov 23, 2003 9:36 PM
Reply to this message Reply
Advertisement
/**
* 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

}

}

Topic: How to convert a String into Clob in Java Previous Topic   Next Topic Topic: File transfer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use