• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

pass x*x*x - 3*x*x +5*x + 8 in HTML file so java make it a single-variable polynomial

hi, i need to parse the string x*x*x - 3*x*x +5*x + 8; so that i can pass it into HTML and java knows what to do with it.

here is a question that i can do: (correct and verified) it has to do with the question that im asking
y = 0.5 x ^ 3 = x * x * x * 0.5                                             ^
// Class PlotD plots y = x^3 - 3x^2 + 5x + 8
public class PlotD extends Plotter {
public double func(double x) {
return x*x*x - 3*x*x +5*x + 8;

this one i need help on :
Now i have a generic plotter question:
write a generic plotter applet that takes a parameter func that represents a single-variable polynomial function and plots the function.
For example, the input might be in the form
x^3 - 3 * x^2 +5*x + 8
somehow in the html file it seems i need to pass this value and somehow parse it so that java knows what to make of it.

     <TITLE> The Plotter Applet </TITLE>
<BODY BGCOLOR=black TEXT=white>
    <H1> The Plotter Applet</H1>
        <APPLET CODE=PlotD.class
               WIDTH=1250 HEIGHT=1150>
        <PARAMETER NAME=myval VALUE=x*x*x -3*x*x + 5*x + 8 >

one i cant do:
2 Solutions

Read up on the StringTokenizer class (java.util.StringTokenizer). This provides the basics for parsing the string.

Once you have the individual tokens, you need to have some "ground rules" built in. For example:
- since your problem domain is restricted to single-variable polynomials, you need the applet to recognize the operator signs  +, -, *, /. And perhaps ^ (exponentiation), or do you want to expand all exponentiation using * (multiplication) as your example equation does?

- for completeness, you should also be able to understand parentheses () to group the operations together.

- do you want to recognize only 'x' as a valid variable symbol, or any letter of the alphabet?

- finally, your code should know the default operator precedence rules where there are no parentheses - i.e. exponentiation takes precedence over everything else, followed by division, multiplication, subtraction, and addition in that order (I think this is the right order...)

- Ravs

Do you want to assume
First of all, this is not an easy task.

The general design for a polynomial function is f(x) = sum(i=0:n) a_i * x^i  

In order to be able to plot that function, you  have to identify the array of coefficients a_i (or set, or whatever structure you need to use). It would also be useful if you could narrow down a constraint for the format of the function. For example: x^3 - 3 * x^2 +5*x + 8 and -3 * x^2 + x^3 +5*x + 8 are equivalent, but the arrangement is different - this is where recognizing the coefficient array is important. In the case above, the array is [1, -3, 5, 8 ] or [8, 5, -3, 1] depending on how you want to arrange it.

To get to the array of coefficients, you will need to create a Parser (including a Tokenizer like ravs suggests) in order to be able to read your function. What the tokenizer does is organize the input function into tokens. For example, if the string is "x*x + 1" the tokens will be "x", "*", "x", "+", "1."

These are just some suggestions. There are many ways to accomplish this type of project, but in general the concept is the same. Look into parsers, scanners and lexers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now