public static void main(String[] args) { // TODO code application logic here String ki = "x2 + 4x - 12"; double y = computeValue(ki, 0, x); x=2; } private double computeValue(String rawNumber, double x) { String current; int index1 = rawNumber.indexOf(43); // finding the position of + in the string int index2 = rawNumber.indexOf(45,1); // finding the position of - in the string after 1 int startIndex=0; int endIndex; boolean hasSign= ( index1 != -1 ) || (index2 != -1); boolean isVariable=false; double number=0; double coeff, power; int whereX=-1; coeff = power = 1; if(rawNumber.charAt(0) == '-') // if the first char is minus, make the coefficient negative { coeff=-coeff; startIndex=1; } if( hasSign) { if( index1 == -1 ) endIndex = index2; else if(index2 == -1 ) endIndex = index1; else endIndex = Math.min(index1, index2) - 1; } else endIndex = rawNumber.length() ; current=rawNumber.substring( startIndex, endIndex ).trim(); whereX=current.indexOf("x"); if(whereX != -1 ) //change the value of the coeff and power only if there is a x symbol { if( whereX > 0) coeff= coeff * Double.parseDouble( current.substring(0, whereX) ); if( whereX + 1 < current.length()) power=power * Double.parseDouble( current.substring(whereX+1 ) ); } else //otherwise, parse the whole string ( a constant number into the coefficient) { coeff = coeff * Double.parseDouble( current); power=0; } //note that if x^0 = 1 number= coeff * ( Math.pow(x, power) ); if( hasSign ) { if( rawNumber.charAt(endIndex) == '+' ) { number = number + isolateArg(rawNumber.substring(endIndex + 1), 0, x); } else { number = number - isolateArg(rawNumber.substring(index1 + 1), 0, x); } } return number; }

# finding the numeric value of a polynomial

Page 1 of 1## 4 Replies - 1026 Views - Last Post: 01 December 2011 - 09:24 AM

### #1

# finding the numeric value of a polynomial

Posted 30 November 2011 - 05:35 AM

I'm trying to compute the value of a polynomial from a string formula, but I can't get this one equation to return the right value and I suspect it's because it doesn't process the - sign properly ( the run is correct for x2+4x +12). In this code x2 means x squared so a power directly follows the x symbol. Please excuse the lack of comments, I'm a relatively new at programming and using forums.. What I basically try to do is isolate each term without the +/- signs and find their value before making recursive calls to the same compute function. PLease, help!

##
**Replies To:** finding the numeric value of a polynomial

### #2

## Re: finding the numeric value of a polynomial

Posted 30 November 2011 - 07:36 AM

Do you have to do this using Strings? Using classes and objects provides a much cleaner way of doing things. This should give you a better idea of what I'm talking about, and a good starting point. Now you can simply add Term objects to the Polynomial object.

class Term{ private int coefficient, exponent; //other code public double evaluate(){ return Math.pow(coefficient, exponent); } } class Polynomial{ private Term[] terms; //other code public double evaluate(){ double sum = 0; for(Term t:terms) sum += t.evaluate(); return sum; } }

### #3

## Re: finding the numeric value of a polynomial

Posted 30 November 2011 - 08:34 PM

My bad.. indexOf('+') and indexOf('-') would have been cleaner

This post has been edited by **pbl**: 30 November 2011 - 08:39 PM

### #4

## Re: finding the numeric value of a polynomial

Posted 01 December 2011 - 06:40 AM

I guess I am really wanting to make use of srings for this because I want to create a user friendly program, macosxnerd101. You did however raise the fact that I could encapsulate more and probably find an easier way to compute the numbers using a term-by term basis.

### #5

## Re: finding the numeric value of a polynomial

Posted 01 December 2011 - 09:24 AM

I would do this:

-Replace all - signs with +- so you can split() on the + signs and keep the - terms.

-Parse through the array returned by the split() method to parse each individual term into Term objects.

-Add those Term objects into the Polynomial object.

-Replace all - signs with +- so you can split() on the + signs and keep the - terms.

-Parse through the array returned by the split() method to parse each individual term into Term objects.

-Add those Term objects into the Polynomial object.

Page 1 of 1