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

Posted on 2007-07-26
Last Modified: 2013-11-23
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:
Question by:jxharding
    LVL 7

    Accepted Solution


    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
    LVL 20

    Assisted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now