I want to modify the below code to read the Polynomial Terms 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.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.*;
public class PolynomialTest
{
private Monomial head;
private double TOLERANCE = 0.00000001;
/***** the Monomial (inner) class ********************************/
private class Monomial
{
private DecimalFormat precision = new DecimalFormat("#.####");
private int exp; // coeff * x^exp
private double coeff;
private Monomial next;
public Monomial(int exp, double coeff, Monomial next)
{
this.exp = exp;
this.coeff = coeff;
this.next = next;
}
public String toString()
{
String form = precision.format(Math.abs(coeff));
if(exp == 0) return form ;
else
if(exp == 1) return form + "*x";
else
return form +"*x^" + exp;
}
public boolean equals(Monomial mono)
{
return exp == mono.exp && coeff == mono.coeff;
}
}
public PolynomialTest()
{
head = null;
}
/**
* Adds a new term into the polynomial, assuming that the polynomial
* is sorted in order from smallest to largest exponent.
*/
public void addTerm(int exp, double coeff)
{
if( Math.abs(coeff) < TOLERANCE ) return;
if( head == null || exp < head.exp )
{
head = new Monomial(exp, coeff, head);
return;
}
Monomial cur = head;
Monomial prev = null;
while( cur != null && exp > cur.exp)
{
prev = cur;
cur = cur.next;
}
if( cur == null || exp != cur.exp )
prev.next = new Monomial(exp, coeff, cur);
else
{
cur.coeff += coeff;
if( Math.abs(cur.coeff) < TOLERANCE )
if(prev != null)
prev.next = cur.next;
else
head = head.next;
}
}
public String toString()
{
StringBuffer sb = new StringBuffer();
for(Monomial tmp = head; tmp != null; tmp = tmp.next)
if(tmp.coeff < 0 )
sb.append(" - " + tmp.toString());
else
sb.append(" + " + tmp.toString());
return sb.toString();
}
/**
* Adds two polynomials
* The method does not change the original polynomial.
*/
public PolynomialTest add(PolynomialTest poly)
{
PolynomialTest res = clone();
for(Monomial tmp = poly.head; tmp != null; tmp = tmp.next)
res.addTerm(tmp.exp, tmp.coeff);
return res;
}
public PolynomialTest clone()
{
PolynomialTest res = new PolynomialTest();
for(Monomial tmp = head; tmp != null; tmp = tmp.next)
res.addTerm(tmp.exp, tmp.coeff);
return res;
}
public boolean equals(PolynomialTest poly)
{
Monomial tmp1 = head;
Monomial tmp2 = poly.head;
while(tmp1 != null && tmp2 != null)
{
if( !tmp1.equals(tmp2) ) return false;
tmp1 = tmp1.next;
tmp2 = tmp2.next;
}
return true;
}
/**
* Multiplies by a number
* The method does not change the original polynomial.
*/
public PolynomialTest multiply(double num)
{
PolynomialTest res = clone();
for(Monomial tmp = res.head; tmp != null; tmp = tmp.next)
tmp.coeff *= num;
return res;
}
/**
* Returns a new polynomial that is the derivative of this polynomial.
*/
public PolynomialTest diff()
{
PolynomialTest res = new PolynomialTest();
for(Monomial tmp = head; tmp != null; tmp = tmp.next)
{
if(tmp.exp != 0)
res.addTerm(tmp.exp - 1, tmp.coeff * tmp.exp );
}
return res;
}
/**
* Computes the polynomial at x = value
*/
public double eval(double value)
{
double res = 0;
for(Monomial tmp = head; tmp != null; tmp = tmp.next)
{
res += tmp.coeff * Math.pow(value, tmp.exp);
}
return res;
}
public static void main(String[] args)
{
PolynomialTest first = new PolynomialTest();
PolynomialTest second = new PolynomialTest();
System.out.println( "first" );
first.addTerm(1, 2.1);
first.addTerm(4, 2);
first.addTerm(3, 1);
first.addTerm(0, 1.3);
first.addTerm(4, 0.3);
System.out.println( first );
System.out.println( );
System.out.println( "second" );
second.addTerm(4, -2.3);
second.addTerm(2, 1);
second.addTerm(0, -1.3);
second.addTerm(1, 0.3);
System.out.println( second );
System.out.println( );
System.out.println( "compare first with second" );
System.out.println( first.equals(second) );
System.out.println( "compare first wih first" );
System.out.println( first.equals(first) );
System.out.println( );
System.out.println( "add first and second" );
PolynomialTest third = first.add(second);
System.out.println( third );
System.out.println( first );
System.out.println( second );
System.out.println( );
System.out.println( "multiply first by 0.2" );
PolynomialTest fourth = first.multiply(0.2);
System.out.println( fourth );
System.out.println( first );
System.out.println( );
System.out.println( "differentiate first" );
PolynomialTest diff = first.diff();
System.out.println( diff );
System.out.println( first );
System.out.println( );
System.out.println( "eval first at x = 1.5" );
System.out.println( first.eval(1.5) );
System.out.println( );
String content = new String();
String name = new String();
File file = new File("test.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNext()) {
name = sc.next();
content = sc.nextLine();
list.add(content);
}
sc.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
Iterator<String> i = list.iterator();
while (i.hasNext()) {
System.out.println(name + i.next() + "\n");
}
}
}